what will happen after i use free()???

N

neilcancer

i come from china,and i'm sorry that my english is very poor.

now i'm studing data structure and i met some problem about c language.

could you tell me what will happen after i use free()? i mean once i
use free() on a pointer,what will the pointer points to ?

for example:

#include<stdio.h>
#include<stdlib.h>

void main()

{

int *p;
if(!p) printf("good");
else printf("fail");


p=(int *)malloc(100);
if(p) printf("\n\ngood");
else printf("\n\nfail");



free(p);
if(!p) printf("\n\ngood");
else printf("\n\nfail");

*p=100;
printf("\n\n%d",*p);
}

the result is:

fail

good

fail

100

why?
 
A

Artie Gold

i come from china,and i'm sorry that my english is very poor.

now i'm studing data structure and i met some problem about c language.

could you tell me what will happen after i use free()? i mean once i
use free() on a pointer,what will the pointer points to ?

for example:

#include<stdio.h>
#include<stdlib.h>

void main() main() returns int

{

int *p;
if(!p) printf("good");
Undefined behavior, the pointer has not been initialized.
else printf("fail");


p=(int *)malloc(100);
Do not cast the return value of malloc(). It is unnecessary and can
(though, in this case you have included the proper header) hide errors.
if(p) printf("\n\ngood");
else printf("\n\nfail");



free(p);
if(!p) printf("\n\ngood");
Undefined behavior. You can't even *look* at the value of a pointer (not
to mention dereference it) after it has been free()-ed.
else printf("\n\nfail");

*p=100;
...and now you've gone and dereferenced it...
printf("\n\n%d",*p);
}

the result is:

fail

