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 -

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

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 -

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

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:

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 :) .

Edited 7 Years Ago by chesspest: n/a

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.

Edited 7 Years Ago by Tom Gunn: n/a

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 -

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

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

More info required.

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

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.

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.

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. ;)

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

Edited 7 Years Ago by nateuni: 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

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.

How is clientWorkout defined? I've come across this error before and in each case it involved a structure.

My friend and I chatted and we narrowed it down to this

ptrToFirstWorkOut = &(*ptrToProgram).week[0].workout[0];

I am reassigning the address of the action pointer (as opposed to the address that it contains).

If I change it to -

*ptrToFirstWorkOut = ptrToProgram->week[0].workout[0];

I no longer get the error message.. but I then need to go through and sort out all the mess that goes with making the change. I will be doing a lot of printf's tomorrow of pointer addresses, the address they point to and the value they point to. But it will be good for my understanding.

A bit pointer detective work. No wonder lectures say that pointers do new programmers heads in. I thought I had them sorted but.. seems I was wrong.. getting a good old compile doesn't mean I am out of the woods yet! Actually it is the combination of the structs and the pointers which is pushing new territory.

Ok thanks for your help guys.. I will keep you posted with the outcome.

OK here is the deal

I was originally changing the address of a dynamically allocated pointer, so malloc was freaking out. So That said...

How what do I have to do to get this bit of code to work?

*ptrToFirstWorkOut = ptrToProgram->week[0].workout[0];

Currently it is making the pointer point to the value of ptrToProgram->week[0].workout[0] !!! I don't want that ,I want its reference.. where it is in memory?

So to clarify, I am trying to make my dynamicaly allocated ptrToFirstWorkOut, point to a reference of ptrToProgram->week[0].workout[0]. I have tried all types of & * and () combos and I am bunging up the syntax? Anyone?

Nate

Edited 7 Years Ago by nateuni: n/a

I was originally changing the address of a dynamically allocated pointer, so malloc was freaking out.

That will do it. Even if you change the value of the pointer, you must change it back to the address malloc() returned or free() will not know how to free it.

I don't want that ,I want its reference.. where it is in memory?

There are two good options for the first element:

ptrToFirstWorkOut = &ptrToProgram->week[0].workout[0];

The & operator binds to the workout, because ->, ., and [] have higher precedence. This is how you get the address of an indexed element in the array, but if you only want to first element, you can do this:

ptrToFirstWorkOut = ptrToProgram->week[0].workout;

An array name is converted to a pointer to the first element when used as a value, so you do not need to index the workout array or use the & operator.

I am trying to make my dynamicaly allocated ptrToFirstWorkOut, point to a reference of ptrToProgram->week[0].workout[0].

Without seeing your current code, I cannot say for sure, but this sounds like a memory leak. If you dynamically allocate memory, then repoint the pointer to another address, you can lose your only reference to the dynamically allocated memory.

This article has been dead for over six months. Start a new discussion instead.