non-aligned pointer being freed??

Reply

Join Date: Aug 2009
Posts: 52
Reputation: nateuni has a little shameless behaviour in the past 
Solved Threads: 0
nateuni nateuni is offline Offline
Junior Poster in Training

non-aligned pointer being freed??

 
0
  #1
Sep 23rd, 2009
I am getting this error message, but I am not sure what I am doing wrong?

Here is a edited snip-it of my code -

  1. void WriteProgram(clientProgram *ptrToProgram, int *ptrTotalWkOuts, int *ptrNumOfEx)
  2. {
  3. int wkOut = 0;
  4.  
  5. clientWorkout *ptrToWorkout;
  6.  
  7. int *autoPopulate = (int *) calloc(1, sizeof(int));
  8.  
  9. clientWorkout *ptrToFirstWorkOut = (clientWorkout *)malloc(sizeof(clientWorkout));
  10.  
  11. free(ptrToFirstWorkOut);
  12. free(autoPopulate);
  13. ptrToFirstWorkOut = NULL;
  14. autoPopulate = NULL;
  15. }

When I stepped through on the debugger it did not come up, can anyone shed some light on what I am doing wrong?

Regards,
Nate
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 5
Reputation: chesspest is an unknown quantity at this point 
Solved Threads: 0
chesspest chesspest is offline Offline
Newbie Poster

Re: non-aligned pointer being freed??

 
0
  #2
Sep 24th, 2009
Originally Posted by nateuni View Post
I am getting this error message, but I am not sure what I am doing wrong?

Here is a edited snip-it of my code -

  1. void WriteProgram(clientProgram *ptrToProgram, int *ptrTotalWkOuts, int *ptrNumOfEx)
  2. {
  3. int wkOut = 0;
  4.  
  5. clientWorkout *ptrToWorkout;
  6.  
  7. int *autoPopulate = (int *) calloc(1, sizeof(int));
  8.  
  9. clientWorkout *ptrToFirstWorkOut = (clientWorkout *)malloc(sizeof(clientWorkout));
  10.  
  11. free(ptrToFirstWorkOut);
  12. free(autoPopulate);
  13. ptrToFirstWorkOut = NULL;
  14. autoPopulate = NULL;
  15. }

When I stepped through on the debugger it did not come up, can anyone shed some light on what I am doing wrong?

Regards,
Nate
I think calloc allocates memory that's already aligned to the specified datatype, whereas malloc allocates memory sufficient to accomodate the specified dataype. So a call to calloc with an integer pointer spec will cause allocation of an integer pointer datatype, but a call to malloc with the same spec will allocate 4 bytes. Alignment to datatype is left to the coder. In your code, freeing ptrToFirstWorkOut causes the error. Alignment can be done by something like this:
  1. unsigned char *aptr=(unsigned char *)ptrToFirstWorkOut;
  2. unsigned long align=(unsigned long)(aptr + sizeof(clientWorkout *)) % sizeof(clientWorkout);
  3. ptrToFirstWorkOut=(clientWorkout *)(aptr + sizeof(clientWorkout *) + sizeof(clientWorkout) - align);

or replacing the call to malloc with one to calloc .
Last edited by chesspest; Sep 24th, 2009 at 12:37 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 135
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: non-aligned pointer being freed??

 
0
  #3
Sep 24th, 2009
When I stepped through on the debugger it did not come up, can anyone shed some light on what I am doing wrong?
Memory corruption can happen and not show symptoms until much later. The code you posted is not broken in any way, so the problem is probably earlier in the execution of your program. But be sure that you are including stdlib.h. Casting malloc() or calloc() can hide warnings about a missing prototype.

I think calloc allocates memory that's already aligned to the specified datatype, whereas malloc allocates memory sufficient to accomodate the specified dataype.
malloc(), calloc(), and realloc() all return a pointer aligned for assignment to any object pointer.
Last edited by Tom Gunn; Sep 24th, 2009 at 9:48 am.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training

Re: non-aligned pointer being freed??

 
0
  #4
Sep 24th, 2009
Originally Posted by nateuni View Post
I am getting this error message, but I am not sure what I am doing wrong?

Here is a edited snip-it of my code -

  1. void WriteProgram(clientProgram *ptrToProgram, int *ptrTotalWkOuts, int *ptrNumOfEx)
  2. {
  3. int wkOut = 0;
  4.  
  5. clientWorkout *ptrToWorkout;
  6.  
  7. int *autoPopulate = (int *) calloc(1, sizeof(int));
  8.  
  9. clientWorkout *ptrToFirstWorkOut = (clientWorkout *)malloc(sizeof(clientWorkout));
  10.  
  11. free(ptrToFirstWorkOut);
  12. free(autoPopulate);
  13. ptrToFirstWorkOut = NULL;
  14. autoPopulate = NULL;
  15. }

When I stepped through on the debugger it did not come up, can anyone shed some light on what I am doing wrong?

Regards,
Nate
What is the error message? You state "this is the error message" without telling us the error message.

