4

So, I got a weird assignment. I have to read a file content to array string. However, I have to initialize the array like this(I have to initialize it as array size 1):

char **input = (char **)malloc(1*sizeof(char*))

instead of

char **input = (char **)malloc((sizeOfFile+1)*sizeof(char*))

So, I have to keep using realloc. My question is, how can I realloc the inner array (the string) and how can I realloc the outher array (the array of string)

2 Answers 2

7

You don't have to reallocate the "inner arrays". The contents of the memory you allocate is the pointers, and when you reallocate input then you only reallocate the input pointer, not the contents of where input points to.


A crude ASCII-image to show how it works:

At first when you allocate a single entry in the input array, it looks like this:

         +----------+    +---------------------------+
input -> | input[0] | -> | What `input[0]` points to |
         +----------+    +---------------------------+

After you reallocate to make place for a second entry (i.e. input = realloc(input, 2 * sizeof(char*));)

         +----------+    +---------------------------+
input -> | input[0] | -> | What `input[0]` points to |
         +----------+    +---------------------------+
         | input[1] | -> | What `input[1]` points to |
         +----------+    +---------------------------+

The contents, i.e. input[0] is still the same as before the reallocation. The only thing that changes is the actual input pointer.

Sign up to request clarification or add additional context in comments.

5 Comments

So do you mean I can do something like this "input = (char**)realloc(input,previousSize+1)"
@NickStov for critical systems you may want to consider what happens to the original input pointer if that realloc() ever fails. However, for your specific purpose here, I doubt it matters much to you. Also, you need no malloc() for your initial input value, since realloc() will behave like malloc() if you pass it an initial NULL value, so just make sure input is NULL when this whole thing starts. (and +1 to this answer; me loves ascii art =P).
@whozcraig under what circumstances did realloc() fail? when there's not enough memory available?
@NickStov yes, at which point it returns NULL, and you just overwrote the only pointer to the previously valid allocation you had. Thus it is usually good practice to at least attempt to know it worked by storing the result in a temp, testing the temp, and only if it is non-null do you replace the original pointer (i.e. input = temp;). Hope that made sense.
@whozcraig okay. I will do something like that in my code :D thx
1

Your char** (i.e. pointer to pointer to char) is an array of pointers that point to some memory as well. So not only that you need to allocate memory for bunch of char* pointers, but you also need to allocate memory that each of these pointers will point to (the memory where some characters will be stored):

const int ARR_SIZE = 10;
const int STR_SIZE = 20;

char** strArr = malloc(ARR_SIZE * sizeof(char*));
for (int i = 0; i < ARR_SIZE; ++i)
    strArr[i] = malloc(STR_SIZE * sizeof(char));

strArr[9] = "Hello";

strArr = realloc(strArr, (ARR_SIZE + 5) * sizeof(char*));
for (int i = 0; i < 5; ++i)
    strArr[ARR_SIZE + i] = malloc(STR_SIZE * sizeof(char));

strArr[14] = "world!";

printf("%s %s", strArr[9], strArr[14]);

Full example is here. Hope this helps :)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.