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

Comment Utility
The argument to free is a pointer to a block previously allocated by malloc

Expert Comment

Comment Utility
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

Comment Utility
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.
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

LVL 51

Expert Comment

Comment Utility
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

Comment Utility
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

Comment Utility

   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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now