Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Using free() in the middle of dynamically allocated space

Posted on 2002-03-16
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

610 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