free memory

M

Marcelo

Hello,

I am a beginner in C, and I would like to know if it is absolutely necessary to
free the memory after a malloc statement? What happens if I don't free the memory?

thanks a lot,

Marcelo
 
R

Richard Heathfield

Marcelo said:
Hello,

I am a beginner in C, and I would like to know if it is absolutely
necessary to
free the memory after a malloc statement? What happens if I don't free
the memory?

It stays allocated.

Modern operating systems will typically reclaim the memory when the program
terminates. Nevertheless, it's a good idea to free memory you no longer
need, because this will make your task much easier when your program ends
up being a function rather than a program, and called many times during the
execution of the program that gets wrapped around it. (Yes, this happens
sometimes, and yes, if you haven't freed up all your memory properly when
the original program was written, you have the mother of all memory leak
headaches.) Besides, it's good programming practice ("clean up your own
messes").
 
M

Marcelo

Is it necessary to make a free for other statements than malloc ones?

I mean, I have just to execute free() to the variables that have been explicitly
used with malloc() ???

thanks

Marcelo
 
P

pete

Marcelo said:
Is it necessary to make a free for other statements than malloc ones?

I mean, I have just to execute free()
to the variables that have been explicitly
used with malloc() ???

There's also calloc and realloc to consider.
realloc is also capable of freeing memory.
 
A

Artie Gold

Marcelo said:
Is it necessary to make a free for other statements than malloc ones?
No. Not only is it not necessary, it would not be correct.
I mean, I have just to execute free() to the variables that have been
explicitly used with malloc() ???
Correct.

HTH,
--ag
 
J

Jordan Abel

No. Not only is it not necessary, it would not be correct.
Correct.

Though for this purpose, you should consider realloc and calloc to work
as if "under the hood" they made calls to malloc [and in realloc's case,
free]

size_t _Alloclen(void *p);
/* returns the allocated size of a pointer, or 0 for a null pointer */

void *calloc(size_t n,size_t s) {
void *p = malloc(n*s);
if(p) memset(p,0,n*s);
return p;
}

void *realloc(void *p,size_t s) {
void *t = malloc(s);
size_t x = _Alloclen(p);
size_t S = x<s?x:s;
if(t || !S) {
if(S) memcpy(t,p,S);
free(p);
}
return t;
}

They can be assumed to work this way in as much as it is impossible for
a conforming program to tell the difference.
 
J

Joe Wright

pete said:
There's also calloc and realloc to consider.
realloc is also capable of freeing memory.

I've heard that but don't find a Standard reference to it. I think you
mean that 'realloc(ptr, 0);' is the same as 'free(ptr);'. Can you
provide a Standard reference? Thanks.
 
P

pete

Joe said:
I've heard that but don't find a Standard reference to it. I think you
mean that 'realloc(ptr, 0);' is the same as 'free(ptr);'. Can you
provide a Standard reference? Thanks.

N869
7.20.3.2 The free function

