3

I wonder, if I have the following code:

var = (double *)calloc(vars_ptr->amount, sizeof(double));
if (var == NULL) {
     printf("ERROR: Problem in memory allocation");
     exit(1);
}

Is it possible there will be a memory leak? I mean exiting without freeing, since it indicates the memory allocation process didn't take place.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Guy
  • 145
  • 3
  • 12

6 Answers6

2

If no memory was allocated, you don't have to free anything.

That said, free(NULL) isn't harmful. It just doesn't do anything.

That said, there is no point in freeing memory just before you're exiting anyway. That's not a memory leak; a leak would be not freeing memory and keeping running.

melpomene
  • 84,125
  • 8
  • 85
  • 148
1

No, this

var = (double*)calloc(vars_ptr->amount, sizeof(double));
if (var == NULL){
    printf("ERROR: Problem in memory allocation");
    /* @TODO proper error handling i.e use error no &  stderr */
    exit(1);
}

doesn't causes memory leak, as if call to calloc() fails i.e memory allocation was failed i.e you don't have to call free().

From C standard

7.20.3.2 The free function

#include <stdlib.h>
void free(void *ptr);

The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation.
If ptr is a null pointer, no action occurs.

Achal
  • 11,821
  • 2
  • 15
  • 37
1

If the returned pointer is equal to NULL then this means that the memory was not allocated.

From the description of the function calloc (C11 Standard)

3 The calloc function returns either a null pointer or a pointer to the allocated space.

You may call free for a null-pointer though this does not have an effect.

On the other hand, you may require a memory of zero size. In this case if the returned pointer is not equal to NULL you have to call free to free the allocated memory.

Here is a demonstrative program

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

int main(void) 
{
    int *p = calloc( 0, sizeof( int ) );

    if ( p != NULL )
    {
        printf( "p = %p\n", ( void * )p );
    }

    free( p );

    return 0;
}

Using an inline compiler its output

p = 0x55efd9c81260

That is though the required size of memory is equal to 0 nevertheless the system allocated memory that needs to be freed.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

When you call malloc or calloc, and the call returns NULL, that means that nothing has been allocated, so nothing has to be freed, and there's no memory leak.

The case you have to worry about concerns realloc:

newp = realloc(oldp, newsize);

If realloc returns NULL, then oldp still points to the old memory (if any). If you were to write

p = realloc(p, newsize);

this would be a memory leak waiting to happen, the first time realloc failed. (That is, p would contain NULL, and the old, still-valid pointer would be lost.)

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
1

Your question is a little confusing:

  • If you are asking whether you should call free(var) before exiting the program, it is obviously not necessary since var == NULL when this memory allocation failed. There is nothing to free in var. Calling free(var) is allowed, but will do nothing.
  • If you are asking whether you should free other memory blocks allocated by your program before exiting it on a fatal error for this particular allocation attempt, it is not necessary either because all memory allocated by the OS to the program is released upon exit.
  • Other system resources such as open files, locks, semaphores... should be released automatically as well but you might still leave the environment in an inconsistent state by exiting the program abruptly like that. For example you might leave some files with inconsistent contents, undeleted temporary files, pending network transactions, inconsistent database updates...
chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

the code you uploaded allocates memory, not freeing it.

according to the info at http://www.cplusplus.com/reference/cstdlib/calloc/ and https://www.tutorialspoint.com/c_standard_library/c_function_calloc) - calloc returns NULL if the function failed to allocate memory from the OS. thus, it's a good practice to check the pointer after the function call, in order to avoi.

I'd say that if the function fails, either you're trying to allocate way too much memory, or that a memory leak already has occured, so I wouldn't worry about exiting the code after failure - it's probably for the best.

BarSahar
  • 69
  • 1
  • 4