Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > accessing freed memory without error

Reply
Thread Tools

accessing freed memory without error

 
 
sachin_mzn@yahoo.com
Guest
Posts: n/a
 
      01-12-2005
Hi,

Why I am not getting any run time error while accessing a freed memory
in following code. This is printing h in std output.

#include<stdio.h>
main()
{
char* buffer = (char*)malloc(6);
strcpy(buffer,"hello");
free(buffer);
printf("buffer=%c\n", *buffer);
}

-Sachin

 
Reply With Quote
 
 
 
 
buda
Guest
Posts: n/a
 
      01-12-2005
<> wrote in message
news: ups.com...
> Hi,
>
> Why I am not getting any run time error while accessing a freed memory
> in following code. This is printing h in std output.
>
> #include<stdio.h>
> main()
> {
> char* buffer = (char*)malloc(6);
> strcpy(buffer,"hello");
> free(buffer);
> printf("buffer=%c\n", *buffer);
> }


You've entered the realm of undefined behaviour with the printf, and
anything at all (including printing h on stdout) can happen. Simply don't do
it.


 
Reply With Quote
 
 
 
 
dandelion
Guest
Posts: n/a
 
      01-12-2005

<> wrote in message
news: ups.com...
> Hi,
>
> Why I am not getting any run time error while accessing a freed memory
> in following code.


C does not have any "run-time-errors". Accessing freed (i.e. non-allocated)
memory results in undefined behavior. If pink rabbits would jump out of your
computer, there would still be nothing wrong as far as the standard is
concerned.

> This is printing h in std output.
>
> #include<stdio.h>
> main()
> {
> char* buffer = (char*)malloc(6);
> strcpy(buffer,"hello");
> free(buffer);


> printf("buffer=%c\n", *buffer);


If that would print the content of the last issue of PlayGirl, it would be
right, too.
> }


Checkout the FAQ.

http://www.eskimo.com/~scs/C-faq/q7.20.html


 
Reply With Quote
 
Taran
Guest
Posts: n/a
 
      01-12-2005

dandelion wrote:
> C does not have any "run-time-errors". Accessing freed (i.e.

non-allocated)
> memory results in undefined behavior. If pink rabbits would jump out

of your
> computer, there would still be nothing wrong as far as the standard

is
> concerned.
> If that would print the content of the last issue of PlayGirl, it

would be
> right, too.
> > }


ROTFL. Pink Rabbits (?), PlayGirl(!) issue...

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-12-2005
<> wrote in message
news: ups.com...
> Hi,
>
> Why I am not getting any run time error while accessing a freed memory
> in following code.


Because your program's behavior is undefined. This means
that from the language perspective, it could do absolutely
anything at all, from 'seems to work', to a crash, anything
in between, or something else.

> This is printing h in std output.


It also might have caused the monitor to fall of the desk.
Moral: Don't Do That.

>
> #include<stdio.h>


#include <stdlib.h> /* declares 'malloc()' and 'free()' */
#include <string.h> /* declares 'strcpy()' */

> main()


int main(void)

> {
> char* buffer = (char*)malloc(6);


You forgot to check if 'malloc()' succeeded. If it
failed, it returns NULL, in which case the call
to 'strcpy()' would give undefined behavior, because
it would try to dereference a null pointer.

Also, your casting of 'malloc()'s return value hides a
serious error: There's no prototype for 'malloc()' in
scope, which will cause a C89 compiler to assume it
returns type 'int'. So your cast tries to convert
a pointer type to an integer type. The language
does not define such a conversion. This conversion
is implementation-defined, but in any case it will
very likely corrupt the returned pointer value, in
which case you have more undefined behavior.

> strcpy(buffer,"hello");
> free(buffer);
> printf("buffer=%c\n", *buffer);


return 0;

> }


It's been a while since I've seen so many errors in such
a small piece of code.

-Mike


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-12-2005
wrote:
> Hi,
>
> Why I am not getting any run time error while accessing a freed memory
> in following code.


Dumb luck. Just as using malloc() and free() without the prototypes
found in <stdlib.h> and using strcpy without the prototype <string.h>
gave you no error.

> This is printing h in std output.


Not for me. A version of your program with other errors corrected
follows yours; note that the output is not what you claim.
>
> #include<stdio.h>
> main()
> {
> char* buffer = (char*)malloc(6);
> strcpy(buffer,"hello");
> free(buffer);
> printf("buffer=%c\n", *buffer);
> }


#include <stdio.h>
#include <stdlib.h> /* note */
#include <string.h> /* note */

