Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Please clear my doubt

Reply
Thread Tools

Please clear my doubt

 
 
Yang Lee
Guest
Posts: n/a
 
      11-30-2003
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
 
Reply With Quote
 
 
 
 
Robert Stankowic
Guest
Posts: n/a
 
      11-30-2003

"Yang Lee" <> schrieb im Newsbeitrag
news:4efb538a10554ccab995e7b8df06c768.93862@mygate .mailgate.org...
> 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);


You don't free _pointer b_, you free _the memory it points_ to.
from this moment the value in b as well as in a is no longer valid and even
the statement

a;
or
b;

invokes undefined behavior [OT](for instance, a segment-descriptor in
certain CPU's may no longer exist and even loading a or b into a
CPU-register may cause a crash)[/OT]

>
> 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?


Undefined behavior means everything can happen, your printf in your case
just happens to work by accident, it could as well format your harddisk or
put haircream into your toothpaste

should I write
> a=NULL;


Good idea (IMHO)
Dereferencing a NULL pointer is more likely to crash your application
immidiately, telling you that somethimg is messed up.
And by accident free()ing a NULL pointer does no harm, it's just a no-op.

HTH
Robert


 
Reply With Quote
 
 
 
 
James Hu
Guest
Posts: n/a
 
      11-30-2003
On 2003-11-30, Yang Lee <> wrote:
> char *a,*b;
> a=(char *)malloc(10);
> b=a; /*******/
> strcpy(a,"gates");
> printf("%s",b);


Probably you should use:

