| | |
non-aligned pointer being freed??
![]() |
•
•
Join Date: Aug 2009
Posts: 52
Reputation:
Solved Threads: 0
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 -
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
Here is a edited snip-it of my code -
c Syntax (Toggle Plain Text)
void WriteProgram(clientProgram *ptrToProgram, int *ptrTotalWkOuts, int *ptrNumOfEx) { int wkOut = 0; clientWorkout *ptrToWorkout; int *autoPopulate = (int *) calloc(1, sizeof(int)); clientWorkout *ptrToFirstWorkOut = (clientWorkout *)malloc(sizeof(clientWorkout)); free(ptrToFirstWorkOut); free(autoPopulate); ptrToFirstWorkOut = NULL; autoPopulate = NULL; }
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
•
•
Join Date: Sep 2009
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
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 -
c Syntax (Toggle Plain Text)
void WriteProgram(clientProgram *ptrToProgram, int *ptrTotalWkOuts, int *ptrNumOfEx) { int wkOut = 0; clientWorkout *ptrToWorkout; int *autoPopulate = (int *) calloc(1, sizeof(int)); clientWorkout *ptrToFirstWorkOut = (clientWorkout *)malloc(sizeof(clientWorkout)); free(ptrToFirstWorkOut); free(autoPopulate); ptrToFirstWorkOut = NULL; autoPopulate = NULL; }
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
C Syntax (Toggle Plain Text)
unsigned char *aptr=(unsigned char *)ptrToFirstWorkOut; unsigned long align=(unsigned long)(aptr + sizeof(clientWorkout *)) % sizeof(clientWorkout); 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.
•
•
•
•
When I stepped through on the debugger it did not come up, can anyone shed some light on what I am doing wrong?
•
•
•
•
I think calloc allocates memory that's already aligned to the specified datatype, whereas malloc allocates memory sufficient to accomodate the specified dataype.
Last edited by Tom Gunn; Sep 24th, 2009 at 9:48 am.
-Tommy (For Great Justice!) Gunn
•
•
•
•
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 -
c Syntax (Toggle Plain Text)
void WriteProgram(clientProgram *ptrToProgram, int *ptrTotalWkOuts, int *ptrNumOfEx) { int wkOut = 0; clientWorkout *ptrToWorkout; int *autoPopulate = (int *) calloc(1, sizeof(int)); clientWorkout *ptrToFirstWorkOut = (clientWorkout *)malloc(sizeof(clientWorkout)); free(ptrToFirstWorkOut); free(autoPopulate); ptrToFirstWorkOut = NULL; autoPopulate = NULL; }
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
More info required.
Manic twiddler of bits
•
•
Join Date: Aug 2009
Posts: 52
Reputation:
Solved Threads: 0
•
•
•
•
What is the error message? You state "this is the error message" without telling us the error message.
More info required.
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
•
•
Join Date: Aug 2009
Posts: 52
Reputation:
Solved Threads: 0
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..
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..
•
•
•
•
I also removed the casts and i am still getting the err msg.
•
•
•
•
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?
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
•
•
Join Date: Aug 2009
Posts: 52
Reputation:
Solved Threads: 0
•
•
•
•
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.
!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!
•
•
•
•
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!
-Tommy (For Great Justice!) Gunn
![]() |
Similar Threads
- Memory freed at function return? (C)
- Virtual destructor Problem (C++)
- does allocated space dissepear? (C)
- Chess Program (C++)
- Overloading assignment operator (C++)
- How to free up memory used for queues? (C)
Other Threads in the C Forum
- Previous Thread: Balanced Tree
- Next Thread: Problem with pointers
Views: 1109 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for C
#include api array arrays binary binarysearch bit build c++ c/c++ calling char character code coke command conversion convert database decimal directory dude dynamic error exec executable factorial fflush fgets file floatingpointvalidation fork function functions getline givemetehcodez grade graphics haiku help|help|help|help homework i/o include input insert int integer intmain() keyboard lazy line linked linkedlist linux list lists loop malloc matrix memory mysql no-effort output overwrite parallel path permutations pointer pointers problem process program programming read readfile recursion recursive recv reverse scanf segmentationfault socketprograming sockets spoonfeeding stdin string strings strtok structures student system testing turbo-c turboc unix user variable win32 windows _getdelim





