Thread: Why did the value of my void pointer change?

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    12

    Why did the value of my void pointer change?

    I have a the following in a header file.
    Code:
    struct SortedList
    {
    	void * data;		 
    	struct SortedList * next; 		
    	struct SortedList * previous;
    	int (*compareFunc)(void *, void *);
    	void (*destructor)(void *);
    };
    typedef struct SortedList* SortedListPtr;
    
    SortedListPtr SLCreate(CompareFuncT cf, DestructFuncT df);

    -------------------------------------------------------------------
    In a .c file, I implemented the SLCreate function.

    Code:
    SortedListPtr SLCreate(CompareFuncT cf, DestructFuncT df)
    {
    	struct SortedList item; 		
    	item.data = NULL;				
    	item.next = (struct SortedList *) malloc(sizeof(struct SortedList));
    	item.previous = (struct SortedList *) malloc(sizeof(struct SortedList));
    	item.compareFunc = cf;
    	item.destructor = df;
    	SortedListPtr ptr = (struct SortedList *) malloc(sizeof(struct SortedList));	
    	ptr = &item;
    	return ptr;
    };
    In main.c, I try to do the following:
    Code:
    SortedListPtr list = SLCreate(&compareInts, &destroy);
    
    ...... A bunch other code that does not alter list or it's contents at all. 
    
    
    
    struct SortedList item = (*list);
    void * data = item.data;
    
    if (data != NULL)	
    {
    	printf("Why did data become not null???\n");
    }
    -------------------------------------------------------------------
    So my problem is how come my variable data became not null anymore when I haven't altered it at all and how can I fix this??

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    'item' is a localized variable in function SLCreate. You can not expect its contents to remain after you exit the function as it is on the stack only. You are attempting to return its address - but contents are likely corrupted upon exit.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In SLCreate, item is a local variable. You returned a pointer to a local variable, and then attempted to use it. However, after returning from the function, the local variable no longer exists, hence you have undefined behaviour.

    To fix this, one option is to pass a pointer to an existing struct SortedList to the function instead.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting to pointer to pointer to void
    By Sharke in forum C Programming
    Replies: 13
    Last Post: 05-12-2009, 08:40 PM
  2. Replies: 4
    Last Post: 08-27-2007, 11:51 PM
  3. Dereference pointer to void pointer to member
    By phil in forum C Programming
    Replies: 5
    Last Post: 04-20-2005, 11:54 AM
  4. Replies: 6
    Last Post: 11-29-2004, 08:50 AM
  5. void pointer
    By frenchfry164 in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2003, 02:31 PM

Tags for this Thread