1

Say, in main(); you read a string from a file, and scan it into a statically declared char array. You then create a dynamically allocated char array with with length strlen(string).

Ex:

FILE *ifp;
char array_static[buffersize];
char *array;

fscanf(ifp, "%s", array_static);
array = malloc(sizeof(char) * strlen(array_static) + 1);
strcpy(array_static, array);

Is there anything we can do with the statically allocated array after copying it into the dynamically allocated array, or will it just be left to rot away in memory? If this is the case, should you even go through the trouble of creating an array with malloc?

This is just a hypothetical question, but what is the best solution here with memory optimization in mind?

8
  • Once array_static goes out of scope it disappears so it doesn't "rot away". Do you know about "stack" and "heap" memory ? Commented Jun 17, 2021 at 1:23
  • In the code example you have shown, you are simply duplicating data from one location into another. The original memory will still be 'in use' until the function or block in which it is declared terminates (goes out of scope). Also note that you have forgotten to allocate memory for the terminating nul character (add 1 to the return value of the strlen call). Also, look at the strdup function. Commented Jun 17, 2021 at 1:25
  • @John3136 what do you mean out of scope? Say you do this inside main();, when would array_static go out of scope? Commented Jun 17, 2021 at 1:25
  • scope is one of the most basic programming concepts. You need to learn about it - there are plenty of books/online tutorials. Once you learn a bit more the answer to your question will become obvious ! If array_static is in main then it never goes out of scope. If it was in a function etc then it would. Commented Jun 17, 2021 at 1:29
  • 1
    sizeof(char) * strlen(array_static) + 1 is conceptually wrong, should be sizeof(char) * (strlen(array_static) + 1). Yet since sizeof(char) == 1, it has the same result. Commented Jun 17, 2021 at 1:59

1 Answer 1

2

Here's how to make your life easier:

/* Returns a word (delimited with whitespace) into a dynamically
 * allocated string, which is returned. Caller is responsible
 * for freeing the returned string when it is no longer needed.
 * On EOF or a read error, returns NULL.
 */
char* read_a_word(FILE* ifp) {
  char* word;
    /* Note the m. It's explained below. */
  if (fscanf(ifp, "%ms", &word) != 1)
    return NULL;
  return word;
}

The m qualifier in the scanf format means:

  • An optional 'm' character. This is used with string conversions (%s, %c, %[), and relieves the caller of the need to allocate a corresponding buffer to hold the input: instead, scanf() allocates a buffer of sufficient size, and assigns the address of this buffer to the corresponding pointer argument, which should be a pointer to a char * variable (this variable does not need to be initialized before the call). The caller should subsequently free(3) this buffer when it is no longer required.

It's a Posix extension to the standard C library and is therefore required by any implementation which hopes to be Posix compatible, such as Linux, FreeBSD, or MacOS (but, unfortunately, not Windows). So as long as you're using one of those platforms, it's good.

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

1 Comment

Wow that's amazing actually. Did not know that at all

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.