473,402 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

some queries on freeing memory allocated using malloc

Hi,
(1)if i do the memory allocation like:
int **p;
p= malloc(n*sizeof p);
for(i=0;i<n;i++) /* i and n are integers */
p[i]= malloc(n*sizeof p);

then in that case is this command sufficient to free all the allocated
memory:

free(p);

or do i need to free it like this:

for(i=0;i<n;i++)
free(p[i]);
free(p);

(2)
consider this:
func1(...)
{
...
...
for(i=0;i<n;i++){
p=func2(...) /* p is an integer pointer */
...
...
}
...
}/* end func1 */

int *func2(...)
{
int *P;
p= malloc(n*sizeof p);
...
...
return(p);
}/* end func2 */

in the above pseudo code where should i free the memory which has been
allocated in func2, supposing that n is significantly large. should it
be every time just before the call to func2 or at the end of func1 or
after return(p) in func2 itself.

thanks,
hassan
Nov 13 '05 #1
3 2558
Hassan Iqbal <iq**********@extenprise.net> scribbled the following:
Hi,
This question comes up fairly regularly here.
(1)if i do the memory allocation like:
int **p;
p= malloc(n*sizeof p);
for(i=0;i<n;i++) /* i and n are integers */
p[i]= malloc(n*sizeof p); then in that case is this command sufficient to free all the allocated
memory: free(p);
No it isn't.
or do i need to free it like this:

for(i=0;i<n;i++)
free(p[i]);
free(p);
Yes you do.
(2)
consider this:
func1(...)
{
...
...
for(i=0;i<n;i++){
p=func2(...) /* p is an integer pointer */
...
...
}
...
}/* end func1 */ int *func2(...)
{
int *P;
p= malloc(n*sizeof p);
...
...
return(p);
}/* end func2 */ in the above pseudo code where should i free the memory which has been
allocated in func2, supposing that n is significantly large. should it
be every time just before the call to func2 or at the end of func1 or
after return(p) in func2 itself.


It should be in the code in func1(). If you add the free(p) code in
func2() after the return(p), it will never get executed, resulting in
memory leaks. Code after a return statement is *never* executed, no
matter what code it is, or which function it appears in.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Immanuel Kant but Genghis Khan."
- The Official Graffitist's Handbook
Nov 13 '05 #2
Hassan Iqbal wrote:
(1)if i do the memory allocation like:
int **p;
p= malloc(n*sizeof p);
We'll give you the benefit of the doubt (for code written instead of
copy-pasted), but the statement above will not exactly do what you expect.

p = malloc(n * sizeof *p);
for(i=0;i<n;i++) /* i and n are integers */
p[i]= malloc(n*sizeof p);
Same here: p[i] = malloc(n * sizeof **p);

for(i=0;i<n;i++)
free(p[i]);
free(p);
That's what you need to free it all, indeed.
in the above <snipped> pseudo code where should i free the memory which has been
allocated in func2, supposing that n is significantly large. should it
be every time just before the call to func2 or at the end of func1 or
after return(p) in func2 itself.

You are quite free to free p wherever between the place it is allocated
at, and the place you last use it. The extreme last place where you can
free p is when it goes out of scope, likely when you are going to leave
the block (between the { and the } ) where p has been declared.

--
Bertrand Mollinier Toublet
Blog: http://www.bmt.dnsalias.org/blog

Nov 13 '05 #3


Hassan Iqbal wrote:
Hi,
(1)if i do the memory allocation like:
You are allocating a multidimensional array of type int. You
need to get the sizes correct. Look at the faq question 6.16
located at http://www.eskimo.com/~scs/C-faq/q6.16.html
int **p;
p= malloc(n*sizeof p);
p = malloc(n*sizeof *p); /* or n*sizeof(int *) */
for(i=0;i<n;i++) /* i and n are integers */
p[i]= malloc(n*sizeof p);
p[i] = malloc(n * sizeof **p); /* or n*sizeof(int) */

then in that case is this command sufficient to free all the allocated
memory:

free(p);
This is wrong.

or do i need to free it like this:

for(i=0;i<n;i++)
free(p[i]);
free(p);
This is correct way to free the allocations.
(2)
consider this:
func1(...)
{
...
...
for(i=0;i<n;i++){
p=func2(...) /* p is an integer pointer */
...
...
}
...
}/* end func1 */

int *func2(...)
{
int *P;
p= malloc(n*sizeof p);
...
...
return(p);
}/* end func2 */

in the above pseudo code where should i free the memory which has been
allocated in func2, supposing that n is significantly large. should it
be every time just before the call to func2 or at the end of func1 or
after return(p) in func2 itself.


You are allocating the space in func2 which is returning a pointer
to the allocated space. You will then use this space in func1 for
some purpose. Once you are finished using the space then
deallocate (free) it. Beware for the hazards in func2 should there
be a allocation failure. In the case you will need to recover from
the allocation failure and signal func2 of the failure. This is
usually done by func2 returning NULL.

--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: disco | last post by:
I am working on this example from a book "C Primer Plus" by Prata 4th edition - p. 672. There is no erata on this problem at the publisher's website. 1) Is it a violation of copyright laws to...
11
by: binaya | last post by:
Dear all, Say I allocate a block of memory using the command malloc.I have a question. Can I deallocate certain portion of it only during runtime ? For eg: Say I allocate 5 pointers to int...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
7
by: Emmet Caulfield | last post by:
In the course of examining the code for an Internet-connected authentication server, I came across the following code (twice in one function with different constants in the "if") in a file of some...
1
by: skg | last post by:
I am passing the address of pointer like char** from managed extension and getting the its initialized value from a C library dll. How can i free the memory from the code in Managed Extension ?...
20
by: Tommy Vercetti | last post by:
Hi - Great group! I have 2 queries about undefined behavior: 1) Is the following code undefined? float myfunction(float f) {
66
by: karthikbalaguru | last post by:
Hi, Will 'free' return the memory Immediately to the OS ? Thx in advans, Karthik Balaguru
25
by: Andreas Eibach | last post by:
Hi again, one of the other big woes I'm having... typedef struct perBlockStru /* (structure) Long words per block */ { unsigned long *lword; } lwperBlockStru_t;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.