Thursday, 15 May 2014

c++ - How can I detect heap corruption in my case -



c++ - How can I detect heap corruption in my case -

i want convert struct respect next approach:

convert struct array of char. convert table of char array of int. convert array of int array of char. convert array of char struct.

but have erreur when running:

this code:

void rserver::convertstruct_to_char ( pcryptdata p) { // ***********convert struct array of char ************ char* frame= new char[p.size]; cout << p.size <<endl; cout << endl; memcpy(frame, &p, sizeof(p)); //***********convert array of char array of int ************ int taille= p.size; int* out = new int[taille]; (int i=0; i<taille+1;i++) { out[i]=frame[i]; } delete [] frame; //***********convert array of int array of char ************ char* int2char = new char[taille]; (int i=0; i<taille+1;i++) { int2char[i]=out[i]; } //delete [] int2char; //***********convert array of char struct ************ pcryptdata t; //re-make struct memcpy(&t, int2char, sizeof(t)); }

can help me please find cause of problem when running.

this

int* out = new int[taille]; (int i=0; i<taille+1;i++) out[i]=...

is bad - in lastly iteration of loop assign value out[taille], out array allocated taille elements indexed 0 trough taille-1. result write past allocated block , overwrite other block of heap, corrupting it.

should be:

int* out = new int[taille]; (int i=0; i<taille;i++) out[i]=...

same other for loops.

c++ visual-c++ heap heap-memory

No comments:

Post a Comment