|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Roman Mashak wrote:
> Hello, > > is it legal to destroy the memory pointed to by a pointer and then assign > NULL to it? Say, like this: > > char *ptr; > ptr = malloc(10); > free(ptr); > ptr = NULL; > Yes, you can assign what ever you like to a freed pointer. Assignment to NULL is often used to mark the memory as freed. -- Ian Collins. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Roman Mashak wrote:
> Hello, > > is it legal to destroy the memory pointed to by a pointer and then > assign NULL to it? Say, like this: > > char *ptr; > ptr = malloc(10); > free(ptr); > ptr = NULL; Certainly. This is common practise. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
"Roman Mashak" <mrv@tusur.ru> wrote:
> is it legal to destroy the memory pointed to by a pointer and then assign > NULL to it? Say, like this: > > char *ptr; > ptr = malloc(10); > free(ptr); > ptr = NULL; Legal, but useless. If you think you've found the royal road to memory management, consider the following: char *ptr, &ptr2; ptr=malloc(10); ptr2=ptr+2; free(ptr); ptr=NULL; if (ptr+2) crash_hard_and_without_grace(); Richard |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
"Roman Mashak" <mrv@tusur.ru> wrote in message > is it legal to destroy the memory pointed to by a pointer and then assign > NULL to it? Say, like this: > > char *ptr; > ptr = malloc(10); > free(ptr); > ptr = NULL; > Yes. As Richard Bos says. it won't solve all your memory management problems. But if the pointer will not immediately go out of scope, it is good idea to make it null to mark that it is now invalid. -- Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Jan 31, 2:15 am, "Roman Mashak" <m...@tusur.ru> wrote:
> Hello, > > is it legal to destroy the memory pointed to by a pointer and then assign > NULL to it? Say, like this: > > char *ptr; > ptr = malloc(10); > free(ptr); > ptr = NULL; > > With best regards, Roman Mashak. E-mail: <removed> This is done because then you can do free(ptr); which will be free(NULL); and that does nothing. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Wed, 30 Jan 2008 07:00:38 -0600, vippstar@gmail.com wrote
(in article <31d018f4-f8eb-4121-801c-329281a0cba7@q21g2000hsa.googlegroups.com>): > On Jan 31, 2:15 am, "Roman Mashak" <m...@tusur.ru> wrote: >> Hello, >> >> is it legal to destroy the memory pointed to by a pointer and then assign >> NULL to it? Say, like this: >> >> char *ptr; >> ptr = malloc(10); >> free(ptr); >> ptr = NULL; >> >> With best regards, Roman Mashak. E-mail: <removed> > > This is done because then you can do free(ptr); which will be > free(NULL); and that does nothing. Historically, that was not always the case. -- Randy Howard (2reply remove FOOBAR) "The power of accurate observation is called cynicism by those who have not got it." - George Bernard Shaw |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On Jan 30, 3:45 pm, Randy Howard <randyhow...@FOOverizonBAR.net>
wrote: > On Wed, 30 Jan 2008 07:00:38 -0600, vipps...@gmail.com wrote > (in article > <31d018f4-f8eb-4121-801c-329281a0c...@q21g2000hsa.googlegroups.com>): > > > On Jan 31, 2:15 am, "Roman Mashak" <m...@tusur.ru> wrote: > >> Hello, > > >> is it legal to destroy the memory pointed to by a pointer and then assign > >> NULL to it? Say, like this: > > >> char *ptr; > >> ptr = malloc(10); > >> free(ptr); > >> ptr = NULL; > > >> With best regards, Roman Mashak. E-mail: <removed> > > > This is done because then you can do free(ptr); which will be > > free(NULL); and that does nothing. > > Historically, that was not always the case. True, back then I suppose they assigned a freed pointer to NULL so when they use a freed pointer they get a NULL address access violation. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
In article <47a03c63.2858170722@news.xs4all.nl>,
Richard Bos <rlb@hoekstra-uitgeverij.nl> wrote: >"Roman Mashak" <mrv@tusur.ru> wrote: > >> is it legal to destroy the memory pointed to by a pointer and then assign >> NULL to it? Say, like this: >> >> char *ptr; >> ptr = malloc(10); >> free(ptr); >> ptr = NULL; > >Legal, but useless. Not completely useless, only mostly. If you know that the pointer you're setting to NULL was the only pointer into that memory, then it's perfectly safe and useful. (This is rare, but not unheard of.) It's also not unreasonable to use the non-null-ness of a pointer as a flag that you have useful information of some sort, and to want to discard that information before you have something else to replace it with. (There are intelligent people who disagree with other intelligent people about whether this indicates a design flaw.) dave -- Dave Vandervies dj3vande at eskimo dot com I like "fun" risks, rather than "lazy" risks. (If I'm going to kill myself, I want to have a good time on the way.) --Graham Reed in the scary devil monastery |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
On Jan 30, 11:25 am, dj3va...@csclub.uwaterloo.ca.invalid wrote:
> In article <47a03c63.2858170...@news.xs4all.nl>, > > Richard Bos <r...@hoekstra-uitgeverij.nl> wrote: > >"Roman Mashak" <m...@tusur.ru> wrote: > > >> is it legal to destroy the memory pointed to by a pointer and then assign > >> NULL to it? Say, like this: > > >> char *ptr; > >> ptr = malloc(10); > >> free(ptr); > >> ptr = NULL; > > >Legal, but useless. > > Not completely useless, only mostly. > > If you know that the pointer you're setting to NULL was the only > pointer into that memory, then it's perfectly safe and useful. > (This is rare, but not unheard of.) > > It's also not unreasonable to use the non-null-ness of a pointer as a > flag that you have useful information of some sort, and to want to > discard that information before you have something else to replace it > with. > (There are intelligent people who disagree with other intelligent > people about whether this indicates a design flaw.) It's UB if you access the value of the pointer after you call free() on it. So you simply can't (in comp.lang.c) do stuff like free(ptr); .... if (ptr) it_wasnt_null(); Yevgen |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
On Jan 31, 5:15am, "Roman Mashak" <m...@tusur.ru> wrote:
> Hello, > > is it legal to destroy the memory pointed to by a pointer and then assign > NULL to it? Say, like this: > > char *ptr; > ptr = malloc(10); > free(ptr); Here, ptr will become a Dangling Pointer. > ptr = NULL; > By this assignment of NULL, ptr will no longer be a dangling pointer. This is a very important step to be followed while freeing . Karthik Balaguru |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
karthikbalaguru wrote:
> On Jan 31, 5:15Âam, "Roman Mashak" <m...@tusur.ru> wrote: >> Hello, >> >> is it legal to destroy the memory pointed to by a pointer and then assign >> NULL to it? Say, like this: >> >> char *ptr; >> ptr = malloc(10); >> free(ptr); > > Here, ptr will become a Dangling Pointer. > >> ptr = NULL; >> > > By this assignment of NULL, ptr will no longer be a dangling pointer. > > This is a very important step to be followed while freeing . This is /sometimes/ an important step. At other times, it's mindblowingly pointless. It depends, for example, on whether there are other ways to get to the pointer variable (if not, assigning null to it is pointless) and whether those other ways bother to check for null (in which case you have problems regardless). In short, there is no royal road to memory management in C. -- Commoner Hedgehog Nit-picking is best done among friends. |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
ymuntyan@gmail.com wrote, On 30/01/08 18:11:
> On Jan 30, 11:25 am, dj3va...@csclub.uwaterloo.ca.invalid wrote: >> In article <47a03c63.2858170...@news.xs4all.nl>, >> >> Richard Bos <r...@hoekstra-uitgeverij.nl> wrote: >>> "Roman Mashak" <m...@tusur.ru> wrote: >>>> is it legal to destroy the memory pointed to by a pointer and then assign >>>> NULL to it? Say, like this: >>>> char *ptr; >>>> ptr = malloc(10); >>>> free(ptr); >>>> ptr = NULL; >>> Legal, but useless. >> Not completely useless, only mostly. >> >> If you know that the pointer you're setting to NULL was the only >> pointer into that memory, then it's perfectly safe and useful. >> (This is rare, but not unheard of.) >> >> It's also not unreasonable to use the non-null-ness of a pointer as a >> flag that you have useful information of some sort, and to want to >> discard that information before you have something else to replace it >> with. >> (There are intelligent people who disagree with other intelligent >> people about whether this indicates a design flaw.) > > It's UB if you access the value of the pointer after you > call free() on it. So you simply can't (in comp.lang.c) > do stuff like > > free(ptr); > ... > if (ptr) > it_wasnt_null(); However it is legal to do: if (something) { free(ptr); ptr = NULL; } ... if (ptr) if_was_still_valid(); -- Flash Gordon; |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
"Chris Dollin" <eh@electrichedgehog.net> wrote in message > karthikbalaguru wrote: > >> Here, ptr will become a Dangling Pointer. >> >> By this assignment of NULL, ptr will no longer be a dangling pointer. >> >> This is a very important step to be followed while freeing . > > This is /sometimes/ an important step. At other times, it's > mindblowingly pointless. It depends, for example, on whether > there are other ways to get to the pointer variable (if not, > assigning null to it is pointless) and whether those other > ways bother to check for null (in which case you have problems > regardless). > > In short, there is no royal road to memory management in C. > No. A good rule is one pointer to each object, but this goes against the principle of structured programming that variables should be local. Also, some data structures, like doubly linked lists, depend on more than one pointer to each node. However if you allocate and destroy whole strucutres in matching creat / free functions, your dangling pointer problems should be minimal. -- Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
karthikbalaguru wrote:
> On Jan 31, 5:15Âam, "Roman Mashak" <m...@tusur.ru> wrote: >> free(ptr); >> ptr = NULL; >> > > By this assignment of NULL, ptr will no longer be a dangling pointer. > > This is a very important step to be followed while freeing . Not necessarily, for example, in many cases the pointer to freed memory is going to immediately go out of scope. -- Army1987 (Replace "NOSPAM" with "email") |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
On Jan 30, 1:01am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
> "Roman Mashak" <m...@tusur.ru> wrote: > > is it legal to destroy the memory pointed to by a pointer and then assign > > NULL to it? Say, like this: > > > char *ptr; > > ptr = malloc(10); > > free(ptr); > > ptr = NULL; > > Legal, but useless. If you think you've found the royal road to memory > management, consider the following: > > char *ptr, &ptr2; > ptr=malloc(10); > ptr2=ptr+2; > free(ptr); > ptr=NULL; > if (ptr+2) crash_hard_and_without_grace(); I don't think there are any magical cures for undefined behavior caused by programming errors. The program may also crash hard and without grace at the free(ptr) function call (or worse, of course). In general, I think that setting a freed pointer to null is a good idea. The fact that it won't cure every sort of pointer illness doesn't negate the small value received in recompense. IMO-YMMV. P.S. I almost always set freed pointers to null myself (the exception being in C++ because it is pointless to set a deleted class member pointer to null in a destructor.) It s with a very small class of problems (but does have one bad side effect -- double frees are less likely to be detected) but overall I think it is a tiny win. |
|
|
|
#16 |
|
Messages: n/a
Hébergeur: |
Hello,
is it legal to destroy the memory pointed to by a pointer and then assign NULL to it? Say, like this: char *ptr; ptr = malloc(10); free(ptr); ptr = NULL; With best regards, Roman Mashak. E-mail: mrv@tusur.ru |
|
|
|
#17 |
|
Messages: n/a
Hébergeur: |
Chris Dollin wrote:
> karthikbalaguru wrote: > >> On Jan 31, 5:15 am, "Roman Mashak" <m...@tusur.ru> wrote: >>> Hello, >>> >>> is it legal to destroy the memory pointed to by a pointer and then >>> assign NULL to it? Say, like this: >>> >>> char *ptr; >>> ptr = malloc(10); >>> free(ptr); >> >> Here, ptr will become a Dangling Pointer. >> >>> ptr = NULL; >>> >> >> By this assignment of NULL, ptr will no longer be a dangling pointer. >> >> This is a very important step to be followed while freeing . > > This is /sometimes/ an important step. At other times, it's > mindblowingly pointless. It depends, for example, on whether > there are other ways to get to the pointer variable (if not, > assigning null to it is pointless) and whether those other > ways bother to check for null (in which case you have problems > regardless). Not quite: you program will crash right there, when dereferencing the NULL pointer, rather than at some other obscure and random place, so this s in detecting the bug. Bye, Jojo |
|
|
|
#18 |
|
Messages: n/a
Hébergeur: |
Army1987 wrote:
> karthikbalaguru wrote: >> On Jan 31, 5:15 am, "Roman Mashak" <m...@tusur.ru> wrote: >>> free(ptr); >>> ptr = NULL; >>> >> >> By this assignment of NULL, ptr will no longer be a dangling pointer. >> >> This is a very important step to be followed while freeing . > Not necessarily, for example, in many cases the pointer to freed > memory is going to immediately go out of scope. In which case there is some work to be done by the optimizer: detect this and drop the assignment Bye, Jojo |
|
|
|
#19 |
|
Messages: n/a
Hébergeur: |
user923005 wrote:
> On Jan 30, 1:01 am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote: <snip> > P.S. > I almost always set freed pointers to null myself (the exception being > in C++ because it is pointless to set a deleted class member pointer > to null in a destructor.) > It s with a very small class of problems (but does have one bad > side effect -- double frees are less likely to be detected) But they won't harm (i.e. possibly crash the program) anymore either. Bye, Jojo |
|
|
|
#20 |
|
Messages: n/a
Hébergeur: |
Joachim Schmitz wrote:
> user923005 wrote: >> On Jan 30, 1:01 am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote: > <snip> >> P.S. >> I almost always set freed pointers to null myself (the exception being >> in C++ because it is pointless to set a deleted class member pointer >> to null in a destructor.) >> It s with a very small class of problems (but does have one bad >> side effect -- double frees are less likely to be detected) > But they won't harm (i.e. possibly crash the program) anymore either. > But the effect can be detrimental on a system (which includes most desktop environments) where the allocator can detect duplicate frees. For that reason, I never set a freed pointer to NULL. -- Ian Collins. |
|
|
|
#21 |
|
Messages: n/a
Hébergeur: |
Ian Collins wrote:
> Joachim Schmitz wrote: >> user923005 wrote: >>> On Jan 30, 1:01 am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote: >> <snip> >>> P.S. >>> I almost always set freed pointers to null myself (the exception >>> being in C++ because it is pointless to set a deleted class member >>> pointer to null in a destructor.) >>> It s with a very small class of problems (but does have one bad >>> side effect -- double frees are less likely to be detected) >> But they won't harm (i.e. possibly crash the program) anymore either. >> > But the effect can be detrimental on a system (which includes most > desktop environments) where the allocator can detect duplicate frees. > > For that reason, I never set a freed pointer to NULL. Same here on the basis that, in C at least, the programmer *must* always be aware of which pointers have valid or invalid values at all points in his program. Anything short of this is going to lead to bugs. From this P.O.V. setting dangling pointers to NULL is redundant. |
|
|
|
#22 |
|
Messages: n/a
Hébergeur: |
"Joachim Schmitz" <nospam.jojo@schmitz-digital.de> writes:
> Chris Dollin wrote: >> karthikbalaguru wrote: >>> On Jan 31, 5:15 am, "Roman Mashak" <m...@tusur.ru> wrote: >>>> is it legal to destroy the memory pointed to by a pointer and then >>>> assign NULL to it? Say, like this: >>>> >>>> char *ptr; >>>> ptr = malloc(10); >>>> free(ptr); >>> >>> Here, ptr will become a Dangling Pointer. >>> >>>> ptr = NULL; >>>> >>> >>> By this assignment of NULL, ptr will no longer be a dangling pointer. >>> >>> This is a very important step to be followed while freeing . >> >> This is /sometimes/ an important step. At other times, it's >> mindblowingly pointless. It depends, for example, on whether >> there are other ways to get to the pointer variable (if not, >> assigning null to it is pointless) and whether those other >> ways bother to check for null (in which case you have problems >> regardless). > Not quite: you program will crash right there, when dereferencing the NULL > pointer, rather than at some other obscure and random place, so this s > in detecting the bug. Probably. There's no guarantee that an attempt to dereference a null pointer will be detected at run time, (though it will be on most systems). -- Keith Thompson (The_Other_Keith) <kst-u@mib.org> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
|
|
#23 |
|
Messages: n/a
Hébergeur: |
santosh wrote, On 31/01/08 08:20:
> Ian Collins wrote: > >> Joachim Schmitz wrote: >>> user923005 wrote: >>>> On Jan 30, 1:01 am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote: >>> <snip> >>>> P.S. >>>> I almost always set freed pointers to null myself (the exception >>>> being in C++ because it is pointless to set a deleted class member >>>> pointer to null in a destructor.) >>>> It s with a very small class of problems (but does have one bad >>>> side effect -- double frees are less likely to be detected) >>> But they won't harm (i.e. possibly crash the program) anymore either. >>> >> But the effect can be detrimental on a system (which includes most >> desktop environments) where the allocator can detect duplicate frees. >> >> For that reason, I never set a freed pointer to NULL. > > Same here on the basis that, in C at least, the programmer *must* always > be aware of which pointers have valid or invalid values at all points > in his program. Anything short of this is going to lead to bugs. From > this P.O.V. setting dangling pointers to NULL is redundant. In some situations setting pointers to NULL can be useful and used to indicate whether it is valid or not. For example, I have some code of this general form... char *ptr = NULL; /* do stuff */ if (cond1) { ptr = malloc(something); /* do stuff with ptr */ if (cond2) { free(ptr); ptr = NULL; } } /* do stuff not using ptr */ if (cond3) { /* do stuff using ptr */ free(ptr); } Note that the code deliberately has only one pointer to the malloc'd block. -- Flash Gordon |
|
|
|
#24 |
|
Messages: n/a
Hébergeur: |
dj3vande@csclub.uwaterloo.ca.invalid wrote:
> In article <47a03c63.2858170722@news.xs4all.nl>, > Richard Bos <rlb@hoekstra-uitgeverij.nl> wrote: > >"Roman Mashak" <mrv@tusur.ru> wrote: > > > >> is it legal to destroy the memory pointed to by a pointer and then assign > >> NULL to it? Say, like this: > >> > >> char *ptr; > >> ptr = malloc(10); > >> free(ptr); > >> ptr = NULL; > > > >Legal, but useless. > > Not completely useless, only mostly. > > If you know that the pointer you're setting to NULL was the only > pointer into that memory, then it's perfectly safe and useful. It's only safe if you know that it's the only copy, but if you know that, I don't see the use. You can just not use that one copy you know about any more. > It's also not unreasonable to use the non-null-ness of a pointer as a > flag that you have useful information of some sort, and to want to > discard that information before you have something else to replace it > with. > (There are intelligent people who disagree with other intelligent > people about whether this indicates a design flaw.) Once, when memory was as rare as the purifier aether and processors had two registers a piece, it may have been useful. Now, with optimising compilers and rarely no registers to spare, just use an extra int. Richard |
|
|
|
#25 |
|
Messages: n/a
Hébergeur: |
Joachim Schmitz wrote:
> Chris Dollin wrote: >> This is /sometimes/ an important step. At other times, it's >> mindblowingly pointless. It depends, for example, on whether >> there are other ways to get to the pointer variable (if not, >> assigning null to it is pointless) and whether those other >> ways bother to check for null (in which case you have problems >> regardless). > Not quite: you program will crash right there, when dereferencing the NULL > pointer, Perhaps. It's not a requirement and I've used systems where it didn't. It wouldn't surprise me if (some) embedded systems didn't read-protect whatever-address-corresponds-to-the-value-of-the-null-pointer, given that they have to shave margins everywhere. Could an embeddey person comment? -- Far-Fetched Hedgehog A rock is not a fact. A rock is a rock. |
|
![]() |
| Outils de la discussion | |
|
|