1
#include <stdio.h>
#include <stdlib.h>

char **Names;
int size = 2;       //Minimum size for 2D array

void main() {

    int i;
    Names = (char **)malloc(size * sizeof(char *));      // First initaliaion  of 2D array in heap
    for (i = 0; i < size; i++)
        Names[i] = (char *)malloc(20 * sizeof(char));

    printf("\nenter");
    for (i = 0; i < size; i++)
        scanf("%s", Names[i]);

    while (1) {
        size++;
        Names = (char **)realloc(Names, size * sizeof(char *));       //Dynamic allocation of 2D aray
        for (i = 0; i < size; i++)
            Names[i] = (char *)realloc(Names[i], 20 * sizeof(char));

        i = size - 1;
        printf("\nenter");
        scanf("%s", Names[i]);

        for (i = 0; i < size; i++)
            printf("\n%s", Names[i]);
    }
}

It doesn't crash immediately it depends on the "size" I initialized. It crashes after 5 allocations for me. I tried adding free function but it did not seem to help.

6
  • 1
    Change scanf("%s" to scanf("%19s" Commented Feb 16, 2022 at 19:33
  • 1
    you should check if realloc succeeded Commented Feb 16, 2022 at 19:33
  • There's no need to reallocate all the Names[i], since the new size is the same as the old one. Commented Feb 16, 2022 at 19:34
  • 1
    calling realloc on an uninitialized pointer is undefined Commented Feb 16, 2022 at 19:36
  • As an aside, two points. First, void main() is non-portable; unless you are running the program on a platform which cannot accept a return value for a program (e.g., some embedded systems), you should always use int main() and end the function with return 0 or return <error-code>. Second, casting the return values of malloc()/realloc() is not necessary in C, and considered non-idiomatic. Commented Feb 16, 2022 at 19:40

1 Answer 1

3

After this memory reallocation

   size ++;
   Names= (char**)realloc(Names,size*sizeof(char *)); 

the last pointer pf the array of pointers has an indeterminate value because it was not initialized.

As a result the call of realloc for the last pointer in this loop

    for (i=0; i<size; i++)
        Names[i] = (char*)realloc(Names[i],20*sizeof(char));

invokes undefined behavior.

Before executing the loop you need to initialize the last pointer at least like

Names[size-1] = NULL;

In fact there is no sense to use the for loop because what you need is to allocate memory for the newly added pointer. The allocated memory pointed to by the early created pointers is not being changed in size.

So instead of

    for (i=0; i<size; i++)
        Names[i] = (char*)realloc(Names[i],20*sizeof(char));

you could just write

Names[size-1] = malloc( 20 * sizeof( char ) );

Pay attention to as you have an infinite loop then the memory reallocation sooner or later can fail.

Also according to the C Standard the function main without parameters shall be declared like

int main( void )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.