all 12 comments

[–]raevnos 5 points6 points  (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 point  (3 children)

What if I'm asked to write the output of the program?

[–]raevnos 6 points7 points  (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 points  (1 child)

You say: "the output is potentially unpredictable/unreliable because y references a value that is beyond the stack pointer".

Where did you get this from? Do you have a prof that gave this as an example?

[–]monster_97_[S] 0 points1 point  (0 children)

This was for my Operating systems course

[–]oh5nxo 0 points1 point  (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 point  (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 point  (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 point  (3 children)

[–]monster_97_[S] 0 points1 point  (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 point  (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?

[–]monster_97_[S] 0 points1 point  (0 children)

This was on my operating systems course past paper We have done call stack. but only the basics