overworked student
overworked student

Reputation: 69

How should I free a malloc variable when it has been assigned to another variable as well? (C Language)

Sorry if this has been asked already.

I have this program:

int main()
{
  int *X = (int *) malloc(sizeof(int));
  *X = 10; 
  int *Y = X;
  free(Y);

  return 0;
}

I don't know if the code is correct already or not. Do I still need to call free(X) as I have called malloc() for X? Do I need to call malloc() for Y as well, or is Y fine as it is?

Upvotes: 1

Views: 1117

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 310990

How should I free a malloc variable when it has been assigned to another variable as well? (C Language)

You need not to free the declared variables (pointers) X and Y. They have automatic storage duration and will be freed automatically by the compiler when the control exits the scope where the variables are declared.

What you need is to free the dynamically allocated memory in this declaration

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

The memory was allocated one time and shall be freed also one time independent on how many pointers point to the allocated memory.

After this declaration

int *Y = X;

two pointers, X and Y, point now to the allocated memory.

To free the allocated memory you can use either of the pointers as for example

free( X );

or

free( Y );

After such a call the both pointers are dangling pointers. Usually it is a good idea to set such pointers to NULL.

X = NULL;
Y = NULL;

Upvotes: 0

Gerhardh
Gerhardh

Reputation: 12404

You do not need to care about variables. You only need to care about allocated memory blocks.

Your code is fine (except for missing headers) You allocate a block and free the same block again. That's it.

If you add another malloc for Y you will cause a memory leak when you assign valu of X. If you add free(X) you free the same block twice which is illegal.

Note: You must be aware that after free(Y) you are not allowed to access *X as the memory does not belong to you anymore.

Upvotes: 1

MikeCAT
MikeCAT

Reputation: 75062

This code is not correct because required header stdlib.h is not included and the result of malloc() is not checked. Also casting results of malloc() in C is discouraged.

Correct code is:

#include <stdlib.h>

int main(void)
{
  int *X = malloc(sizeof(int));
  if (X == NULL) return 1;
  *X = 10;
  int *Y = X;
  free(Y);

  return 0;
}

You don't need and mustn't call free(X); because X is pointing at the same point as Y and the memory is already freed by free(Y);.

You need to call malloc() for Y if you want Y point at different (new) thing than X. In this case freeing both what are pointed at by X and Y is recommended. (freeing will satisfy checkers like Valgrind, but in modern OS you don't have to free things at end of program execution because the OS will free up all memories used in your process. For more information, see c - What REALLY happens when you don't free after malloc? - Stack Overflow)

Upvotes: 3

Related Questions