realloc malloc and Heap problem.

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 11
Reputation: loushou is an unknown quantity at this point 
Solved Threads: 0
loushou loushou is offline Offline
Newbie Poster

realloc malloc and Heap problem.

 
0
  #1
Jan 25th, 2008
Here is my code:

  1. int LoadVerts(char *s_fName, OURCUSTOMVERTEX *p_Verts, short *p_Indices)
  2. {
  3. long i_CountVerts;
  4. long i_CountInds;
  5.  
  6. unsigned char buffer[sizeof(OURCUSTOMVERTEX)];
  7.  
  8. basic_ifstream<unsigned char> f_DataFile;
  9.  
  10. f_DataFile.open(s_fName, std::ios_base::in | std::ios_base::binary);
  11.  
  12. f_DataFile.read(buffer, sizeof(long));
  13. memcpy(&i_CountVerts, buffer, sizeof(long));
  14. f_DataFile.read(buffer, sizeof(long));
  15. memcpy(&i_CountInds, buffer, sizeof(long));
  16.  
  17. if ((p_Verts = (OURCUSTOMVERTEX *)realloc(p_Verts, sizeof(OURCUSTOMVERTEX) * i_CountVerts)) == NULL)
  18. exit( 1 );
  19. if ((p_Indices = (short *)realloc(p_Indices, sizeof(short) * i_CountInds)) == NULL)
  20. exit( 1 );
  21.  
  22. for (int temp = 0; temp < i_CountVerts; temp++)
  23. {
  24. f_DataFile.read(buffer, sizeof(OURCUSTOMVERTEX));
  25. memcpy(&p_Verts[temp], buffer, sizeof(OURCUSTOMVERTEX));
  26. }
  27.  
  28. for (int temp2 = 0; temp2 < i_CountInds; temp2++)
  29. {
  30. f_DataFile.read(buffer, sizeof(short));
  31. memcpy(&p_Indices[temp2], buffer, sizeof(short));
  32. }
  33. f_DataFile.close();
  34.  
  35. return 0;
  36. }

My issue is coming in at the following lines:

  1. if ((p_Verts = (OURCUSTOMVERTEX *)realloc(p_Verts, sizeof(OURCUSTOMVERTEX) * i_CountVerts)) == NULL)
  2. exit( 1 );
  3. if ((p_Indices = (short *)realloc(p_Indices, sizeof(short) * i_CountInds)) == NULL)
  4. exit( 1 );

This is the error I am getting after a successful build and the program is running:

  1. Windows has triggered a breakpoint in saveverts.exe.
  2.  
  3. 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.
  4.  
  5. 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: realloc malloc and Heap problem.

 
0
  #2
Jan 25th, 2008
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.
  1. void *temp = realloc ( p_Verts, sizeof(OURCUSTOMVERTEX) * i_CountVerts );
  2. if ( temp != NULL ) {
  3. // success, update the pointer
  4. p_Verts = temp;
  5. } else {
  6. // p_Verts is still pointing at the OLD memory. This needs to be saved
  7. // or free'd as appropriate
  8. }

The third problem is you're calling the function by value, not by reference.
  1. void func ( OURCUSTOMVERTEX *&p_Verts ) {
  2. // your code
  3.  
  4. void *temp = realloc ( p_Verts, sizeof(OURCUSTOMVERTEX) * i_CountVerts );
  5. if ( temp != NULL ) {
  6. // success, update the pointer
  7. p_Verts = temp;
  8. } else {
  9. // p_Verts is still pointing at the OLD memory. This needs to be saved
  10. // or free'd as appropriate
  11. }
  12.  
  13. // your code
  14. }
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 11
Reputation: loushou is an unknown quantity at this point 
Solved Threads: 0
loushou loushou is offline Offline
Newbie Poster

Re: realloc malloc and Heap problem.

 
0
  #3
Jan 27th, 2008
Originally Posted by Salem View Post
The first question is why are you using something as clunky as realloc in a C++ program?
You are obviously more versed than I, and i definitely thank you for your help. That being said what sort of alternative do you propose?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: realloc malloc and Heap problem.

 
0
  #4
Jan 27th, 2008
Look at std::vector and the push_back method.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC