Find the answer to your Linux question:
Results 1 to 4 of 4
HI, I allocated void pointer using malloc. Please let me know how to free this memory? typedef struct_t node { int a; char b; void *c; void *d; }node_t; node_t ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2010
    Posts
    2

    How to free the void pointer


    HI,
    I allocated void pointer using malloc. Please let me know how to free this memory?

    typedef struct_t node {
    int a;
    char b;
    void *c;
    void *d;
    }node_t;

    node_t *dp;

    in my program, I assigned some head to the pointer. I am allocating like this.

    dp = phead.
    dp->c = (void*)malloc(10);
    dp->d = (void*)malloc(20);

    Now I have to free the memory. I used at last in the program like this.

    if (dp->c) free(dp->c)
    if (dp->d) free(dp->d)

    But when I executing it is getting crashed. Let me know the solution for this.

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    You should be able to free the allocated memory. Maybe its something else, did you try running your program in a debugger?
    Make mine Arch Linux

  3. #3
    Just Joined!
    Join Date
    Apr 2008
    Posts
    4
    dp = phead;
    dp->c = (void*)malloc(10);
    dp->d = (void*)malloc(20);

    Does this 'phead' pointer variable really have allocated memory?
    Check that! If it is incorrect or missing, then it is likely that you get this kind of behaviour.

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,392
    Most likely something in your program has "walked on" the pointers, resulting in corrupted memory, hence the crash. You need to run your program in the debugger and set a watch point on dp or phead to see when it is modified. Also, if something deallocated phead, or phead is a pointer to an automatic variable that went out of scope, then you will get the same results.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •