Search code examples
arraysmfcstack-overflowmemcpy

Stack Overflow Error when using memcpy to copy 1d array to 2d array


In one of the button clicked function of my MFC project, I got the following code which try to copy an array of data to another array:

Char dest[2500][50];
double totalNum = CountNum*2;
for (int n=0; n< totalNum; n++){
    memcpy(dest[n], readingdata+n*14, 13);
    dest[n][13]=0;
} 

If I run this code once or twice, there is nothing wrong. But when I try to run this code several time more, the program gives me a exception error: Stack Overflow. And the error occurs in chkstk.asm file, it is in line 99:

test dword ptr [eax],eax ; probe page

Can Anyone help me out and explain how does this cause the stack overflow problem?

Thanks in advance!


Solution

  • Try use dynamic array here

    double totalNum = CountNum*2;    
    ViChar (*dest)[50] = new ViChar[totalNum][50];
    for (int n=0; n< totalNum; n++){
        memcpy(dest[n], readingdata+n*14, 13);
        dest[n][13]=0;
    }
    
    delete [] dest;