printf("%s\n", 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.


Since b and a point to the same object prior to the call to free, then
they both point to freed space after the call to free.

> Also
>
> a=(char *)malloc(10);
> strcpy(a,"gates");
> free(a);
> printf("%s",a);


See the printf comment above.

> this still prints "gates " why is this happening even after freeing
> the memory? should I write
>
> a=NULL;


You should never read from or write to an object whose lifetime has
expired. In the case of allocated objects, their lifetime expires after
the call to free. Compare this with automatic objects, whose lifetime
expires when execution reaches the end of the their associated statement
block.

Why you see the "gates" output is not really relevant here, since you
are invoking undefined behavior. One possible answer is that
it is the nature of your C library implementation to not disturb the
contents of freed memory until the memory is allocated and written to
for a different purpose. However, it is a programming error to invoke
undefined behavior.

In almost all cases, a program can not reliably exploit this "do not
disturb" behavior, even if the programmer so wishes. Considering your
example, printf itself may allocate memory and write something for its
own purpose there. Then your snippet above may actually cause the
program to crash.

-- James
 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      11-30-2003
"Yang Lee" <> wrote:
> I have two char pointers
>
> char *a,*b;
> a=(char *)malloc(10);
> b=a; /*******/
>
> so both pointers are looking at same mamory location


It's rude to point, especially at mammary locations.

> strcpy(a,"gates");
> printf("%s",b);
>
> Now I free pointer b;
> free(b);


All good so far. Now the value of both a and b is no longer valid.

> 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.


Pointer a and b both store the same memory address. You have
freed the memory at that address, so neither a nor b are now
valid. You must not attempt to free(a) now.

> Also
>
> a=(char *)malloc(10);
> strcpy(a,"gates");
> free(a);
> printf("%s",a);


Whoops -- you just used the invalid pointer value in a, so
your program has undefined behaviour. Anything could happen.

> this still prints "gates " why is this happening
> even after freeing the memory?


Anything could happen, including the possibility that the
memory still contains what was in it before -- for now --
but it may get reallocated or written over, some time in
the future, so you should not rely on this behaviour.

> should I write a=NULL;


Only if setting it to a null pointer makes sense in your
particular case. If you have finished with the variable
then there is no need to change its value. If you intend
to re-use the variable then it must be set to a valid
pointer value at some point; if you can be sure that you
will not attempt to read from it before that point, you
can leave it alone until you set it to a correct value.

--
Simon.


 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      12-01-2003
In <0cudnbz-FKXiIVSiRVn-> James Hu <> writes:

>On 2003-11-30, Yang Lee <> wrote:
>> char *a,*b;
>> a=(char *)malloc(10);
>> b=a; /*******/
>> strcpy(a,"gates");
>> printf("%s",b);

>
>Probably you should use:
>
> printf("%s\n", 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.

>
>Since b and a point to the same object prior to the call to free, then
>they both point to freed space after the call to free.


That's hard to tell, considering that their values become indeterminate
after the free() call. There is nothing in the standard preventing the
implementation from turning both of them into null pointers after the
free call, for example.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email:
 
Reply With Quote
 
Mac
Guest
Posts: n/a
 
      12-02-2003
On Sun, 30 Nov 2003 08:55:00 +0000, Yang Lee wrote:

> 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.
>


Pointer a will not be in existence, now. You MUST NOT use it. The same
goes for pointer b. Technically, the pointers still exist, but they no
longer point to memory you can use or inspect.

> 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?


After freeing, you are not supposed to touch or even look at the memory.
In your case, I guess the memory was unchanged, but this is not guaranteed.

> should I write
> a=NULL;
>


I think it is a good practice to set a (and b) = NULL, after freeing. This
gives you your best chance of detecting any accidental access. But you
don't have to if you don't want to.

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


Mac
--

 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      12-02-2003
Dan Pop <> spoke thus:

> That's hard to tell, considering that their values become indeterminate
> after the free() call. There is nothing in the standard preventing the
> implementation from turning both of them into null pointers after the
> free call, for example.


Wait, what? Are you saying that

int *a;
a=malloc(sizeof *a); /* check against NULL omitted */
printf( "a=%p\n", (void *)a ); /* let's say it prints 0x012345 */
free( a );
if( a==NULL )
printf( "a is NULL\n" );

that the second printf has a chance in hell of being called?
Undoubtedly I've failed to "engage my brain," but I have no idea what
you're talking about.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      12-02-2003
Christopher Benson-Manica <> spoke thus:

> free( a );
> if( a==NULL )

^^^^^^^^^^^^^

Let me guess, UB?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      12-02-2003
Christopher Benson-Manica <> scribbled the following:
> Christopher Benson-Manica <> spoke thus:
>> free( a );
>> if( a==NULL )

> ^^^^^^^^^^^^^


> Let me guess, UB?


Yes. (Or not necessarily, if a was originally NULL, in which case it
would be a comparison NULL==NULL, which is perfectly safe.)
But free() can't alter its argument anyway (no C function can), so the
if statement is pointless.

--
/-- Joona Palaste () ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'It can be easily shown that' means 'I saw a proof of this once (which I didn't
understand) which I can no longer remember'."
- A maths teacher
 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      12-03-2003
"Christopher Benson-Manica" <> wrote:
> > free( a );
> > if( a==NULL )

> ^^^^^^^^^^^^^
>
> Let me guess, UB?


But you can avoid that UB:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
int *a;
unsigned char b[sizeof a];
a = malloc(sizeof *a);
if(a != NULL)
{

/* make b hold object representation of valid pointer */
memcpy(b, &a, sizeof a);

/* on call to free, pointer value becomes invalid */
free(a);

/* but I can still compare the object representation */
if(memcmp(&a, b, sizeof a) != 0)
printf("the object representation of a has changed\n");
}
return 0;
}

By the function call semantics of C, I do not believe that
the object representation of a can be changed by the
call to free.

--
Simon.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dotnet doubt can any body clarify my doubt challa462@gmail.com ASP .Net 0 08-22-2012 06:02 AM
Response.Clear() doesn't clear David ASP .Net 2 01-31-2008 08:32 PM
Unrecognized element 'add' after <clear></clear> InvalidLastName ASP .Net Web Services 3 03-06-2007 03:07 AM
doubt about doubt Bob Nelson C Programming 11 07-30-2006 08:17 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57