Re: dereferencing memory location some time after deleting the same one
From: fabio de francesco (fmdf_at_tiscali.it)
Date: 10/21/04
- Next message: fabio de francesco: "Re: dereferencing memory location some time after deleting the same one"
- Previous message: Sumit Nagpal: "Re: dynamically instantiate a class"
- In reply to: Victor Bazarov: "Re: dereferencing memory location some time after deleting the same one"
- Next in thread: Victor Bazarov: "Re: dereferencing memory location some time after deleting the same one"
- Reply: Victor Bazarov: "Re: dereferencing memory location some time after deleting the same one"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 20 Oct 2004 17:27:37 -0700
Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news:<Lsydd.5990$Ae.3850@newsread1.dllstx09.us.to.verio.net>...
> fabio de francesco wrote:
> > what do you think of the following? Why are we permitted to do that?
>
> Why not? The Standard says that any program that attempts to dereference
> a pointer after the pointer has been deleted, has undefined behaviour.
> An attempt to define what should happen in such case is a waste of time.
I am sorry for my English because sometimes I am not able to explain
what the it is the exact point of my question: I already know that the
behaviour is undefined as the Standard says. What I intended to ask is
why the Standard say that. I think that Standard would be better not
to allow dereferencing a pointer to deleted memory for the reasons I
wrote.
>
> > And why the C++ Library doesn't stop someone willing to perfom that
> > assignement (*a = 20)?
>
> How would it "stop" you?
Ok, let me try a way. I don't have enough IT competence to suggest the
best solution but I start from knowing that the Library takes pages of
memory from the kernel VM and manages the allocation of little pieces
of that to programs requiring bits of that memory. Is it true, isn't
it? So the responsibility for finer allocation of memory is in charge
of the Library that eventually could mark deleted locations as
unavailable and recollect them. May be this is not the right way but I
just tried to answer your question.
> >
> >
> > #include <iostream>
> >
> > using std::cout;
> >
> > int main()
> > {
> > int *a = new int(10);
> > cout << *a << " " << a << '\n';
> > delete a;
> > *a = 20;
> > cout << *a << " " << a << '\n';
> > }
> >
> >
> > After compiling (with gcc-3.4.1 on Linux and with VC++ on XP as well):
> >
> > #./a.out
> > 10 0x9f6d008
> > 20 0x9f6d008
>
> Undefined behaviour. Anything is allowed to happen.
>
> >
> >
> > Maybe what is worse is that if you compile and run the above written
> > code without the line "*a = 20;" you get the following output:
> >
> > #./a.out
> > 10 0x9f6d008
> > 0 0x9f6d008
>
> Again, it really shows nothing. The behaviour of that code is
> not defined. It is allowed to produce _any_ output or no output
> whatsoever.
That has been said before.
>
> >
> > I didn't know that this is the behaviour until I read a post on
> > it.comp.lang.c++ from someone who didn't understand why He got the
> > printed list of all the nodes of a bin-tree with a "0" output at the
> > position of some previously deleted nodes. So He didn't realize that
> > He forgot to assign NULL to the parent pointer field addressing the
> > just deleted nodes (and unfortunatelly all the deleted nodes were leaf
> > ones).
>
> OK
>
> >
> > I think that if He had got a crash, dereferencing a pointer to
> > deallocated memory, He would had understood what was bad with his
> > cancelling algorithm.
>
> He could get a crash. Or he could get a thank-you note in the mail.
> Or he could get his hard drive formatted. Anything is allowed to
> happen.
I think it shouldn't be allowed to happen anything not specified, for
the sake of safety.
> >
> > So wouldn't it be better if a program crashed when someone tried to
> > dereference a pointer to deleted memory location?
>
> No, it wouldn't. Forcing the program to crash would require some
> special processing. Besides, requiring the program to crash would
> only permit creation of C++ programs for systems where "crash" is
> defined. What if the system _cannot_ (or must not) crash? Even
> debugging such system would be a problem.
I don't know. What does the standard defines when a program tries to
access a memory location allocated to another process?
> V
Thank you,
Fabio De Francesco
- Next message: fabio de francesco: "Re: dereferencing memory location some time after deleting the same one"
- Previous message: Sumit Nagpal: "Re: dynamically instantiate a class"
- In reply to: Victor Bazarov: "Re: dereferencing memory location some time after deleting the same one"
- Next in thread: Victor Bazarov: "Re: dereferencing memory location some time after deleting the same one"
- Reply: Victor Bazarov: "Re: dereferencing memory location some time after deleting the same one"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|