Re: How to know the memory pointed by a ptr is freed?
From: Flash Gordon (spam_at_flash-gordon.me.uk)
Date: 09/13/04
- Next message: Flash Gordon: "Re: speed of int vs bool for large matrix"
- Previous message: ala: "Re: How to know the memory pointed by a ptr is freed?"
- In reply to: ala: "Re: How to know the memory pointed by a ptr is freed?"
- Next in thread: ala: "Re: How to know the memory pointed by a ptr is freed?"
- Reply: ala: "Re: How to know the memory pointed by a ptr is freed?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 13 Sep 2004 12:47:17 +0100
On Mon, 13 Sep 2004 10:29:40 GMT
ala <aa@ala.ala> wrote:
> On Sun, 12 Sep 2004 11:12:10 +0100, Flash Gordon
> <spam@flash-gordon.me.uk> wrote:
>
> >On Sun, 12 Sep 2004 09:27:50 GMT
> >ala <aa@ala.ala> wrote:
> >
> >> On Sun, 12 Sep 2004 09:22:01 GMT, ala <aa@ala.ala> wrote:
> >>
> >> >On 17 Aug 2004 02:29:09 -0700, "ravi" <ec_au_ravi2000@yahoo.com>
> >> >wrote:
> >> >
> >> >>I have a situation where i want to free the memory pointed by a
> >> >>pointer, only if it is not freed already. Is there a way to know
> >> >>whether the memory is freed or not?
> >> >
> >> in the borland c 5.3 compiler *seems*
> >> p=malloc(size);
> >> <code_sinp>
> >> if(p==0 || ( ((unsigned*)p)[-1]&0x1 ) ) printf("not allocated")
> >> else printf("allocated");
> >
> >For all you know this might only work on Sundays. It is undefined
> >behaviour and as has been discussed here there is NO standard way to
> >determine if the object a pointer points to has been freed or not.
> >
> >In fact, even with your specific implementation I am willing to bet
> >that there are situations where (unsigned*)p)[-1] would actually
> >point in to another object.
>
> it is easy to speak (but if p!=0 seems I have right). Find an exaple
> where it doesn't work
If you send me your computer system I'll do that. However, my machine is
different so it behaves differently when undefined behaviour is invoked.
The standard says that what you are doing is undefined therefor
literally anything can happen.
Anyone recommending doing the sort of thing you are suggesting is either
very ignorant about C or very idiotic. However, just to give you one
example that could cause your test to fail (although it might not since
undefined behaviour is undefined and therefor anything can happen):
{
unsigned *q = malloc(sizeof *q);
unsigned *p = malloc(sizeof *p);
free(p);
free(q);
q = malloc(2 * sizeof *p);
q[0]=1;
q[1]=1;
}
One way in which this could cause your test to fail is if the second
time space is malloced for q it uses the space immediately before where
p was allocated as part of its allocation. This would be a perfectly
reasonable thing for the malloc implementation to do.
NEVER rely on looking outside the memory that your implementation tells
you is yours.
-- Flash Gordon Sometimes I think shooting would be far too good for some people. Although my email address says spam, it is real and I read it.
- Next message: Flash Gordon: "Re: speed of int vs bool for large matrix"
- Previous message: ala: "Re: How to know the memory pointed by a ptr is freed?"
- In reply to: ala: "Re: How to know the memory pointed by a ptr is freed?"
- Next in thread: ala: "Re: How to know the memory pointed by a ptr is freed?"
- Reply: ala: "Re: How to know the memory pointed by a ptr is freed?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|