1

The static keyword keeps the pointer alive until the program terminates, but is the memory allocated to the pointer buffer free'd automatically when the process terminates? or does the programmer have to do it?


In the following example, we do not know how many times the function will be called and we can only free the allocated memory if we haven't read anything on the current call of the function. Otherwise the memory cannot be free'd since we will need to use the bytes that we just read in the next call.


Function:

char *readStdin(void) {
    static char *buffer = NULL;
    ssize_t ret;

    if (buffer != NULL) {
        // DO SOMETHING WITH PREVIOUSLY READ BYTES
    }
    /* Allocate and NULL-terminate a Buffer */
    buffer = malloc(BUFSIZ);
    if (buffer == NULL)
        return (NULL);
    buffer[BUFSIZ] = '\0';

    /* Read from Standard Input at most 'BUFSIZ' characters*/
    ret = read(0, buffer, BUFSIZ);

    /* Free Buffer if no Input Read */
    if (ret <= 0) {
        free(buffer);
        buffer = NULL;
    }

    /* Return the read Input */
    return (buffer);
}

Program:

int main(void) {
    /* Print the Read Bytes */
    printf("%s", readStdin());
    printf("%s", readStdin());
    printf("%s", readStdin());
    ...
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
AymenTM
  • 529
  • 2
  • 5
  • 17
  • Depends on the OS. But on sane systems it is. – Eugene Sh. Mar 15 '19 at 16:34
  • 4
    Yes. All of the memory used by a process goes away and is reclaimed by the OS when the process terminates. The `static` declaration has nothing to do with this. (Yes, I know there are other ways to allocate persistent system memory areas, but this is not the way that is done.) – David R Tribble Mar 15 '19 at 16:39
  • @DavidRTribble R Tribble... hmm then how is it done ? – AymenTM Mar 15 '19 at 16:42
  • 1
    http://man7.org/linux/man-pages/man3/shm_open.3.html, http://man7.org/linux/man-pages/man2/shmget.2.html – melpomene Mar 15 '19 at 16:48
  • Note that static is the opposite of dynamic. – machine_1 Mar 15 '19 at 17:02
  • 1
    This will result in a memory leak. https://en.wikipedia.org/wiki/C_dynamic_memory_allocation (In your programme, the OS is free to deallocate.) – Neil Mar 15 '19 at 17:03
  • Viz, it will result in a `static` pointer to memory on the heap. – Neil Mar 15 '19 at 17:10
  • @NeilEdelman, how then does one deal with this issue ? Or is declaring a static buffer (i.e `char buffer[BUFSIZ]`) the only way of doing it ? – AymenTM Mar 15 '19 at 17:12
  • Exactly. Specifically, static memory is fixed and cannot change size. – Neil Mar 15 '19 at 18:01
  • See https://stackoverflow.com/questions/408670/stack-static-and-heap-in-c and https://craftofcoding.wordpress.com/2015/12/07/memory-in-c-the-stack-the-heap-and-static/. – Neil Mar 15 '19 at 18:09
  • Possible duplicate of [Should you free at the end of a C program](https://stackoverflow.com/questions/48430891/should-you-free-at-the-end-of-a-c-program) – Nikolas Stevenson-Molnar Mar 15 '19 at 18:26
  • 1
    You could register an `atexit()` handler that frees the buffer, but personally I wouldn't bother. The OS will clean up your process (and all of its memory) anyway. – melpomene Mar 15 '19 at 20:03
  • regarding: `buffer[BUFSIZ] = '\0';` in C, the index to a buffer is in the range 0...(number of elements in buffer -1) So `buffer[BUFSIZ] is modifying memory beyond the end of the array `buffer[]` The result is undefined behavior ( and can lead to a seg fault event ) – user3629249 Mar 17 '19 at 07:15
  • regarding: `ret = read(0, buffer, BUFSIZ);` The function: `read()` does NOT NUL terminate the data read. Therefore, the calls to `printf()` in the `main()` function will result in undefined behavior because the '%s' format specifier will keep outputting characters until a NUL char is encountered. So it is unknown when/where that NUL character might be encountered. – user3629249 Mar 17 '19 at 07:20
  • after the code block beginning with: `if (ret <= 0) {` there needs to be the statement: `buffer[ ret ] = '\0';` – user3629249 Mar 17 '19 at 07:22
  • it is considered very poor programming practice to leave loose/sloppy code that does not cleanup after itself. – user3629249 Mar 17 '19 at 07:23

1 Answers1

3

is the memory allocated to the pointer buffer free'd automatically when the process terminates ?

It depends. On most modern desktops and servers, yes. On some older systems, or some modern oddballs, not necessarily. Best practice is to always clean up after yourself regardless of what the OS does when your program exits.

Exactly how dynamic memory is managed, and what happens to it after your program exits, is not specified by the language standard - that's entirely a function of the platform on which your program is running.

As a rule, you should save all your dynamic memory pointers somewhere so you can perform some cleanup as necessary. Also, always check the result of malloc, calloc, or realloc. Do not blindly assume that they always succeed.

John Bode
  • 119,563
  • 19
  • 122
  • 198