-1

I am trying to reduce the size of a char ** double pointer using realloc. First the char ** pointer is allocated using malloc and then the individual char* pointers as given below.

    char **first= (char**)malloc(num*sizeof(char*)); 
    char **last=(char**)malloc(num*sizeof(char*));

    for(int i=0;i<num;i++)
    {
    //each individual pointer holds 20 characters
            first[i]=(char*)malloc(20*sizeof(char)); 
            last[i]=(char*)malloc(20*sizeof(char));

    }

Now after some operations, I want to reduce the size of first and last by one counter value where counter is less than num.
Purpose is to free last counter number of records.

If I do the following

//Freeing up internal pointer memories
        i = 0;
        while (i<counter)
        {
            free(first[num1-1 - i]);
            free(last[num1-1-i]);
            i++;
         }

    //Reducing the size of the double pointer using realloc
      first = (char **)realloc(first, (num1-counter) * sizeof(char *));
      last = (char **)realloc(last, (num1-counter) * sizeof(char *) );

After this if I print the records

    for(i=0; i<num - counter; i++){
         printf("First Name: %s ",first[i]);
         printf("Last Name: %s ",last[i]);
    }`

The last record seems to be corrupted.

If I just perform the following and do not free the individual char * pointers, it works fine.

first = (char **)realloc(first, (num1-counter) * sizeof(char *));
last = (char **)realloc(last, (num1-counter) * sizeof(char *) );

My question is does realloc of a double pointer to reduce the size, internally frees up the internal pointers and as I am doing both, it is some how corrupting the memory?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 3
    You are using the right technique. It sounds like there is a bug in your code elsewhere. Please post a [MCVE](http://stackoverflow.com/help/mcve) – M.M Apr 20 '17 at 01:37
  • 1
    [don't cast malloc in C](http://stackoverflow.com/q/605845/995714) – phuclv Apr 20 '17 at 01:38
  • Allocations done with `num`, `free()` done with `num1`. Re-allocations done with `num1`. Printing done with `num` --> Hmmm? – chux - Reinstate Monica Apr 20 '17 at 01:57
  • 3
    "My question is does `realloc` of a double pointer to reduce the size, internally frees up the internal pointers" --> No. `realloc()` does not know there are pointers. Problem is in un-posted code. – chux - Reinstate Monica Apr 20 '17 at 02:03
  • 3
    You don't reduce the size of the pointer, but what it points to! And **always** check the result of `realloc`, `malloc` & friends for errors! For `realloc` you must not overwrite the original pointer before you ensured the call succeeded! – too honest for this site Apr 20 '17 at 02:06
  • But you can reduce the number of allocated pointers by passing the exact **count** you need and resizing to that number using `realloc`. If count is less than the currently allocated number of pointers, space for the excess pointers will be freed (released) from the current allocation. – David C. Rankin Apr 20 '17 at 04:14

1 Answers1

1

To answer your question: No, it doesn't.

Freeing the nested pointers in a char ** might be rude if the pointer is still in use somewhere else.

The C compiler doesn't follow memory pointers around, nor does it count object references, so it's up to you to free (or keep) the allocated memory of the nested strings within the char **.

As for the example code itself, it seems to have issues, but these were described in the comments to your question.

Myst
  • 18,516
  • 2
  • 45
  • 67