Undefined behavior means *anything* can happen. In this case the
`garbage' value of the pointer happened to be non-null.

You successfully allocated memory

See above (undefined behavior).
100
Similarly.

HTH,
--ag
 
R

Robert Gamble

i come from china,and i'm sorry that my english is very poor.

now i'm studing data structure and i met some problem about c language.

could you tell me what will happen after i use free()? i mean once i
use free() on a pointer,what will the pointer points to ?

for example:

#include<stdio.h>
#include<stdlib.h>

void main()

main returns int:

int main (void)

See:
http://www.c-faq.com/ansi/voidmain.html
http://www.c-faq.com/ansi/maindecl.html
http://www.c-faq.com/ansi/voidmainbooks.html
{

int *p;
if(!p) printf("good");
else printf("fail");

At this point p has not been initialized so it's value is indeterminate
and dereferencing the pointer will result in undefined behavior. In
fact, attempting to access it's value is also undefined behavior which
you have invoked in your if statement.
p=(int *)malloc(100);

Don't cast the return value of malloc.
See:
http://www.c-faq.com/malloc/mallocnocast.html

You should check the return value of malloc as it will return a null
pointer on failure.
if(p) printf("\n\ngood");
else printf("\n\nfail");

free(p);
if(!p) printf("\n\ngood");
else printf("\n\nfail");

Dereferencing a free'd pointer is undefined behavior, technically any
use of such a pointer, aside from assigning a new value to p, is
undefined behavior including the test in your if statement. The value
of p is not set to NULL after free is called.
See:
http://www.c-faq.com/malloc/ptrafterfree.html

Undefined behavior.
See:
http://www.c-faq.com/malloc/useafterfree.html
http://www.c-faq.com/malloc/ptrafterfree.html
printf("\n\n%d",*p);
}

the result is:

fail

Because automatic variables, including pointers, do not initialize
themselves and can contain any value, apparently that value wasn't 0 in
this case.

Because malloc returned a non-null value indicating success.

Because free'd pointers aren't set to NULL.

Undefined behavior so anything can happen. In some implementations
attempting to access memory that has been free'd will result in program
termination or worse. Other implementations may not actually release
the memory but mark it as available for future calls to malloc in which
case it may be possible to continue to use the memory successfully but
it would be quite unwise to do so and is still undefined behavior.

Robert Gamble
 
T

Thunderbird

my textbook is <the c programming language> by Kernighan and Ritchie.so
when i read your answer i felt astonished!!

first , i think in that book ,main can return void, but you told me it
was wrong!!

second ,i that book , malloc() is casted,cuz malloc() returns void.

the third is that thank you for teaching me the knowledge about free().
from now on i will never handle a pointer after it's free()ed. thank
you!
 
V

Vladimir S. Oka

my textbook is <the c programming language> by Kernighan and
Ritchie.so when i read your answer i felt astonished!!

Provide context! Read: said:
first , i think in that book ,main can return void, but you told me it
was wrong!!

second ,i that book , malloc() is casted,cuz malloc() returns void.

I think you should get the Second Edition of the above title...

(I can't check all your claims now, as my K&R2 copy is in the office --
I should be there as well, but hey, it's a gray day out there...).
 
R

Richard Heathfield

Thunderbird said:
my textbook is <the c programming language> by Kernighan and Ritchie.so
when i read your answer i felt astonished!!

first , i think in that book ,main can return void, but you told me it
was wrong!!

Page reference, please. (Don't spend forever trying to find it, though. K&R
does not in fact have any programs in it that abuse main's return type as
you suggest.)
second ,i that book , malloc() is casted,cuz malloc() returns void.

No, malloc returns void *, not void; and it requires no cast in C. K&R, for
some strange reason, decided to test their code using a C++ compiler. In
C++, the cast is required. In C, it is not. (But in C++, you almost
certainly won't be using malloc anyway.)
the third is that thank you for teaching me the knowledge about free().
from now on i will never handle a pointer after it's free()ed. thank
you!

free(p);
p = NULL;

is okay, though. (And wise, in most cases.)
 
C

CBFalconer

Thunderbird said:
my textbook is <the c programming language> by Kernighan and
Ritchie.so when i read your answer i felt astonished!!

first , i think in that book ,main can return void, but you told
me it was wrong!!

second ,i that book , malloc() is casted,cuz malloc() returns
void.

Look up the published errata from dmr for the book. All those
things are covered.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
J

Jordan Abel

I think you should get the Second Edition of the above title...

(I can't check all your claims now, as my K&R2 copy is in the office --
I should be there as well, but hey, it's a gray day out there...).

K&R2 does cast malloc in some places. Main never returns void, but
sometimes it doesn't return a value.
 
T

Thunderbird

Richard Heathfield 写é“:

Page reference, please. (Don't spend forever trying to find it, though. K&R
does not in fact have any programs in it that abuse main's return type as
free(p);
p = NULL;

is okay, though. (And wise, in most cases.)

thank u very much.
 
R

Ravi Nakidi

Hi,

This is a concept called Dangling pointer. Once u free the pointer
pointer does't point any memory location. So, the program gives a
problem. In real life after int *p; now person born then can u offer
some food correct, then he will work fine. Once that person died means
free(p) free of pointer. I can't offer any food to him correct. But, u
are offering *p=100. some food. Think, how it is possible in general
life. The same thing here. If u want once again allocate memory and use
it.


I think by this u will got. If, u want more and detailed explanation
conatct. Always welcome.

Regards,
Ravi Nakidi,
S/w Enginner.
 
S

santosh

Ravi said:
Hi,

This is a concept called Dangling pointer. Once u free the pointer
pointer does't point any memory location. So, the program gives a
problem. In real life after int *p; now person born then can u offer
some food correct, then he will work fine. Once that person died means
free(p) free of pointer. I can't offer any food to him correct. But, u
are offering *p=100. some food. Think, how it is possible in general
life. The same thing here. If u want once again allocate memory and use
it.

How do you expect anyone to understand the above?
Also please quote whomever you're replying to.
 
T

Thunderbird

Richard Heathfield wrote:

Page reference, please. (Don't spend forever trying to find it, though. K&R
does not in fact have any programs in it that abuse main's return type as
you suggest.)

now i wanna know what main() returns? int? is it wrong when i
write"void main()"in my programme? or i must write "int main()" since
it is the only correct way?
No, malloc returns void *, not void; and it requires no cast in C. K&R, for
some strange reason, decided to test their code using a C++ compiler. In
C++, the cast is required. In C, it is not. (But in C++, you almost
certainly won't be using malloc anyway.)

now there is another thing i wanna know.should i cast malloc when i
call malloc() in standard c?

is there any difference in standard c between "p=(char
*)malloc(1000)"and "p=malloc(1000)"?

thank u
 
V

Vladimir S. Oka

now i wanna know what main() returns? int? is it wrong when i
write"void main()"in my programme? or i must write "int main()" since
it is the only correct way?

At the risk of being trolled (I'm not sure I like the tone)...

The C Standard allows only:

int main(void)
int main(int argc, char *argv[])

However, it also allows particular implementations to specify whatever
they want, _as_long_as_it's_documented_. So, if your C compiler's
documentation says so, you may as well use:

float main(double *x)

NB, using this will break your code if you want to recompile on a
different implementation (and none are required to support anything not
in C Standard).
now there is another thing i wanna know.should i cast malloc when i
call malloc() in standard c?

Yes, since otherwise you won't be alerted by the compiler if you fail to
#include said:
is there any difference in standard c between "p=(char
*)malloc(1000)"and "p=malloc(1000)"?

Functionally, no, but see previous comment (i.e. not casting is not
required, but good sense).

--
BR, Vladimir

Forgive, O Lord, my little jokes on Thee
And I'll forgive Thy great big one on me.
-- Robert Frost
 
S

santosh

Thunderbird said:
Richard Heathfield wrote:



now i wanna know what main() returns? int? is it wrong when i
write"void main()"in my programme? or i must write "int main()" since
it is the only correct way?

According to the C99 standard main() is required to return an int
value. The form 'void main()' is not correct though many cheap C books
seem to unaccountably prefer it.
now there is another thing i wanna know.should i cast malloc when i
call malloc() in standard c?

It's strongly preferable to *not* cast malloc()'s return value.
is there any difference in standard c between "p=(char
*)malloc(1000)"and "p=malloc(1000)"?

p = malloc( 1000 * sizeof (char) ) is a better form than both the
above.
 
C

Coos Haak

Op 17 Mar 2006 10:44:15 -0800 schreef santosh:
According to the C99 standard main() is required to return an int
value. The form 'void main()' is not correct though many cheap C books
seem to unaccountably prefer it.


It's strongly preferable to *not* cast malloc()'s return value.


p = malloc( 1000 * sizeof (char) ) is a better form than both the
above.

Nonsense, sizeof (char) is always one in every conforming implementation
 
V

Vladimir S. Oka

Op 17 Mar 2006 10:44:15 -0800 schreef santosh:


Nonsense, sizeof (char) is always one in every conforming
implementation

Redundant, maybe. Nonsense, no.

IMHO, it's always better to spell out exactly what you mean. So, if
you're allocating memory for `char` -- say so. It will make your, or
maintainer's life much easier two years down the line.
 
R

Richard Heathfield

Vladimir S. Oka said:
Redundant, maybe. Nonsense, no.

IMHO, it's always better to spell out exactly what you mean. So, if
you're allocating memory for `char` -- say so. It will make your, or
maintainer's life much easier two years down the line.

I disagree, since it creates a maintenance headache when you change to
wchar_t. Better:

p = malloc(1000 * sizeof *p);
 
V

Vladimir S. Oka

Vladimir S. Oka said:


I disagree, since it creates a maintenance headache when you change to
wchar_t. Better:

p = malloc(1000 * sizeof *p);

Agreed. I should've spent a minute more on my post. :-(
 

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
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top