How to deallocate memory of a pointer to a character array

This is a discussion on How to deallocate memory of a pointer to a character array within the C Programming forums, part of the General Programming Boards category; Hi I have allocated memory to a pointer to a character array using malloc Code: tmp_argv_pipe[index] = (char *)malloc(sizeof(char) * ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    6

    How to deallocate memory of a pointer to a character array

    Hi I have allocated memory to a pointer to a character array using malloc

    Code:
    tmp_argv_pipe[index] = (char *)malloc(sizeof(char) * strlen(yytext) + 1);
    Later on in this array, I put NULL values to divide and use it later.

    Now I want to deallocate the memory specified to it.

    For a different array pointer, I use

    Code:
    void free_memory(char *tmp_argv[])
    {
        int counter;
        for(counter = 0; tmp_argv[counter] != NULL; counter++)
        {
            bzero(tmp_argv[counter], strlen(tmp_argv[counter])+1);
            tmp_argv[counter] = NULL;
            free(tmp_argv[counter]);
        }
    }
    and it works fine.

    I tried passing one more argument to the function above that gives the length of tmp_argv_pipe and then give the condition as

    Code:
    tmp_argv[counter] != NULL  || counter <= length
    It gives me segementation error when I do this.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    It should be " && counter <= length".
    Besides you free the memory first, then you assign NULL to your element, not the opposite, otherwise you always call free(0) which is legal but does nothing.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    6
    Sorry, I dont think I framed my question correctly.

    Suppose I have char *a[] = "a","b","c","d","e","\0"

    Now in my program I do this a[] = "a",NULL,"c",NULL,"e","\0"

    So that I can break one arrary charater pointer into 3(or more) and then access each by a, a+2 and so on.

    How do I deallocate such an array ??

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    ah, all right.
    Then only use "count <= length" as the exit condition of your loop and add an if(tmp_argv[counter] != NULL) statement to do the work.
    Code:
    for(...) if( ...!= NULL) { bzero(...); free(...); ...= NULL; }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deallocate an array of pointer
    By angeloulivieri in forum C Programming
    Replies: 5
    Last Post: 02-14-2012, 09:29 AM
  2. Deallocate Memory
    By Nuzut in forum C++ Programming
    Replies: 1
    Last Post: 10-05-2011, 10:06 AM
  3. Using free() to deallocate memory
    By Stiletto in forum C Programming
    Replies: 1
    Last Post: 04-24-2011, 07:47 AM
  4. Replies: 4
    Last Post: 11-01-2009, 05:19 AM
  5. Deallocate Memory and reuse pointer
    By appleGuy in forum C++ Programming
    Replies: 9
    Last Post: 06-20-2007, 11:07 AM

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