E_learner
E_learner

Reputation: 3582

Strange error for copying data

I am trying to read video frames in OpenCV, then copy the data into my another C++ code to perform something else. My code is as follows:

cv::Mat capturedFrame; 
int newData[600][800];
std::cout<<"Debug1 " << std::endl;
memcpy((char*)newData, (char*)capturedFrame.data, 600*800*sizeof(int) );
std::cout<<"Debug2 " << std::endl;
mycode.setData ( newData );
std::cout<<"Debug3 " << std::endl;

Then the class "setData" was defined as follows:

char data [600][800];
void mycode::setData ( char newData[600][800] )
{
  for ( int m=0; m < 600; m ++ )
  {
      for ( int n = 0; n < 800; n ++ )
      {
          data[i][j] = newData[i][j]; 
      }
  }
}

But the code stops when it comes to the line:

    mycode.setData ( newData );

What confuses me is, if I delete this code, then I can see "Debug1" to "Debug3" on the screen, which is normal. But if I use this code, the program stops even without printing out "Debug1" and "Debug2" on the screen. This is really strange. I also tried to comment out all the lines in the "setData" class to make it be an empty class, but the error still is the same. So I believe it was not about the "setData" class. I also know that the "capturedFrame.data" was correct, because I performed some other filters on it, and the result was good. Can someone explain the error here?

Edit:

I used a debugger, but there was no error message but the program just stopped responding. Also, I changed the data type into "char".

Upvotes: 0

Views: 89

Answers (2)

Michael Burr
Michael Burr

Reputation: 340516

This array:

int newData[600][800];

is larger than 1 MB. If this is a local variable, then you're likely blowing the stack.

The same may go for the data array, but since your code snippets have very little context, it's hard to know what might be statically allocated vs. automatically allocated.

I think you should consider dynamically allocating these large arrays.

Upvotes: 4

FrostNovaZzz
FrostNovaZzz

Reputation: 842

If you comment out

mycode.setData ( newData );

the compile-optimizer may know the newData is not used, so

memcpy((int*)newData, (int*)capturedFrame.data, 600*800*sizeof(int) );

may be eliminated as well so it may not have been executed.

It's possible the problem exist in memcpy method, or even somewhere else.

Based on the limited information you provided, it's hard to investigate the real reason, but I suggest you can look deeper into other code.

Upvotes: 1

Related Questions