Solved

How to use "free"?

Posted on 2000-04-24
11
169 Views
Last Modified: 2010-04-02
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
Comment
Question by:pigpig
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 2
11 Comments
 
LVL 1

Accepted Solution

by:
cdepetris earned 50 total points
ID: 2745730
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
 

Author Comment

by:pigpig
ID: 2745746
> 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
 
LVL 22

Expert Comment

by:nietod
ID: 2745748
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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Author Comment

by:pigpig
ID: 2745750
> 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
 

Author Comment

by:pigpig
ID: 2745756
nietod:

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

Author Comment

by:pigpig
ID: 2745759
nietod:

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

Author Comment

by:pigpig
ID: 2745765
nietod:

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

Author Comment

by:pigpig
ID: 2745776
nietod:

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

Author Comment

by:pigpig
ID: 2745781
nietod:

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

Expert Comment

by:cdepetris
ID: 2745897
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
 

Author Comment

by:pigpig
ID: 2911365
Answer accepted
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question