| | |
realloc malloc and Heap problem.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 11
Reputation:
Solved Threads: 0
Here is my code:
My issue is coming in at the following lines:
This is the error I am getting after a successful build and the program is running:
After a bit of lengthy chasing.... i have turned up empty handed with my focus on a function in istream called "_Read_s", which I am confident is not the problem. SOOOO.... I am looking for Help, advice, or Insight.
SHORT DESC: This function should receive 2 arrays of different types. During the meat of the code it should load a file, read from the file the first two long values to get the lengths of each array. it should reset the arrays to the new lengths to allow for input of the values from the files. After the values are filled in the newly sized arrays, these new arrays and new values should be accessible from the calling procedure, in this case WinMain().
Thanks in advance.
C++ Syntax (Toggle Plain Text)
int LoadVerts(char *s_fName, OURCUSTOMVERTEX *p_Verts, short *p_Indices) { long i_CountVerts; long i_CountInds; unsigned char buffer[sizeof(OURCUSTOMVERTEX)]; basic_ifstream<unsigned char> f_DataFile; f_DataFile.open(s_fName, std::ios_base::in | std::ios_base::binary); f_DataFile.read(buffer, sizeof(long)); memcpy(&i_CountVerts, buffer, sizeof(long)); f_DataFile.read(buffer, sizeof(long)); memcpy(&i_CountInds, buffer, sizeof(long)); if ((p_Verts = (OURCUSTOMVERTEX *)realloc(p_Verts, sizeof(OURCUSTOMVERTEX) * i_CountVerts)) == NULL) exit( 1 ); if ((p_Indices = (short *)realloc(p_Indices, sizeof(short) * i_CountInds)) == NULL) exit( 1 ); for (int temp = 0; temp < i_CountVerts; temp++) { f_DataFile.read(buffer, sizeof(OURCUSTOMVERTEX)); memcpy(&p_Verts[temp], buffer, sizeof(OURCUSTOMVERTEX)); } for (int temp2 = 0; temp2 < i_CountInds; temp2++) { f_DataFile.read(buffer, sizeof(short)); memcpy(&p_Indices[temp2], buffer, sizeof(short)); } f_DataFile.close(); return 0; }
My issue is coming in at the following lines:
C++ Syntax (Toggle Plain Text)
if ((p_Verts = (OURCUSTOMVERTEX *)realloc(p_Verts, sizeof(OURCUSTOMVERTEX) * i_CountVerts)) == NULL) exit( 1 ); if ((p_Indices = (short *)realloc(p_Indices, sizeof(short) * i_CountInds)) == NULL) exit( 1 );
This is the error I am getting after a successful build and the program is running:
C++ Syntax (Toggle Plain Text)
Windows has triggered a breakpoint in saveverts.exe. This may be due to a corruption of the heap, and indicates a bug in saveverts.exe or any of the DLLs it has loaded. The output window may have more diagnostic information
After a bit of lengthy chasing.... i have turned up empty handed with my focus on a function in istream called "_Read_s", which I am confident is not the problem. SOOOO.... I am looking for Help, advice, or Insight.
SHORT DESC: This function should receive 2 arrays of different types. During the meat of the code it should load a file, read from the file the first two long values to get the lengths of each array. it should reset the arrays to the new lengths to allow for input of the values from the files. After the values are filled in the newly sized arrays, these new arrays and new values should be accessible from the calling procedure, in this case WinMain().
Thanks in advance.
The first question is why are you using something as clunky as realloc in a C++ program?
The second problem is you're using realloc in an unsafe manner.
You need to do this.
The third problem is you're calling the function by value, not by reference.
Thus, if the allocated memory really does move when realloc is called, the actual pointer to that memory (from the caller's point of view) will also change.
In your code, this change was NOT reflected in the caller, and it probably ended up using an invalid pointer as a result.
The second problem is you're using realloc in an unsafe manner.
You need to do this.
C++ Syntax (Toggle Plain Text)
void *temp = realloc ( p_Verts, sizeof(OURCUSTOMVERTEX) * i_CountVerts ); if ( temp != NULL ) { // success, update the pointer p_Verts = temp; } else { // p_Verts is still pointing at the OLD memory. This needs to be saved // or free'd as appropriate }
The third problem is you're calling the function by value, not by reference.
C++ Syntax (Toggle Plain Text)
void func ( OURCUSTOMVERTEX *&p_Verts ) { // your code void *temp = realloc ( p_Verts, sizeof(OURCUSTOMVERTEX) * i_CountVerts ); if ( temp != NULL ) { // success, update the pointer p_Verts = temp; } else { // p_Verts is still pointing at the OLD memory. This needs to be saved // or free'd as appropriate } // your code }
In your code, this change was NOT reflected in the caller, and it probably ended up using an invalid pointer as a result.
![]() |
Other Threads in the C++ Forum
- Previous Thread: c-string
- Next Thread: Reassigning existing object with ios::app ?
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






