Hi, I realize that such a problem has already been discussed many times out there as I have found many threads with such a title but even after looking into 10 different threads I still dont know why this is happening to me (but maybe I'm just missing some basic understanding?).
here's the funtcion where the error shows up:
1 2 3 4 5 6 7 8 9 10
|
void Packets::SetVar( char* varname, int varvalue )
{
int size = (int)ceil((float)(varvalue/10))+1;
char* buffer = new char[size];
sprintf(buffer, "%i", varvalue);
SetVar(varname, buffer);
delete[] buffer;
}
|
when the function is called, size contains 1 and varvalue contains 2.
when he tries to delete buffer, the program crashes with the above mentioned error. here it is more detailed:
HEAP CORRUPTION DETECTED: after Normal block (#248) at 0x02887598.
CRT detected that the application wrote to memory after end of heap buffer.
(Press Retry to debug the application)
also here is buffer before sprintf is called:
- buffer 0x02437598 "Íýýýý««««««««þîþ"
51 'Í' char
and after sprintf:
- buffer 0x02437598 "2"
50 '2' char
he has no problem with deleting buffer when I skip the sprintf call so I guess it has something to do with that?
and also I suppose I have to use here delete[] rather than delete?