Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why would malloc() modify the behavior of this output?

Reply
Thread Tools

Why would malloc() modify the behavior of this output?

 
 
grocery_stocker
Guest
Posts: n/a
 
      05-11-2005
If go like the following:

#include <stdlib.h>

void *test_me(int a) {
int *m;
m = &a;
}

main(void){
char *j;

j = test_me(1);

printf("The value of j is : %x", *j);

}

I get:
The value of j is : 1


However, when I add malloc()

#include <stdlib.h>

void *test_me(int a) {
int *m;
m = &a;
}

main(void){
char *p;
char *j;

p = malloc(10);
j = test_me(1);

printf("The value of p is : %x", *p);
printf("The value of j is : %x", *j);

free(p);
}

I get the following:
The value of p is : 0
The value of j is: ffffffc4

Why does p become zero?

 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      05-11-2005
grocery_stocker wrote:
> If go like the following:
>
> #include <stdlib.h>
>
> void *test_me(int a) {
> int *m;
> m = &a;


Please notice that you're not returning anything from this function,
though its signature indicates a pointer to void will be returned.
Undefined behavior. All bets are off.

> }
>
> main(void){
> char *j;
>
> j = test_me(1);


Again, since test_me() fails to return anything, the value of `j' is
indeterminate.
>
> printf("The value of j is : %x", *j);
>
> }
>
> I get:
> The value of j is : 1
>
>
> However, when I add malloc()
>
> #include <stdlib.h>
>
> void *test_me(int a) {
> int *m;
> m = &a;
> }
>
> main(void){
> char *p;
> char *j;
>
> p = malloc(10);
> j = test_me(1);
>
> printf("The value of p is : %x", *p);
> printf("The value of j is : %x", *j);
>
> free(p);
> }
>
> I get the following:
> The value of p is : 0
> The value of j is: ffffffc4
>
> Why does p become zero?
>

Undefined behavior means the results could be *anything* -- even nasal
demons might ensue.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
 
Reply With Quote
 
 
 
 
August Karlstrom
Guest
Posts: n/a
 
      05-11-2005
grocery_stocker wrote:
> If go like the following:
>
> #include <stdlib.h>
>
> void *test_me(int a) {
> int *m;
> m = &a;
> }
>
> main(void){
> char *j;
>
> j = test_me(1);
>
> printf("The value of j is : %x", *j);
>
> }
>
> I get:
> The value of j is : 1
>
>
> However, when I add malloc()
>
> #include <stdlib.h>
>
> void *test_me(int a) {
> int *m;
> m = &a;
> }
>
> main(void){
> char *p;
> char *j;
>
> p = malloc(10);
> j = test_me(1);
>
> printf("The value of p is : %x", *p);
> printf("The value of j is : %x", *j);
>
> free(p);
> }
>
> I get the following:
> The value of p is : 0
> The value of j is: ffffffc4
>
> Why does p become zero?
>

My guess is that after test_me has been called the stack is corrupt,
since test_me returns an unspecified value, so p is probably modified by
printf.
 
Reply With Quote
 
grocery_stocker
Guest
Posts: n/a
 
      05-11-2005
So in other words, when in the function test_me(), I should have used
'static'?

 
Reply With Quote
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      05-11-2005
Groovy hepcat grocery_stocker was jivin' on 10 May 2005 20:10:24 -0700
in comp.lang.c.
Why would malloc() modify the behavior of this output?'s a cool scene!
Dig it!

>If go like the following:
>
>#include <stdlib.h>
>
>void *test_me(int a) {
>int *m;
>m = &a;
>}


You should get a diagnostic here. You're not returning anything from
this function. This is no doubt the cause of your problem.

>main(void){


int main(void)
{

.... to satisfy C99.

>char *j;
>
>j = test_me(1);


j now contains an indeterminate value, because you have failed to
return a value from test_me().

>printf("The value of j is : %x", *j);


Since j is indeterminate, dereferencing it here causes undefined
behaviour.

return 0;

.... to satisfy C90.

>}
>
>I get:
>The value of j is : 1


It could be anything. For that matter, you needn't get any output at
all. The program could crash. Or it could cause a shift in the
space-time continuum which traps you in the 13th dimention. Undefined
behaviour means that ANYTHING the program does is "right".

>However, when I add malloc()
>
>#include <stdlib.h>
>
>void *test_me(int a) {
>int *m;
>m = &a;
>}
>
>main(void){


int main(void)
{
>char *p;
>char *j;
>
>p = malloc(10);


Check! Always check the return value of functions like malloc(). You
must not try to dereference the pointer if malloc() fails.

>j = test_me(1);
>
>printf("The value of p is : %x", *p);


*p is uninitialised. Its value is indeterminate. p points to
dynamically allocated memory IF the malloc() call succeeded. but the
memory it points to has not been given a value.

>printf("The value of j is : %x", *j);


And once again the value of j is indeterminate, and you are causing
undefined behaviour by dereferencing it.

>free(p);


return 0;
>}
>
>I get the following:
>The value of p is : 0
>The value of j is: ffffffc4
>
>Why does p become zero?


p does not become 0. *p, however, is indeterminate. It could be 0,
but it could be anything else. It just happens to be 0 in this
instance.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
Reply With Quote
 
Artie Gold
Guest
Posts: n/a
 
      05-11-2005
grocery_stocker wrote:
> So in other words, when in the function test_me(), I should have used
> 'static'?
>

First of all, it is imperative in Usenet discussions to maintain enough
context to know what you're talking about.

And the answer is: The function test_me() needs to return something. It
doesn't.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
 
Reply With Quote
 
Grumble
Guest
Posts: n/a
 
      05-11-2005
Peter Shaggy Haywood wrote:

> It could be anything. For that matter, you needn't get any output at
> all. The program could crash. Or it could cause a shift in the
> space-time continuum which traps you in the 13th dimention. Undefined
> behaviour means that ANYTHING the program does is "right".


Mankind should try to harness the power of UB.

It seems to have even more potential than nuclear fusion.
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      05-11-2005
Grumble <(E-Mail Removed)> wrote:

> Peter Shaggy Haywood wrote:
>
> > It could be anything. For that matter, you needn't get any output at
> > all. The program could crash. Or it could cause a shift in the
> > space-time continuum which traps you in the 13th dimention. Undefined
> > behaviour means that ANYTHING the program does is "right".

>
> Mankind should try to harness the power of UB.
>
> It seems to have even more potential than nuclear fusion.


It does, but unfortunately this includes the power not to be harnessed.
It is a bit like Mephistophilis in this way - people who try to harness
the power of uninitialised objects tend to find themselves facing
thunder and lightning at midnight, twenty-four years being expired.

Richard
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-11-2005
"grocery_stocker" <(E-Mail Removed)> writes:
[...]
> printf("The value of j is : %x", *j);

[...]
> printf("The value of p is : %x", *p);
> printf("The value of j is : %x", *j);


Among numerous other errors, you're not printing the values of j and
p. You're printing the values of *j and *p.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      05-11-2005
Peter "Shaggy" Haywood wrote:
> Groovy hepcat grocery_stocker was jivin':
> >
> >#include <stdlib.h>
> >
> >void *test_me(int a) {
> >int *m;
> >m = &a;
> >}

>
> You should get a diagnostic here. You're not returning
> anything from this function. This is no doubt the cause
> of your problem.


No diagnostic is required for this situation.

> int main(void)
> {
>
> >char *j;
> >
> >j = test_me(1);

>
> j now contains an indeterminate value, because you have
> failed to return a value from test_me().


Actually the behaviour became undefined at the point of
test_me() returning. So 'j' could contain any value
(indeterminate or not), or nasal demons, or cease to exist, etc.

> >printf("The value of j is : %x", *j);

>
> Since j is indeterminate, dereferencing it here
> causes undefined behaviour.


If j were indeterminate, merely mentioning it would cause
undefined behaviour.

 
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
an oddball scary kind of thing you would think would never happen richard Computer Support 4 01-31-2010 06:34 PM
Need to modify JFileChooser's default behavior Ramon F Herrera Java 3 11-13-2007 04:58 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
xmlspy: how to modify behavior j kopek XML 0 02-09-2004 10:34 PM



Advertisments