Description
[#2] The free function causes the space pointed to by ptr to
be deallocated, that is, made available for further
allocation. If ptr is a null pointer, no action occurs.
Otherwise, if the argument does not match a pointer earlier
returned by the calloc, malloc, or realloc function, or if
the space has been deallocated by a call to free or realloc,
the behavior is undefined.

7.20.3.4 The realloc function

Description
[#2] The realloc function deallocates the old object pointed
to by ptr
 
K

Keith Thompson

Joe Wright said:
I've heard that but don't find a Standard reference to it. I think you
mean that 'realloc(ptr, 0);' is the same as 'free(ptr);'. Can you
provide a Standard reference? Thanks.

A successful resizing realloc() (with a non-null first argument and a
non-zero second argument) can either return its first argument after
doing whatever is necessary to adjust the object size, or it can move
the object to a different location. In the latter case, it has to
allocate space for the new object and free the space for the old
object.

The wording in the standard is:

The realloc function deallocates the old object pointed to by ptr
and returns a pointer to a new object that has the size specified
by size.

The phrase "deallocates the old object" refers to doing the equivalent
of a call to free().
 
J

Joe Wright

pete said:
Joe said:
I've heard that but don't find a Standard reference to it. I think you
mean that 'realloc(ptr, 0);' is the same as 'free(ptr);'. Can you
provide a Standard reference? Thanks.


N869
7.20.3.2 The free function

Description
[#2] The free function causes the space pointed to by ptr to
be deallocated, that is, made available for further
allocation. If ptr is a null pointer, no action occurs.
Otherwise, if the argument does not match a pointer earlier
returned by the calloc, malloc, or realloc function, or if
the space has been deallocated by a call to free or realloc,
the behavior is undefined.

7.20.3.4 The realloc function

Description
[#2] The realloc function deallocates the old object pointed
to by ptr

In full, it says:

[#2] The realloc function deallocates the old object pointed |
to by ptr and returns a pointer to a new object that has the |
size specified by size. The contents of the new object |
shall be the same as that of the old object prior to |
deallocation, up to the lesser of the new and old sizes. |
Any bytes in the new object beyond the size of the old |
object have indeterminate values. |

No reference to free() here. Want to look again? Even if realloc()
deallocates the memory at ptr and with 0 as a size argument, if it were
successful it returns a pointer to memory of zero length? That's not
what free() does is it? What do we do with the returned pointer value,
free() it?
 
J

Joe Wright

Keith said:
A successful resizing realloc() (with a non-null first argument and a
non-zero second argument) can either return its first argument after
doing whatever is necessary to adjust the object size, or it can move
the object to a different location. In the latter case, it has to
allocate space for the new object and free the space for the old
object.

The wording in the standard is:

The realloc function deallocates the old object pointed to by ptr
and returns a pointer to a new object that has the size specified
by size.

The phrase "deallocates the old object" refers to doing the equivalent
of a call to free().

I just responded to pete above. A successful call to realloc(ptr,0) will
return a pointer to the new (or same) memory. It will be of zero length.
What are we to do with that pointer value? As it is returned from
realloc() I suppose it still has to be free()'ed. Or?

I don't think realloc(ptr,0) is equivalent to free(ptr) according to the
Standard. If you think so, please show me. I will be indebted, and
modestly embarrassed.
 
P

pete

Joe said:
Joe said:
pete wrote:

Marcelo wrote:


Is it necessary to make a free for other statements than malloc ones?

I mean, I have just to execute free()
to the variables that have been explicitly
used with malloc() ???


There's also calloc and realloc to consider.
realloc is also capable of freeing memory.


I've heard that but don't find a Standard reference to it. I think you
mean that 'realloc(ptr, 0);' is the same as 'free(ptr);'. Can you
provide a Standard reference? Thanks.


N869
7.20.3.2 The free function

Description
[#2] The free function causes the space pointed to by ptr to
be deallocated, that is, made available for further
allocation. If ptr is a null pointer, no action occurs.
Otherwise, if the argument does not match a pointer earlier
returned by the calloc, malloc, or realloc function, or if
the space has been deallocated by a call to free or realloc,
the behavior is undefined.

7.20.3.4 The realloc function

Description
[#2] The realloc function deallocates the old object pointed
to by ptr

In full, it says:

[#2] The realloc function deallocates the old object pointed |
to by ptr and returns a pointer to a new object that has the |
size specified by size. The contents of the new object |
shall be the same as that of the old object prior to |
deallocation, up to the lesser of the new and old sizes. |
Any bytes in the new object beyond the size of the old |
object have indeterminate values. |

No reference to free() here. Want to look again? Even if realloc()
deallocates the memory at ptr and with 0 as a size argument,
if it were
successful it returns a pointer to memory of zero length? That's not
what free() does is it?

realloc does more than what free does,
but realloc does everything that free does.
 
S

S.Tobias

Joe Wright said:
[snip]

I just responded to pete above. A successful call to realloc(ptr,0) will
return a pointer to the new (or same) memory. It will be of zero length.
(Unless the pointer is null, of course.)
What are we to do with that pointer value? As it is returned from
realloc() I suppose it still has to be free()'ed. Or?

I don't think realloc(ptr,0) is equivalent to free(ptr) according to the
Standard. If you think so, please show me. I will be indebted, and
modestly embarrassed.
In C99 it's not equivalent. In C89 it is; here's a reference to the post
that convinced me about this recently:
| From: Nils Weller <xxxxxxxxxxxxxx>
| Newsgroups: comp.lang.c
| Subject: Re: More questions on realloc - Thanks
| Date: 1 Dec 2005 15:55:14 GMT
| Message-ID: <[email protected]>

I guess the difference doesn't matter for a programmer if he
always assumes that realloc()'s results should be free()d.

(just my $0.02)
 

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

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,473
Latest member
ChristelPe

Latest Threads

Top