I have two dynamically allocated arrays. c
char **a = (char**)malloc(sizeof(char*) * 5));
char **b = (char**)malloc(sizeof(char*) * 5));
for (int i = 0; i
NO. Both are not same. In this case, strcpy(b[0], a[0])
is correct way to copy the string pointed by a[0]
to b[0]
.
In case of b[0] = a[0]
, memory allocated to b[0]
will lost and it will cause memory leak. Now freeing both of a[0]
and b[0]
will invoke undefined behavior. This is because both of them are pointing to same memory location and you are freeing same allocated memory twice.
NOTE: It should be noted that, as Matt McNabb pointed in his comment, memory leak does not invokes undefined behavior.