New answers tagged

3

The answer depends on the context of the question. If the context of the question is whether there is any way to do this without respect to the compiler, in a completely standard way without relying on undefined behavior, that works everywhere, then the answer is a clear and unambiguous "no". However with that said... In some contexts, it may be very ...


10

The compiler is only required to honor legal, valid constructs. Compilers have myriads of choices to make in code generation. Compilers have many, many ways of doing the same thing. If the difference between one choice or another can't be observed -- by the program, using only valid language constructs -- then it will generally opt for the more ...


2

Not legally. There is a good chance that after writing either p += 1 or p -= 1 or p += 2 or p -= 2 we find that p == &b. (All these assignments other than p += 1 invoke undefined behaviour). However, there is no way to read or write b using that pointer without invoking undefined behaviour. Therefore, the compiler may for example delaying the ...


12

No is the correct answer. However, seeing that both variables have been declared on stack right after each other, you might actually write p++ to maybe obtain the pointer to memory location where b is, if you are lucky. Note this is in no case safe and can never be relied upon, as it relies on undefined behaviour.


2

A very schematic explanation Basic Idea: A pointer is a variable storing a memory address (typically 4 bytes) E.g. char* t_pointer; // This Pointer is a 4 bytes variable that will contain (but currently does not contain) the memory address of a char Setting up a pointer value: A typical way consists of using the “&” address-of or reference ...


7

That's not how pointers work. A pointer is, essentially, a record of a memory address. If dereferenced, you can read (or write) the value that lives at that address. Pointers can point to anything that lives in memory. In particular, they can point to other pointers. They can even point themselves (although that is pretty useless). However, the action of ...



Top 50 recent answers are included