Using free() in the middle of dynamically allocated space

Posted on 2002-03-16
Last Modified: 2010-04-15
How does free() behave if you try to call it in the middle of the dynamically allocated space?

My situation is like this: I dynamically allocate a call stack (about 8k), and during the execution of my program the stack pointer gets moved around. Because of how the program works, I am unable to keep a reference pointer to the initial stack pointer. Once I am done with that call stack, I have to deallocate it. So I am doing something like this:

Now, I can guarantee that the stack pointer is SOMEWHERE inside the dynamically allocated space, but I have absolutely no idea WHERE. How will free() behave in this situation? Any advice? Like I said, I am unable to store the initial stack pointer inside the pd structure.

Roger Schmidt
Toronto, Ontario

P.S. The structures look like this:
typedef struct PD {
   struct PD* link;
   ProcessId pid;
   int priority;
   int waittime;
   struct LL* inlist;
} PrcDesc;

typedef struct Registers {
   unsigned pc;
   void *sp;
} Reg;
Question by:RogerSchmidt
LVL 84

Expert Comment

ID: 6872968
The argument to free is a pointer to a block previously allocated by malloc

Expert Comment

ID: 6873492
Not exactly sure what you are doing here ... if this is some sort of virtual machine/debugger that you are in control of then you might consider having base registers.  These would hold the base address of certain memory locations.

One such might be SBP - stack base pointer

The other alternative might be to have an Init function of some sort that fills in base pointers to important structures that you use into some dynamically allocated structure.  And a cleanup routine that frees them.

One other thing.  Depending on your implementation - if your stack consists of only one type of structure then the bottom of the stack should hold the base pointer of the entire stack.  So if you pop the stack empty - you should be able to free the memory location of the first stack entry.
LVL 12

Expert Comment

ID: 6873664
What free will do in the situation you're talking about is either crash instantly, cause severe heap corruption which will cause the program to crash at some later stage, or, if you're incredibly lucky, just fail to deallocate the block and thus cause a memory leak. You MUST pass the same pointer returned from malloc() to the free() function, or it won't work--noway, nohow.
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

LVL 51

Expert Comment

ID: 6874003
If you're talking about the pointer "link" from your example, you don't need to worry about if you move around the content of the pointer pointing to the "PrcDesc" struct.
If you mean that the pointer to the "PrcDesc" struct changes (means that it points to another data area), you must enshure that the pointer "link" is a valid pointer allocated with malloc() before, otherwise see pjknibbs comment.

Expert Comment

ID: 6886302
IIRC you've asked this question in comp.lang.c also.
The answer to your question is that free(p) works iff:

   - p == NULL or
   - p is the return value of malloc, calloc or realloc.

kind regards,


Expert Comment

ID: 7124060

   You have mentioned that your stack pointer (sp) moves at runtime.  Instead of moving 'sp' to point to the new location, can you use an index variable which tells how many locations to move.  For ex.  use (sp+index) and do all your pointer movements with respect to this 'index'.  Now you can have 'sp' pointing to the starting address and hence free(sp) would deallocate the valuable memory!


Accepted Solution

SpideyMod earned 0 total points
ID: 8279735
I am unlocking this question in preparation for cleanup.  I will return in 7 days to finalize this question.  Please leave any recommendations for the final state of this question, I will take all recommendations into consideration.  Failing any feedback, I may decide in 7 days to delete or PAQ this question with no refund.  Thanks.

Community Support Moderator @Experts Exchange

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question