Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    How to free the void pointer

    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 *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
    Canada, Prince Edward Island
    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
    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. $spacer_open
  5. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    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