4 ms·
I tried the example below, saved as `main.c`. It seems she automatically reorders the static mem. Can that behavior be disabled? Can someone please explain?
by kevin_clever 9y ago
I tried the example below, saved as `main.c`. It seems she automatically reorders the static mem. Can that behavior be disabled? Can someone please explain?
#include<stdio.h>
int main(int argc, char** argv)
{
char *p;
char c;
int x;
printf("char p %p\n", &p);
printf("char %p, offset %i\n", &c, (int)((void*)&c-(void*)&p));
printf("integer %p, offset %i\n", &x, (int)((void*)&x-(void*)&p));
return 0;
}
Output on `gcc main.c; ./a.out`
char p 0x7ffc5107dff0
char 0x7ffc5107dfeb, offset -5
integer 0x7ffc5107dfec, offset -4
- kps 9y agoThose are not structure members. C provides no guarantees about the relative locations of separate variables.
- kevin_clever 9y agoSo, it's a compiler specific reordering?
- pjc50 9y agoYes. Probably specific to compiler options too. Only structure members have a defined order.
- sargun 9y agoAre there any guarantees about (re)use of stack addresses?
- sigjuice 9y agoNo. "Stack" is an implementation detail. I don't think the standard even mentions the word "stack" anywhere.
- sigjuice 9y agoSubtraction of arbitrary pointers is not legal.
- marcoperaza 9y agoYou're technically right, because you can have C compilers for architectures with all sorts of weird memory semantics. But in reality, this will work as expected on all of the major platforms and give you the relative positions of the variables on the stack. The C standard is not the final say on things, your compiler and platform are. The standard is merely the minimum specifications that are ideally to be supported on all platforms, however you will still be unpleasantly surprised if you rely on it as such.
- ajross 9y agoIt's not defined by the language standard. Real machine addresses have values and can be compared.
- lomnakkus 9y agoWhat does "can be compared" mean? To me it means that "<=>" [1] is well-defined. It doesn't say anything about numerical values. [1] Spaceship operator. Think "return value of memcmp" but generalized to any operand types.
- YSFEJ4SWJUVU6 9y agoAny valid pointer values can be compared for equality. Only pointers that point within a single object (like an array, or a struct) can be compared for ordering. Nesting doesn't matter as long as down the line there's a common "parent" of sorts. (That is, if you want to have well-defined behaviour.) (Edit: and for any pointers that you can compare for ordering, you may also calculate their offset from one another. However, like GP said, in practice you can do that for any pointers on common platforms.)
- ajross 9y agoNo, real pointer values have numerical values. They are numbers, period. You guys are getting balled up thinking about the language standard as the "ultimate reference of truth" in the argument. When the whole point of the linked article is about pointing out that, no, the behavior of the actual machine being used is a more important "truth" in some contexts.
- unwind 9y agoFirst: you have UB; %p requires void *. Second: separate variables don't have to appear in memory in source code order. This applies only to struct members, which do.
- deleted 9y ago[deleted]
- ksherlock 9y agoWhy would you want to disable that behavior? If you need them in a specific order, you can disable that behavior by using a struct. Ideally, your automatic variables won't have any memory location at all because they're optimized out or kept in registers.
- eloff 9y agoVariables like that don't even have to have unique addresses. They may live only in registers, or if their usage doesn't overlap they can be given the same stack address!
- bla2 9y agoThose are on the stack, and the stack grows down.
- pafje 9y agoThat's true most of the time yes. But it's not a rule, some architectures allow the stack pointer to grow in either direction: https://stackoverflow.com/questions/664744/what-is-the-direction-of-stack-growth-in-most-modern-systems/664779#664779 https://stackoverflow.com/questions/664744/what-is-the-direc...