469,277 Members | 1,953 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,277 developers. It's quick & easy.

malloc questions in C

if I use malloc() in a function to allocate a space for my array of
structures. I didn't free() them anywhere in the program.
I found that I can still use that space after I come back to main().

Am I supposed to use that space or it is temporary and will be removed
soon afterwards?

Thanks

Jan 21 '06 #1
9 1537
questions? a écrit :
if I use malloc() in a function to allocate a space for my array of
structures. I didn't free() them anywhere in the program.

I found that I can still use that space after I come back to main().

Am I supposed to use that space or it is temporary and will be removed
soon afterwards?


The duration of an allocated bloc starts when malloc() returns a
non-NULL value (aka a valid block address) and ends when free() is
invoked (with the value returned by the corresponding malloc().

--
A+

Emmanuel Delahaye
Jan 21 '06 #2
Ico
questions? <un************@hotmail.com> wrote:
if I use malloc() in a function to allocate a space for my array of
structures. I didn't free() them anywhere in the program.
I found that I can still use that space after I come back to main().
That is how it is supposed to work. When you allocate memory with
malloc() or one of the similar functions, it will be available until you
call free(). A lot of C implementations will also free this memory for
you when your program exits, but don't count on that.
Am I supposed to use that space or it is temporary and will be removed
soon afterwards?


You are supposed to give back the memory to the system with free() when
you no longer need the memory, or at least before your program
terminates.

--
:wq
^X^Cy^K^X^C^C^C^C
Jan 21 '06 #3

Ico wrote:
questions? <un************@hotmail.com> wrote:
if I use malloc() in a function to allocate a space for my array of
structures. I didn't free() them anywhere in the program.
I found that I can still use that space after I come back to main().


That is how it is supposed to work. When you allocate memory with
malloc() or one of the similar functions, it will be available until you
call free(). A lot of C implementations will also free this memory for
you when your program exits, but don't count on that.


So, with malloc() function, it is static regardless in the sense that
whether you are in main or in a function, it is permanant,right?


Am I supposed to use that space or it is temporary and will be removed
soon afterwards?


You are supposed to give back the memory to the system with free() when
you no longer need the memory, or at least before your program
terminates.

--
:wq
^X^Cy^K^X^C^C^C^C


Jan 21 '06 #4
questions? wrote:
Ico wrote:
questions? <un************@hotmail.com> wrote:
if I use malloc() in a function to allocate a space for my array of
structures. I didn't free() them anywhere in the program.
I found that I can still use that space after I come back to main().


That is how it is supposed to work. When you allocate memory with
malloc() or one of the similar functions, it will be available until you
call free(). A lot of C implementations will also free this memory for
you when your program exits, but don't count on that.


So, with malloc() function, it is static regardless in the sense that
whether you are in main or in a function, it is permanant,right?


There are three kinds of storage durations: automatic, static and
allocated. So, "static" is probably the wrong term to use.

Static storage duration: The object exists before main() is entered
the first time till the end of the programme. It is always initialised
(default: zero of the appropriate type).
Automatic storage duration: The object begins life starting at its
declaration to the end of the scope it has been declared in.
Allocated storage duration: The object begins life starting at the
successful call to malloc()/calloc()/realloc() and can be accessed
until it is free()d or until the end of the programme.

Example (not tested):

#include <stdlib.h>

int *GiveMeFive (void)
{
static int five = 5;
return &five;
}

int *GiveMeTen (void)
{
int *ten = malloc(*ten);
if (ten) {
*ten = 10;
}
return ten;
}

int *GiveMeNothing (void)
{
int nothing = 0;
return &nothing;
}

int main (void)
{
int *example = NULL;

example = GiveMeFive();
*example += 2; /* Okay. five is permanently changed. */

example = GiveMeTen();
if (example)
{
*example += 3; /* Okay; the storage returned at the first
* call of GiveMeTen() is permanently changed. */
example = GiveMeTen(); /* Memory leak: The storage written to
* above still exists and contains "13"
* but is no longer accessible. If example
* is != NULL, it contains the address of
* newly allocated storage.
*/
if (example) {
*example += 4;
free(example); /* Now ends the life of example */
}
}

example = GiveMeNothing() /* Error! nothing is no longer alive */

/* five and, if malloc() was successful, the first value of ten,
* are still alive. */

return 0;
}
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jan 22 '06 #5
"questions?" <un************@hotmail.com> writes:
Ico wrote:
questions? <un************@hotmail.com> wrote:
> if I use malloc() in a function to allocate a space for my array of
> structures. I didn't free() them anywhere in the program.
> I found that I can still use that space after I come back to main().


That is how it is supposed to work. When you allocate memory with
malloc() or one of the similar functions, it will be available until you
call free(). A lot of C implementations will also free this memory for
you when your program exits, but don't count on that.


So, with malloc() function, it is static regardless in the sense that
whether you are in main or in a function, it is permanant,right?


"Static" isn't the right word for it.

The lifetime of a memory block allocated with malloc() doesn't depend
on where you call it.

There are three storage durations: "static" (for static or global
objects; these exist for the entire lifetime of the program),
"automatic" (for non-static objects declared locally to a function or
block; these cease to exist when the function or block finishes), and
"allocated" (for objects allocated by malloc(); these exist until
they're free()d).

Note that malloc() and free() aren't the only relevant functions, but
I'm too lazy to go into the details of calloc() and realloc().

And main() *is* a function. An object declared inside main() isn't
global; it's local to the function.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 22 '06 #6

Keith Thompson wrote:
"questions?" <un************@hotmail.com> writes:
Ico wrote:
questions? <un************@hotmail.com> wrote:
> if I use malloc() in a function to allocate a space for my array of
> structures. I didn't free() them anywhere in the program.
> I found that I can still use that space after I come back to main().

That is how it is supposed to work. When you allocate memory with
malloc() or one of the similar functions, it will be available until you
call free(). A lot of C implementations will also free this memory for
you when your program exits, but don't count on that.


So, with malloc() function, it is static regardless in the sense that
whether you are in main or in a function, it is permanant,right?


"Static" isn't the right word for it.

The lifetime of a memory block allocated with malloc() doesn't depend
on where you call it.

There are three storage durations: "static" (for static or global
objects; these exist for the entire lifetime of the program),
"automatic" (for non-static objects declared locally to a function or
block; these cease to exist when the function or block finishes), and
"allocated" (for objects allocated by malloc(); these exist until
they're free()d).

Note that malloc() and free() aren't the only relevant functions, but
I'm too lazy to go into the details of calloc() and realloc().

And main() *is* a function. An object declared inside main() isn't
global; it's local to the function.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Thank you all for the answer!!! It is really really helpful.

1) what is really a memory leak?
2) if I forgot to free a memory from malloc(), after termination of my
program. It will be returned back to system,right?

Jan 22 '06 #7
questions? wrote:
1) what is really a memory leak?
Allocating memory and not freeing it before the last pointer to it goes
out of scope or is reassigned. After the pointer has been reassigned,
the previously allocated memory is leaked as it can no longer be freed.
2) if I forgot to free a memory from malloc(), after termination of my
program. It will be returned back to system,right?

