Reputation: 3294
I'm trying to determine a way to test the location of variables on the stack (without doing it the common sense way).
int main()
{
int i,j;
return 0;
}
there's really no difference between stating int i; intj; and int i,j; I can tell by simply cout'ing the addresses. i goes on the stack, then j goes on the stack. But is there anyway I could tell this w/o using the memory address operator (or pointers).
this is a specific assignment. Probably so that I can comprehend the reason why the stack is implemented this way. I can easily determine of how they are located (and yes it's compiler specific but gcc for simplicities sake).
So yes I meant to be doing things the hard way here.. to figure out and demonstrate why and how stacks are implemented in a particular manner
I can add more code, more functions.. whatever.. but the point is to demonstrate the ordering on the stack without the &
Upvotes: 1
Views: 253
Reputation: 86651
So you want to find the addresses of local variables without using the C operator designed specifically to find the address of a variable. Why?
It's worth pointing out that, in any modern compiler/processor combination, the i and j in your example may not be on the stack if you don't use the &
operator. Even if you do something with them so the compiler doesn't optimise them away, it may still choose to put them in registers.
Edit: I've read your comment to datenwolf's answer and I think I know what you are being asked for. Here is a possible answer, but I must stress that I am making big assumptions about your compiler. In particular,
If you have a function like this:
int f()
{
int i = 1, j = 2;
return 0;
}
You can inspect the order of i, j in memory with this function:
void order()
{
int a[2];
if (a[0] == 1)
{
printf("i first\n");
}
else
{
printf("j first\n");
}
}
int main()
{
f();
order();
return 0;
}
Never use anything like the above in real code. It relies on several pieces of officially undefined behaviour.
Upvotes: 4
Reputation: 162164
The language C doesn't even have this concept called a stack. It just defines the behaviour of automatic storage and most compilers implement it using the stack provided by most architectures.
Heck even the &
operator is purely abstract and the value returned by it, in its numerical representation can be anything, as long as it adheres to the rules of pointer arithmetic. But this number, it may completely virtual and not related at all to the addresses used in the instructions.
So while the task of determining if a variable is located on the stack is well defined, it is, by definition, impossible to implement using pure ANSI C, without resting on implementation specific behaviour. The usual way to carry out this task is by doing a stack walk, provided by functions of the implementations runtime library.
Sorry that I can't be more specific than this. Unlike it's reputation of being a very low level language, C is in fact defined in very abstract terms and many of the programming errors found in C programs stem from this misconception.
Upvotes: 1
Reputation: 3890
AFAIK There are no guarantees of the ordering in either the C nor the C++ standard. i.e. while you might not detect any differences, there might well be differences when using a different compiler or different (optimization) settings. (in fact the optimizer might also reorder i and j or remove them entirely)
So yes, you need to use "address of". Why wouldn't you?
Upvotes: 3
Reputation: 16406
Why do you want to do this? The location of i and j on the stack are implementation dependent. In fact, in many implementations, i and j won't occupy any space anywhere in your example, because they are never used. The only way to find out for sure is to look at the generated code (which will be different if you use the addresses of your variables).
Upvotes: 2