kumar sanghvi wrote:
int* p; //declared pointer to int (statement does nothing)<br />int w=10; //declares and initializes an int to 10, but it won't exist unless you use it<br />p=&10; //this is an error, you probably meant p=&w?
p=&w
would do, assuming p is used: give (if it didn't have one yet) w a place on the stack (previously it would probably reside in a register), and set p to that address (eg
lea esi, dword ptr[esp+24]
),
but if the operations on p are simple enough (for the specific implementation, not every theoretically simple enough sequence of operations) to evaluate at compile time it may be optimized to something weird and/or scary.
But it's fine to think of them as being on the stack, the compiler will guarantee that this assumption is true whenever you make it (and if you don't make it, it doesn't matter). They're definitely not on the heap, if that's what you were wondering about.
notes: many simplifications and omissions were made to avoid writing a king-sized post, if you really want to know every little detail you could ask the same question on
comp.lang.c++[
^], they will be happy to tell you every pedantic little detail. And I don't guarantee that anything in this post was actually true, use the information at your own risk (etc etc)