-2

If I dynamically allocated a space for a pointer, list this:

int *a = (int*)malloc(sizeof(int));

should I free a when the code is done? Thanks!

Liu
  • 413
  • 4
  • 11
  • 5
    The space for the pointer isn't allocated dynamically, the `int` that it points to is. When the code is done, you should free it with `free(a);`. – Blaze Mar 07 '19 at 14:56
  • 3
    1. Don't cast `malloc`. 2. It is a good practice to free allocated memory and is always recommended, but it's not necessary if you use the pointer till the absolute end of the program. – Susmit Agrawal Mar 07 '19 at 14:57
  • 2
    Rule of thumb: anything that has been allocated by `malloc/calloc/realloc/` needs to be freed with `free` at some point. – Jabberwocky Mar 07 '19 at 14:58
  • 1
    I think I've got your question now. `a` is not uninitialized here. But the `int` _pointed_ by `a` is uninitialized. Yes you need to free `a` a some point. – Jabberwocky Mar 07 '19 at 15:08
  • @Jabberwocky: Also `strdup`, `asprintf` and `vasprintf`. Maybe others? – abelenky Mar 07 '19 at 15:12
  • Why you shouldn't cast a malloc: https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Jose Mar 07 '19 at 15:13
  • 1
    @abelenky correct, but `asprintf` is a gcc extension, and `strdup` is posix. – Jabberwocky Mar 07 '19 at 15:14
  • Caution: the value passed to `free` must be *exactly the same* as was obtained from `malloc`, not one that has been, for example, incremented. The pointer's **value** is its only identity, as far as `free` is concerned. – Weather Vane Mar 07 '19 at 15:18

4 Answers4

6

I think you have a little misunderstanding related to pointer.

Your title says:

Free uninitialized pointer ...

and your code is

int *a = (int*)malloc(sizeof(int));

The problem with this is that there is no uninitialized pointer in the code. The only pointer in the code is the variable a and it is initialized by the value returned by malloc.

Freeing an uninitialized pointer would be bad - example:

int *a;  // a is an uninitialized pointer

free(a);  // Real bad - don't do this

but since you actually initialize the pointer then - Yes, you must call free when your are done using the object/memory pointer a points to. It does not matter whether or not the pointed-to object (aka memory) has been assigned a value.

The general rule: For each call of malloc there must be a call of free

(Exception: If your program terminates, you don't need to call free)

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
3
int *a = malloc(sizeof(*a));
if (a) 
{
    /* a is now valid; use it: */
    *a = 1 + 2 + 3;
    printf("The value calculated is %d\n", *a);
}

/* Variable A is done being used; free the memory. */
free(a);  /* If a failed to be allocated, it is NULL, and this call is safe. */
abelenky
  • 63,815
  • 23
  • 109
  • 159
0

Yes. If you successfully malloc something is is correct to free it as well.

int *a = (int *) malloc(sizeof int);
if (a != NULL)
{
    /* Do whatever you need to do with a */
    free(a);
}
else
{
    puts("the malloc function failed to allocate an int");
}
EvilTeach
  • 28,120
  • 21
  • 85
  • 141
0
int *a = (int*)malloc(sizeof(int));

should I free a when the code is done?

The question should be

Must I free a when the code is done?

And the answer is YES. A malloc must be accompanied by a a free statement.

free(a);
VHS
  • 9,534
  • 3
  • 19
  • 43