[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

How to use "free"?

Hello experts,

I use malloc to allocate array,
eg. int *a; a = malloc(10);

how can I use "free" to free it?
how if a is a 2D array?
0
pigpig
Asked:
pigpig
  • 8
  • 2
1 Solution
 
cdepetrisCommented:
you would just use
free(a);

however you only allocated
a[10/sizeof(int)] entries in your array


so to malloc a 2d array you would use
// allocate a[2][5]
int *a = (int*)malloc(5 * 2 * sizeof(int));

and to free it just call
free(a);

Chris
0
 
pigpigAuthor Commented:
> so to malloc a 2d array you would use
> // allocate a[2][5]
> int *a = (int*)malloc(5 * 2 * sizeof(int));

How do I know if I am allocating size of 10 or 2x5?

also, after calling "free", the memory address of that address will go to "0x000000"? if not, is it means the "free" is failed?

Thanks a lot!
0
 
nietodCommented:
You should not be using malloc and free in C++.  they only time it makes sense to use them is when you are writing overloaded new and delete operators.  Other than that, you should be using new and delete.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
pigpigAuthor Commented:
> so to malloc a 2d array you would use
> // allocate a[2][5]
> int *a = (int*)malloc(5 * 2 * sizeof(int));

How do I know if I am allocating size of 10 or 2x5?

also, after calling "free", the memory address of that address will go to "0x000000"? if not, is it means the "free" is failed?

Thanks a lot!
0
 
pigpigAuthor Commented:
nietod:

I also don't want to use "malloc and free", but it acts as some kinds of overloading delete and new.
0
 
pigpigAuthor Commented:
nietod:

I also don't want to use "malloc and free", but it acts as some kinds of overloading delete and new.
0
 
pigpigAuthor Commented:
nietod:

I also don't want to use "malloc and free", but it acts as some kinds of overloading delete and new.
0
 
pigpigAuthor Commented:
nietod:

I also don't want to use "malloc and free", but it acts as some kinds of overloading delete and new.
0
 
pigpigAuthor Commented:
nietod:

I also don't want to use "malloc and free", but it acts as some kinds of overloading delete and new.
0
 
cdepetrisCommented:
I agree with nietod you should not use malloc and free in c++, however
as for you questions
the pointer will not be set to 0x00000000 when you free the memory, that is up to you. Also it does not matter if you allocate out 10 * sizeof(int) or 2*5*sizeof(int) you can access it as a single dimesioned arrray a[10] or a 2d array a[2][5]. When you access say a[1][3] it is equivalent to a[8]

Chris
0
 
pigpigAuthor Commented:
Answer accepted
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 8
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now