473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

accessing freed memory without error

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

Nov 14 '05 #1
14 1607
<sa********@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro 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.
Nov 14 '05 #2

<sa********@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro 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
Nov 14 '05 #3

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

Nov 14 '05 #4
<sa********@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro 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
Nov 14 '05 #5
sa********@yahoo.com 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.
Nov 14 '05 #6

"Taran" <ta************@honeywell.com> wrote in message
news:11**********************@f14g2000cwb.googlegr 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? ;-).
Nov 14 '05 #7
On Wed, 12 Jan 2005 18:13:15 +0000, Mike Wahler wrote:
<sa********@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro 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
Nov 14 '05 #8

"dandelion" <da*******@meadow.net> wrote in message
news:41*********************@dreader10.news.xs4all .nl...

"Taran" <ta************@honeywell.com> wrote in message
news:11**********************@f14g2000cwb.googlegr 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
Nov 14 '05 #9
Lawrence Kirby wrote:
On Wed, 12 Jan 2005 18:13:15 +0000, Mike Wahler wrote:
<sa********@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googleg 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

Nov 14 '05 #10
On Thu, 13 Jan 2005 21:53:36 +0530, Krishanu Debnath wrote:

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


It is impossible to say what will happen. There's no requrement that an
int and a pointer return value even be returned in the same place. For
example 68000 series processors have different data and address registers
so an integer value might be returned in D0 and a pointer in A0. Code that
casts from the wrong return type will likely end up looking in the wrong
place completely for the value.

Lawrence
Nov 14 '05 #11
On Wed, 12 Jan 2005 18:13:15 GMT, "Mike Wahler"
<mk******@mkwahler.net> wrote:
<sa********@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com... <other good comments snipped>
char* buffer = (char*)malloc(6);

<snip> 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
You mean integer to pointer.
does not define such a conversion. This conversion
is implementation-defined, but in any case it will
Right so far.
very likely corrupt the returned pointer value, in
which case you have more undefined behavior.

Actually if probability is measured over platforms, it "very likely"
will work "accidentally" -- on most platforms pointers are just
addresses and addresses are really integers. But it isn't required to
work by the standard, and you shouldn't rely on it.

The implicit misdeclaration also provides two (more) sources of
Undefined Behavior. malloc actually returns a pointer (void *) while
the calling code expects it to return an int, which is then converted
as discussed already. And its parameter is size_t, but the actual
argument here is int, and without the prototype declaration isn't
automatically converted. It is not required that the mechanisms for
returning and passing different types be the same, but again, on many
platforms they are and this "accidentally" works, although I would say
not quite as often as the conversion. And certainly not guaranteed.

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #12
Mike Wahler wrote:
<sa********@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro 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.


It's worse than that, it is guaranteed to be undefined behaviour because
malloc returns a void* but the OP has effectively lied to the compiler
by making it assume an int is returned, there is no integer to pointer
conversion invoked. It is perfectly reasonable and valid for and int to
be returned in one register and a pointer in a different register,
something that some implementations definitely do. So this could cause
some random value that happens to be in a data register to be converted
to a pointer whilst the actual return value is completely ignored.
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. :-)


Yes, it was horrendous, and very similar to lots of other such examples
I've seen posted asking the same question.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #13
Dave Thompson wrote:
[concerning the use of malloc() without a declaration]

The implicit misdeclaration also provides two (more) sources of
Undefined Behavior. malloc actually returns a pointer (void *) while
the calling code expects it to return an int, which is then converted
as discussed already. And its parameter is size_t, but the actual
argument here is int, and without the prototype declaration isn't
automatically converted. It is not required that the mechanisms for
returning and passing different types be the same, but again, on many
platforms they are and this "accidentally" works, although I would say
not quite as often as the conversion. And certainly not guaranteed.


The not-really-`int' to pointer conversion is particularly
likely to produce garbage on an implementation where `int' is
narrower than `void*'. Implementations with 32-bit `int' and
64-bit `void*' are not difficult to find.

Similarly, the failure to convert `int' to `size_t' is
likely to garble the argument on implementations where `size_t'
is wider than `int', particularly if the system uses something
like a "register pair" to hold the wider quantity. And once
again, such systems are not hard to find: 32-bit `int' and 64-bit
`size_t' is a fairly common combination.

In short, I agree with Dave's "certainly not guaranteed,"
except that I would have disouraged the practice even more
strongly. Fortunately, C99 offers the strongest discouragement
of all: Calling malloc() without a prior declaration is now a
flat-out compile-time error, requiring a diagnostic.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 14 '05 #14
bd
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

sa********@yahoo.com wrote:
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);
}


The effects of accessing freed memory are undefined. This means anything can
happen, including nothing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)

iD8DBQFB7Bmx+hz2VlChukwRAuvUAKC0oI2YJBXOxsWO4rAzSq wer8/YtQCfVBfR
lIVTC8MC9YidBIuG9GPcErg=
=TQbd
-----END PGP SIGNATURE-----
Nov 14 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

73
by: David Scarlett | last post by:
Is the following code safe? SomeType *a, *b; /* Some code.... */ free(a);
72
by: ravi | last post by:
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?
5
by: Thor W Hammer | last post by:
Hello all, Is it possible to find out if a script is freed? This is actual when having a pointer to a function and should determine if it is freed so we don't call it and get error.. TWH
13
by: lovecreatesbeauty | last post by:
/* How do free() know how many elements should be freed in a dynamic array? When free a single variable, the amount of byte of memory can be retrieved from the type of variable itself. ...
1
by: lars.uffmann | last post by:
Hello everyone! I just debugged a pretty huge project, eliminating basically every memory leak that would occur with the current configuration files, at least according to the mtrace() tool from...
10
by: frakie | last post by:
Hi 'body, is there a method to check if a pointer is pointing a freed memory location?
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.