Thread: how to copy a generic linked list node structure

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    how to copy a generic linked list node structure

    Hi i am having problems copying this data structure (memory leaks), so I was wondering if anyone could help me with it.

    Code:
    typedef struct List {
    	int x; 
    	void *ptr;             
    	struct List *next;  
    } List;
    The problem is allocating memory for void *ptr.

    thnx

    baxy

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    If you've allocated memory using ptr, then you copy another structure that copies over ptr, you just lost the pointer to the memory that was addressed by ptr.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    What have you done so far? Could you post the copy code you've written, and explain how it doesn't work?

    What is ptr? Is it a void* because it can point to many different things? If so, how do you determine what the current ptr points to?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-25-2013, 01:50 AM
  2. Replies: 7
    Last Post: 11-04-2010, 01:18 PM
  3. Replies: 0
    Last Post: 09-16-2008, 05:04 AM
  4. traversing a linked list with a node and list class
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 11:57 AM
  5. Replies: 5
    Last Post: 10-04-2001, 03:42 PM