int main(void)
{
char *buffer = malloc(6); /* note */
if (!buffer) {
fprintf(stderr, "malloc failed.\n");
exit(EXIT_FAILURE);
}
strcpy(buffer, "hello");
free(buffer);
printf("buffer=%c\n", *buffer); /* impromper use of pointer */
return 0;
}

[output]
buffer=

BTW: please note the other threads on preserving indenting when using
google.com. You can follow those suggestions or -- even better -- stop
using google for posting.
 
Reply With Quote
 
dandelion
Guest
Posts: n/a
 
      01-13-2005

"Taran" <> wrote in message
news: oups.com...
>
> dandelion wrote:
> > C does not have any "run-time-errors". Accessing freed (i.e.

> non-allocated)
> > memory results in undefined behavior. If pink rabbits would jump out

> of your
> > computer, there would still be nothing wrong as far as the standard

> is
> > concerned.
> > If that would print the content of the last issue of PlayGirl, it

> would be
> > right, too.
> > > }

>
> ROTFL. Pink Rabbits (?), PlayGirl(!) issue...


Hey! Can't a girl have some fun, too? .


 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      01-13-2005
On Wed, 12 Jan 2005 18:13:15 +0000, Mike Wahler wrote:

> <> wrote in message
> news: ups.com...


....

> int main(void)
>
>> {
>> char* buffer = (char*)malloc(6);

>
> You forgot to check if 'malloc()' succeeded. If it
> failed, it returns NULL, in which case the call
> to 'strcpy()' would give undefined behavior, because
> it would try to dereference a null pointer.
>
> Also, your casting of 'malloc()'s return value hides a
> serious error: There's no prototype for 'malloc()' in
> scope, which will cause a C89 compiler to assume it
> returns type 'int'. So your cast tries to convert
> a pointer type to an integer type.


Well, an int to a pointer. But that's not the problem. The code invokes
undefined behaviour because it tries to call a function with a type that
is not compatible with the function's definition. So the code has left the
rails before the cast is executed.

Lawrence
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-13-2005

"dandelion" <> wrote in message
news:41e6464e$0$189$4all .nl...
>
> "Taran" <> wrote in message
> news: oups.com...
> >
> > dandelion wrote:
> > > C does not have any "run-time-errors". Accessing freed (i.e.

> > non-allocated)
> > > memory results in undefined behavior. If pink rabbits would jump out

> > of your
> > > computer, there would still be nothing wrong as far as the standard

> > is
> > > concerned.
> > > If that would print the content of the last issue of PlayGirl, it

> > would be
> > > right, too.
> > > > }

> >
> > ROTFL. Pink Rabbits (?), PlayGirl(!) issue...

>
> Hey! Can't a girl have some fun, too? .


In that case, I'd think you'd want not the 'content',
but the pictures.

"Honey, I buy 'em for the articles, honest!"



-Mike


 
Reply With Quote
 
Krishanu Debnath
Guest
Posts: n/a
 
      01-13-2005
Lawrence Kirby wrote:

>On Wed, 12 Jan 2005 18:13:15 +0000, Mike Wahler wrote:
>
>
>
>><> wrote in message
>>news: roups.com...
>>
>>

>
>....
>
>
>
>>int main(void)
>>
>>
>>
>>>{
>>>char* buffer = (char*)malloc(6);
>>>
>>>

>>You forgot to check if 'malloc()' succeeded. If it
>>failed, it returns NULL, in which case the call
>>to 'strcpy()' would give undefined behavior, because
>>it would try to dereference a null pointer.
>>
>>Also, your casting of 'malloc()'s return value hides a
>>serious error: There's no prototype for 'malloc()' in
>>scope, which will cause a C89 compiler to assume it
>>returns type 'int'. So your cast tries to convert
>>a pointer type to an integer type.
>>
>>

>
>Well, an int to a pointer. But that's not the problem. The code invokes
>

Just wondering what will happen in a 64 bit m/c? 32 bit integer may yield a
junk 64 bit pointer value. Because 64 bit return value of malloc has now
truncated to 32 bit integer.

>undefined behaviour because it tries to call a function with a type that
>is not compatible with the function's definition. So the code has left the
>rails before the cast is executed.
>
>Lawrence
>
>

Krishanu

 
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
destroy primitive/object types but memory is not freed george972@mailinator.com C Programming 5 05-25-2009 10:44 PM
why 50% of allocated memory is not freed? MN C Programming 32 03-11-2009 03:40 PM
Detecting freed memory Rob C Programming 6 09-01-2005 07:39 PM
How to know the memory pointed by a ptr is freed? ravi C Programming 72 09-14-2004 01:13 PM
use delete to destroy primitive/object types but memory is not freed jimjim C Programming 28 04-13-2004 11:34 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