Please clear my doubt

From: Yang Lee (yang__lee_at_ausi.com)
Date: 11/30/03


Date: Sun, 30 Nov 2003 08:55:00 +0000 (UTC)

Hi All,
I have two char pointers

char *a,*b;
a=(char *)malloc(10);
b=a; /*******/

so both pointers are looking at same mamory location

strcpy(a,"gates");
printf("%s",b);

Now I free pointer b;
free(b);

Then will pointer a be in existance or it will be also freed.
or if I free pointer a then will b point to same location.

Also

a=(char *)malloc(10);
strcpy(a,"gates");
free(a);
printf("%s",a); this still prints "gates " why is this happening
even after freeing the memory? should I write
a=NULL;

Please help my simple querries as it will help me a lot.

regards Lee

-- 
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG


Relevant Pages

  • Re: Please clear my doubt
    ... > so both pointers are looking at same mamory location ... > Then will pointer a be in existance or it will be also freed. ... > or if I free pointer a then will b point to same location. ... And by accident freeing a NULL pointer does no harm, ...
    (comp.lang.c)
  • Re: Please clear my doubt
    ... > so both pointers are looking at same mamory location ... > Then will pointer a be in existance or it will be also freed. ... > or if I free pointer a then will b point to same location. ... longer point to memory you can use or inspect. ...
    (comp.lang.c)
  • Re: Please clear my doubt
    ... > or if I free pointer a then will b point to same location. ... Pointer a and b both store the same memory address. ... your program has undefined behaviour. ...
    (comp.lang.c)
  • Re: Program repeats itself, pointer trouble I suspect.
    ... I'm using an old Borland Turbo C++ for DOS Compiler to write ... If you don't intialize a pointer, but you declared it mainand just ... Does this present a problem after the program terminates? ... Can two char pointers in an array of pointers point to each other ...
    (comp.lang.c)
  • Re: if (pointer) ?
    ... Back in prehistory in PRIMOS C char pointers were 48 bits whereas ... I've always assumed that since 0 is a null pointer constant, ... avoid for fear of breaking other things. ...
    (comp.lang.c)