More info required.
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 52
Reputation: nateuni has a little shameless behaviour in the past 
Solved Threads: 0
nateuni nateuni is offline Offline
Junior Poster in Training

Re: non-aligned pointer being freed??

 
0
  #5
Sep 24th, 2009
Originally Posted by yellowSnow View Post
What is the error message? You state "this is the error message" without telling us the error message.

More info required.
Ok the exact error message is -

prog_name(35122) malloc :
*** error for object 0xbfffa878: non-aligned pointer being freed
*** set a breakpoint in malloc_error_break to debug
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training

Re: non-aligned pointer being freed??

 
0
  #6
Sep 24th, 2009
Originally Posted by nateuni View Post
Ok the exact error message is -

prog_name(35122) malloc :
*** error for object 0xbfffa878: non-aligned pointer being freed
*** set a breakpoint in malloc_error_break to debug
OK .. off the bat, that doesn't help that much. I would suggest that you take Tommy Gunn's advice and remove the casts to your malloc() and calloc() function calls.
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 52
Reputation: nateuni has a little shameless behaviour in the past 
Solved Threads: 0
nateuni nateuni is offline Offline
Junior Poster in Training

Re: non-aligned pointer being freed??

 
0
  #7
Sep 24th, 2009
I used some printf's to print out the where the pointers are pointing to memory.. but I can't find the exact address! I can find close to it.... eg 0xbfffa768 but not 0xbfffa878.

I also removed the casts and i am still getting the err msg.

I tried changing calloc to malloc and manually setting the memory that it is pointing to, to hold the value 0; Still the same error message.

Can i dynamically allocate (and deallocate) memory in sub functions, or does it all have to happen in main? As this is in a subfunction.. and the other uses of dynamic memory in main are not causing any issues?

Sorry if this is annoying, I am going beyond my current knowledge so I am a little stumped on how to fix this..
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 135
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: non-aligned pointer being freed??

 
0
  #8
Sep 24th, 2009
I also removed the casts and i am still getting the err msg.
Removing the casts would not fix the error, but if you forgot to include stdlib.h, it would probably give you a warning too. The problem with casting malloc() in C is that it can hide that kind of error. I did not expect it to be related to your current problem though.

Can i dynamically allocate (and deallocate) memory in sub functions, or does it all have to happen in main?
You can dynamically allocate anywhere you can call a function. malloc() is nothing more than another function, after all.

As this is in a subfunction.. and the other uses of dynamic memory in main are not causing any issues?
I think you have issues somewhere prior to this function. It is easy to corrupt your dynamic memory even if the pointer is not related to what you corrupted. Internally, malloc() can store all kinds of bookkeeping information that can easily be overwritten with a random buffer overflow.

If the problem is beyond your current knowledge and you cannot figure it out after say a day of troubleshooting, I would advise that you cut your losses and roll back to a point where the error did not occur and start over from there. If you are building your project incrementally and saving at regular intervals, rolling back should not be a big problem because there should not be too many changes between the last stable build and your current broken build.

If you are not working like that, this will help you to learn why it is a good idea.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 52
Reputation: nateuni has a little shameless behaviour in the past 
Solved Threads: 0
nateuni nateuni is offline Offline
Junior Poster in Training

Re: non-aligned pointer being freed??

 
0
  #9
Sep 24th, 2009
Originally Posted by Tom Gunn View Post
Removing the casts would not fix the error, but if you forgot to include stdlib.h, it would probably give you a warning too. The problem with casting malloc() in C is that it can hide that kind of error. I did not expect it to be related to your current problem though.


You can dynamically allocate anywhere you can call a function. malloc() is nothing more than another function, after all.


I think you have issues somewhere prior to this function. It is easy to corrupt your dynamic memory even if the pointer is not related to what you corrupted. Internally, malloc() can store all kinds of bookkeeping information that can easily be overwritten with a random buffer overflow.

If the problem is beyond your current knowledge and you cannot figure it out after say a day of troubleshooting, I would advise that you cut your losses and roll back to a point where the error did not occur and start over from there. If you are building your project incrementally and saving at regular intervals, rolling back should not be a big problem because there should not be too many changes between the last stable build and your current broken build.

If you are not working like that, this will help you to learn why it is a good idea.
Tommy... after all the good advice.. that is not what I am wanting to hear !

No surrender to the evil malloc! I need that dynamic allocation!

Ok I will keep playing!

Regards,
Nate
Last edited by nateuni; Sep 24th, 2009 at 1:35 pm. Reason: I have a thing for typing words out of order!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 135
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: non-aligned pointer being freed??

 
0
  #10
Sep 24th, 2009
Tommy... after all the good advice.. that is not what I am wanting to hear
That is how it goes sometimes. On the up side, I have never seen a rewrite worse than the code it replaced. Lessons learned before starting make for better software at the end.

No surrender to the evil malloc! I need that dynamic allocation!
You can still use malloc(), just roll back to before it broke and try again.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1109 | Replies: 13
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC