free() question

This is a discussion on free() question within the C Programming forums, part of the General Programming Boards category; Hi all: I declared a structure in the .h file to implement a stack: typedef struct VIEW_MEMBER_TAG { char *ptrString; ...

  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
    21,452
    How are you creating the stack? Is ptrString dynamically allocated, or at least initialised to NULL?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,185
    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, 02:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21