Free nach Realloc führt zu Exception
-
Hallo,
hier der Code:
#include <iostream> using namespace std; #define MEMORY_SIZE sizeof(int) * 10 #define NEW_SIZE MEMORY_SIZE + (sizeof(int) * 5) int main( int argc, char** argv ) { void *mem_block = malloc( MEMORY_SIZE ); memset( mem_block, 0, MEMORY_SIZE ); int *int_ptr = reinterpret_cast<int*>(mem_block), *end_of_memory = reinterpret_cast<int*>(mem_block) + 10; while( int_ptr != end_of_memory ) { *int_ptr = 77; int_ptr++; } // set ptr back to begin int_ptr = reinterpret_cast<int*>(mem_block); while( int_ptr != end_of_memory ) { cout << *int_ptr << endl; int_ptr++; } cout << "\n\n" << endl; realloc( mem_block, NEW_SIZE ); //memset( (reinterpret_cast<unsigned char*>(mem_block) + MEMORY_SIZE ), 0, (NEW_SIZE - MEMORY_SIZE) ); end_of_memory = reinterpret_cast<int*>(mem_block) + 15; // set ptr back to begin int_ptr = reinterpret_cast<int*>(mem_block); while( int_ptr != end_of_memory ) { cout << *int_ptr << endl; int_ptr++; } //int_ptr = end_of_memory = NULL; free( mem_block ); return 0; }
Bei free bekomme ich jedesmal eine Exception. Warum?
-
mem_block = realloc( mem_block, NEW_SIZE );
-
David_pb schrieb:
mem_block = realloc( mem_block, NEW_SIZE );
... ohne Worte
Trotzdem Danke!
-
FrEEzE2046 schrieb:
Bei free bekomme ich jedesmal eine Exception.
welche exception?
FrEEzE2046 schrieb:
reinterpret_cast<int*>
was ist das? das wird nicht compilieren.
FrEEzE2046 schrieb:
cout << "\n\n" << endl;
- wo sind 'cout' und 'endl' definiert?
- einen string kann man nicht als shift-count benutzen
- es fehlt eine zuweisung o.ä. selbst wenn der code funktionieren würde, würde er nichts tun, meinst du vielleicht sowas:cout = cout << irgendwas << endl;
?
-
Das ist ja auch C++ Code, und da sollte man eigentlich kein malloc, realloc und free mehr benutzen (dafür gibt es extra die vector<>-Klasse)...
Der Code sieht mir auch sehr nach einem Test aus...