Re: Memory location of bytes
From: James Dennett (jdennett_at_acm.org)
Date: 07/25/04
- Next message: Ben Measures: "Re: Memory location of bytes"
- Previous message: Ben Measures: "Re: Memory location of bytes"
- In reply to: Ben Measures: "Re: Memory location of bytes"
- Next in thread: Ben Measures: "Re: Memory location of bytes"
- Reply: Ben Measures: "Re: Memory location of bytes"
- Reply: Edo: "Re: Memory location of bytes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 25 Jul 2004 14:06:58 -0700
Ben Measures wrote:
> Edo wrote:
>
>> Hello
>> the code below puts out -4, I was expecting 4, could some one explain
>> why?
>>
>> x1 is [snip] the lower number, x2 [snip] is [the] bigger number.
>
>
> A positive number minus a larger positive number will have a negative
> result. O_o
>
Note also that there are no guarantees in C or C++ about the
relative addresses in memory of local variables; it's quite
legal for them to be allocated in multiple frames which could
in turn be dynamically allocated.
For even more fun, note that simply taking their addresses can
change things: often when the use of two variables don't actually
overlap, optimizing compilers can use the same memory for both
of them (or sometimes not place them in memory at all) unless
your code "observes" the addresses, in which case the compiler
is more constrained.
It's interesting and informative experimentation to look at
things like stack addresses to see some examples of what can
happen and how things can be done, but it's important not to
conclude that the behaviour you observe is guaranteed.
-- James
- Next message: Ben Measures: "Re: Memory location of bytes"
- Previous message: Ben Measures: "Re: Memory location of bytes"
- In reply to: Ben Measures: "Re: Memory location of bytes"
- Next in thread: Ben Measures: "Re: Memory location of bytes"
- Reply: Ben Measures: "Re: Memory location of bytes"
- Reply: Edo: "Re: Memory location of bytes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|