Thread: free() question

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    26

    Question free() question

    Hi all:

    I declared a structure in the .h file to implement a stack:

    typedef struct VIEW_MEMBER_TAG
    {
    char *ptrString;
    struct VIEW_MEMBER_TAG *ptrNextViewMember;
    }VIEW_MEMBER;

    In .c file:

    static VIEW_MEMBER* stackPtr; //It is the pointer that points to the top of the stack.


    Later I need to free up the stack so I wrote:

    Code:
      void freeStack(void)
    {
    	while (stackPtr != NULL)
    	{
    		free(stackPtr->ptrString);  //This line holds up the program.
    		stackPtr->ptrString = NULL;
    		stackPtr->ptrNextViewMember = stackPtr;
    	}
    }
    This line holds up the whole program.

    free(stackPtr->ptrString);


    Can anyone help me out?

    Cheers

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How are you creating the stack? Is ptrString dynamically allocated, or at least initialised to NULL?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    26
    Code:
    void push(char* string)
    {
    	size_t N = 0;
    	VIEW_MEMBER* newmem = NULL;
    
    	newmem = (VIEW_MEMBER*)malloc(sizeof(VIEW_MEMBER));    //Need to check for error here.
    
    	InitializeStack(newmem); 
    
    	N = strlen(string)+1;
    	newmem->ptrString = (char*)malloc(N);     //Need to check for error here.
    	strncpy(newmem->ptrString, string, N);
    	newmem->ptrString[N] = '\0';
    
    	if (stackPtr == NULL)
    	{
    		stackPtr = newmem;
    	}
    	else
    	{
    		newmem->ptrNextViewMember = stackPtr;
    		stackPtr = newmem;
    	}
    }
    
    void InitializeStack(VIEW_MEMBER *newMember)
    {
    	newMember->ptrString = NULL;
    	newMember->ptrNextViewMember = NULL;
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    stackPtr->ptrNextViewMember = stackPtr;
    Other way around, perhaps? I don't see how stackPtr ever changes in your loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design question
    By MacGyver in forum C Programming
    Replies: 9
    Last Post: 05-17-2007, 02:36 AM
  2. Simple question about free()
    By fanoliv in forum C Programming
    Replies: 7
    Last Post: 06-16-2006, 11:14 AM
  3. question about free()
    By TalosChen in forum C Programming
    Replies: 5
    Last Post: 05-20-2006, 06:11 PM
  4. SIGABRT upon free()
    By registering in forum C Programming
    Replies: 2
    Last Post: 07-19-2003, 07:52 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM