Can I get a -1 exit code using C++ on mac?

734 Views Asked by At

I was just wondering if it's possible to get a function in C++ on a Unix system to return -1 and not 255?

I have been programming some OpenCV stuff and realised stuff wasnt working as intended. The functions I was using were meant to return -1, but had been returning 255 the whole time. So I was just wondering if there is any configuration I'd be able to do to allow a return code of -1?

This is the example I am using from my textbook

int main( int argc, char** argv ) {
  int i = 0;
  cv::namedWindow( "Example3", cv::WINDOW_AUTOSIZE );
  cv::VideoCapture cap;
  cap.open( std::string(argv[1]) );
  cv::Mat frame;
  for(;;) {
    cap >> frame;
    if( frame.empty() ) break; // Ran out of film
    cv::imshow( "Example3", frame );
    std::cout<<"waitkey:"<<cv::waitKey(33) << std::endl;
  }
  return -1;
}

Information on waitkey() can be found here

2

There are 2 best solutions below

0
On BEST ANSWER

It seems that this issue is actually a bug with the OpenCV code. The bug has been addressed and pushed to the master branch of the OpenCV github

4
On

POSIX says,

The value of status may be 0, EXIT_SUCCESS, EXIT_FAILURE, or any other value, though only the least significant 8 bits (that is, status & 0377) shall be available from wait() and waitpid(); the full value shall be available from waitid() and in the siginfo_t passed to a signal handler for SIGCHLD.

So, it's the shell program that narrows the value to 8 bits when it uses certain POSIX APIs to manage its children.