The few lines of code you provided are correct, but it is likely that the context in which they are executed cause the error. The difference between
unsigned char * pBuffer = (unsigned char *)malloc(ncnt);
unsigned char * pBuffer = (unsigned char *)malloc(ncnt);
To copy to clipboard, switch view to plain text mode
and
unsigned char pBuffer[ncnt];
unsigned char pBuffer[ncnt];
To copy to clipboard, switch view to plain text mode
is that in the first case, the array is allocated on the heap (and you must deallocate it yourself with free()), while in the second case it is allocated on the stack and automatically deallocated at the end of its scope. Your program probably accesses pBuffer outside its scope, after it has been deallocated, which causes an error at some point.
Bookmarks