Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

I want to ask a question...

35 views
Skip to first unread message

Ramine

unread,
Aug 7, 2015, 2:53:47 PM8/7/15
to
Hello,


I want to ask a question...


Suppose that you are using a pointer that points to an allocated chunk
of memory, and suppose that you deallocated this chunk of memory and
you have keeped the old address of this pointer inside another pointer ,
is there a problem to access the old address of the pointer and content
of the memory even if it was deallocated ?

I am experimenting with that inside FreePascal compiler and
it doesn't give a problem.. can you confirm that there is or
there is no problem ?


Thank you,
Amine Moulay Ramdane.

Ramine

unread,
Aug 7, 2015, 3:06:49 PM8/7/15
to


Hello,

I have just received an answer from Gareth Owen from the
comp.lang.c forum:

Here it is:

"You can access the pointer itself, but you can't access the memory it
points to. Sometimes it might work - and it simple programs it probably
will work more ofhen than not - but you can't rely on it.

Don't do that."



So i will not do that.

red floyd

unread,
Aug 7, 2015, 3:07:32 PM8/7/15
to
On 8/7/2015 2:53 PM, Ramine wrote:
> Hello,
>
>
> I want to ask a question...
>
>
> Suppose that you are using a pointer that points to an allocated chunk
> of memory, and suppose that you deallocated this chunk of memory and
> you have keeped the old address of this pointer inside another pointer ,
> is there a problem to access the old address of the pointer and content
> of the memory even if it was deallocated ?
>

It's undefined behavior. Which means that anything can happen, ranging
from "appears to work correctly" to segfaults to "reformats your hard
drive and installs Windows Vista".



Alf P. Steinbach

unread,
Aug 7, 2015, 3:09:41 PM8/7/15
to
On 07-Aug-15 11:53 PM, Ramine wrote:
> Hello,
>
>
> I want to ask a question...
>
>
> Suppose that you are using a pointer that points to an allocated chunk
> of memory, and suppose that you deallocated this chunk of memory and
> you have keeped the old address of this pointer inside another pointer ,
> is there a problem to access the old address of the pointer and content
> of the memory even if it was deallocated ?

Yes.


> I am experimenting with that inside FreePascal compiler and
> it doesn't give a problem.. can you confirm that there is or
> there is no problem ?

One common effect of Undefined Behavior is that what one naively expects
will happen, happens, but possible problems include

* That there is hardware-supported checking of pointer values,
generating some trap.

* The the relevant memory page is no longer in use, again with
a possible trap (hardware exception) as result.

* That the code changes memory that is now used for something else.

* That the code incorrectly uses data that has been changed due to
the memory block being used for something else.

* That the C or C++ compiler emits code that detects the situation.

On the PC platform, which seems to be indicated by "FreePascal
compiler", the first point is not so likely, because everything is set
up to use a common single large segment for code, data and stack, and
offsets in one big segment are not so easy to check for validity.


Cheers & hth.,

- Alf

--
Using Thunderbird as Usenet client, Eternal September as NNTP server.

Vir Campestris

unread,
Aug 7, 2015, 4:57:10 PM8/7/15
to
The problem comes when some other part of the program allocates memory.
It's quite likely to get the bit you freed, in which case you now have
two parts of the program writing different things into the same place.

If you're on Linux Valgrind can help catch these things. ISTR Visual
Studio has some tools too.

Andy
0 new messages