0

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".

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
E_learner
  • 3,512
  • 14
  • 57
  • 88
  • Can you cut-n-paste your code? The line `data = newData;` should not even compile, much less get you to a runtime problem. – Michael Burr Sep 28 '12 at 07:50
  • 1
    You're assigning the same value 480000 times in the line `data = newData` in `mycode::setData` function. If both buffers are from the same size you can use `memcpy`. Take a look at [this answer](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c/4810668) to know more about arrays in c++. – PaperBirdMaster Sep 28 '12 at 07:51
  • sorry, my mistake, I edited into "data[i][j]=[i][j]". – E_learner Sep 28 '12 at 07:55
  • To start with, have you tried running your program in a debugger? Also, shouldn't it be `data[m][n] = newData[m][n];` in your loop? And don't you think using something like [`std::copy`](http://en.cppreference.com/w/cpp/algorithm/copy) or even [`memcpy`](http://en.cppreference.com/w/cpp/string/byte/memcpy) would be more effective? – Some programmer dude Sep 28 '12 at 07:55
  • yes, I tried running in a debugger, but there was no error message, but the program just stops responding. – E_learner Sep 28 '12 at 07:59

2 Answers2

4

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.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
1

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.

FrostNovaZzz
  • 802
  • 3
  • 10
  • 22