malloc questions in C

Q

questions?

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
 
E

Emmanuel Delahaye

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().
 
I

Ico

questions? said:
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.
 
Q

questions?

Ico said:
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?
 
M

Michael Mair

questions? said:
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
 
K

Keith Thompson

questions? said:
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.
 
Q

questions?

Keith said:
"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?
2) if I forgot to free a memory from malloc(), after termination of my
program. It will be returned back to system,right?
 
I

Ian Collins

questions? said:
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.
 
E

Emmanuel Delahaye

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.
 
M

Michael Mair

questions? said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,100
Latest member
MelodeeFaj
Top