Right, assuming there is a system to return it to.

--
Ian Collins.
Jan 22 '06 #8
questions? a écrit :
1) what is really a memory leak?
When there is no way to free an allocated block. If you do that on 24/7
application (a server, for instance), you 'eat' the memory ressources
until the system crashes down.
2) if I forgot to free a memory from malloc(), after termination of my
program. It will be returned back to system,right?


Can't tell. It's a system issue.

--
A+

Emmanuel Delahaye
Jan 22 '06 #9
questions? wrote:
Keith Thompson wrote:
"questions?" <un************@hotmail.com> writes:
Ico wrote:

questions? <un************@hotmail.com> wrote:

>if I use malloc() in a function to allocate a space for my array of
>structures. I didn't free() them anywhere in the program.
>I found that I can still use that space after I come back to main().

That is how it is supposed to work. When you allocate memory with
malloc() or one of the similar functions, it will be available until you
call free(). A lot of C implementations will also free this memory for
you when your program exits, but don't count on that.

So, with malloc() function, it is static regardless in the sense that
whether you are in main or in a function, it is permanant,right?
"Static" isn't the right word for it.

The lifetime of a memory block allocated with malloc() doesn't depend
on where you call it.

There are three storage durations: "static" (for static or global
objects; these exist for the entire lifetime of the program),
"automatic" (for non-static objects declared locally to a function or
block; these cease to exist when the function or block finishes), and
"allocated" (for objects allocated by malloc(); these exist until
they're free()d).

Note that malloc() and free() aren't the only relevant functions, but
I'm too lazy to go into the details of calloc() and realloc().

And main() *is* a function. An object declared inside main() isn't
global; it's local to the function.


Thank you all for the answer!!! It is really really helpful.

1) what is really a memory leak?


Ask wikipedia or the jargon file
http://en.wikipedia.org/wiki/Memory_leak
http://www.catb.org/jargon/html/M/memory-leak.html
2) if I forgot to free a memory from malloc(), after termination of my
program. It will be returned back to system,right?


Yes, if your system does this. And as long as the usage of your
code does not change.
Imagine that you wrote a module and a "test" programme for it.
#include "myhappyfunmodule.h"

void test (void)
{
/* use myhappyfunmodule stuff */
/* a) myhappyfunmodule has an internal memory leak */
/* b) myhappyfunmodule returns allocated storage and
** test() does not free() it */
}
int main (void)
{

test();

return 0;
}
Now, one year after having written it, you want to use
myhappyfunmodule for a programme which runs for hours and
days in a loop. You thoughtlessly copy "test", throw out
main(), rename test() to runhappyfun(), change a couple
of things and try it out.
runhappyfun() is called millions of times and leaks a
couple of bytes every time; this effectively becomes a problem
over time as the programme eats your resources. You may have
to terminate it early/it dies by itself/it takes down your
system and valuable data.
Now, you have a look into runhappyfun(), find b), remove it.
Seems to work.
a) still leaks a few bytes at a time but maybe not always.
Then comes the one time it really counts and, sadly, a) eats
your memory once again and does Bad Things.
So, you have to debug your old code, find a) and fix it.
Maybe you even knew about the leaks when writing
myhappyfunmodule but thought "test() is only a test function,
I do not want to bother with deallocation" and "well, in a
very special situation, we have a leak (a)) but preventing
this costs ten lines extra which I always can do later",
respectively...
If you do it right from the beginning, you can save yourself
trouble further down the road.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jan 22 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by dam_fool_2003 | last post: by
11 posts views Thread by Mannequin* | last post: by
11 posts views Thread by Gustavo G. Rondina | last post: by
23 posts views Thread by puzzlecracker | last post: by
7 posts views Thread by Alexandre | last post: by
17 posts views Thread by Chen Shusheng | last post: by
3 posts views Thread by Petr Pavlu | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.