简体   繁体   中英

memcpy c++ - crashing

I have a very simple routine (at least should be simple)

I create a large char array and I am copying data to it as I receive it moving. After about the third iteration the app crashes.

first create a large buffer

_buffer = new char(7931880);

...

void writeData(char* newData,size_t size)
{
  memcpy(_buffer,newData,size); //this call succeeds the first 4 times then fails bytes received
 _buffer+=size;                 //(size) is never larger than 16000
}
_buffer = new char(7931880);

This is a pointer to a SINGLE character. To get an array use

_buffer = new char[7931880];

And turn on compiler warnings to detect the overflow.

You are dynamically allocating a single char with value 7931880 . Making an assumption that your buffer should be a little more than a single char (not much of a buffer), perhaps you were looking for _buffer = new char[7931880]; . This would allocate 7931880 char s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM