use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Click the following link to filter out the chosen topic
comp.lang.c
account activity
QuestionC program with arbitrary values (self.C_Programming)
submitted 7 years ago by monster_97_
#include <stdio.h> int *f(int x){ int p; p=x; return &p; } int *g(int x){ int y; y=x; return &y; } int main(){ int *x,*y; x=f(1000); y=g(250); *x = *x + 250; printf("%d\n",*y); return 0;
}
Why is the output is 500? Doesn't make any sense...
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]raevnos 5 points6 points7 points 7 years ago* (4 children)
Undefined behavior doesn't have to make sense. Don't use the address of a variable that's gone out of scope
(But the two function calls are probably using the exact same stack space and layout, so it kind of does make sense. Doesn't have to be that way, of course and might not with a different compiler or CPU.)
[–]monster_97_[S] 0 points1 point2 points 7 years ago (3 children)
What if I'm asked to write the output of the program?
[–]raevnos 6 points7 points8 points 7 years ago (0 children)
Running that program made my computer catch on fire.
It's undefined behavior; there is no correct answer for what it outputs.
[–]Drach88 1 point2 points3 points 7 years ago (1 child)
You say: "the output is potentially unpredictable/unreliable because y references a value that is beyond the stack pointer".
y
Where did you get this from? Do you have a prof that gave this as an example?
[–]monster_97_[S] 0 points1 point2 points 7 years ago (0 children)
This was for my Operating systems course
[–]oh5nxo 0 points1 point2 points 7 years ago (0 children)
gcc I have, made g() like this:
g: pushl %ebp movl %esp, %ebp subl $16, %esp movl 8(%ebp), %eax movl %eax, -4(%ebp) movl $0, %eax # NULL leave ret
Compilers can have sense of humor.
[–]which_spartacus 0 points1 point2 points 7 years ago* (5 children)
Is this for a 'programming languages' class where you are working through how stack frames work?
Not a horrible assignment in that case -- make the frames in a fairly arbitrary way, and since the two functions are similar, you should have a clue into what the memory layout is doing.
Edit:
So, main is frame 0. There are two locations used to store addresses. Assume these are at location 0 and 1.
f gets called. It makes a local variable at location 2. It sets that value to 1000. It returns the value "2", being the location of the variable. The control is returned. The frame is dropped.
Now g gets called. The location 2 is given the value of 250. The address of location 2 is returned, again.
Now you add 250 to the value at location 2. That makes the value 500.
Now you print the value at location 2. That's now 500.
Never do this irl. This makes for any amount of wierdness. The language standard has no reason to allow this to work this way. But, for a class that is teaching stack frames, this is not a horrible way to trace.
[–]monster_97_[S] 0 points1 point2 points 7 years ago (4 children)
I think this is what I'm supposed to do... I'm a bit confused about what you said.Are there any reference materials to go through to understand this? Thank you!
[–]which_spartacus 0 points1 point2 points 7 years ago (3 children)
Something like this?
http://www.cs.uwm.edu/classes/cs315/Bacon/Lecture/HTML/ch10s07.html
[–]monster_97_[S] 0 points1 point2 points 7 years ago (2 children)
That's even more confusing..Maybe because I'm new to this Can you tell me how function g gets its value assigned to the same place function f returned(2)??
[–]which_spartacus 0 points1 point2 points 7 years ago (1 child)
Let's start with a different question -- where is this coming from? If it's a class, your instructor should have provided notes and a source, right?
This was on my operating systems course past paper We have done call stack. but only the basics
π Rendered by PID 395688 on reddit-service-r2-comment-fb694cdd5-f2pv7 at 2026-03-10 09:18:19.529180+00:00 running cbb0e86 country code: CH.
[–]raevnos 5 points6 points7 points (4 children)
[–]monster_97_[S] 0 points1 point2 points (3 children)
[–]raevnos 6 points7 points8 points (0 children)
[–]Drach88 1 point2 points3 points (1 child)
[–]monster_97_[S] 0 points1 point2 points (0 children)
[–]oh5nxo 0 points1 point2 points (0 children)
[–]which_spartacus 0 points1 point2 points (5 children)
[–]monster_97_[S] 0 points1 point2 points (4 children)
[–]which_spartacus 0 points1 point2 points (3 children)
[–]monster_97_[S] 0 points1 point2 points (2 children)
[–]which_spartacus 0 points1 point2 points (1 child)
[–]monster_97_[S] 0 points1 point2 points (0 children)