0

I know the memory allocated dynamically as follow can be free like below:

int *array = malloc(sizeof(int) * 100);
memset(array,0,sizeof(int) * 100);
// use the array
free(array);

(Is the example above applied to C++ only or both C and C++?)

But I do not know if I have to free it when it is used like below (plain C language):

int array[3];
array[0] = 0;
array[1] = 1;
array[2] = 2;

And what is the difference between the two methods to create an array?

Thx a lot!

Damon Yuan
  • 3,683
  • 4
  • 23
  • 31

1 Answers1

3

No, you don't need to free the array in your second example, as you are not malloc'ing it.

About the difference:

Difference between declaration and malloc

In C++ you usually want to use new/delete, but malloc/free also work.

Community
  • 1
  • 1
Rodrigo Gómez
  • 1,079
  • 8
  • 24