Your compiler does not support the new feature of the C++ 2011 Standard nullptr.
variable intptr is a local variable of function main. If the program starts execution at the same address then the address of intptr will not be changed. What is being changed is the address of the allocated memory that is the address returned by the operator new.
I am using Code::Blocks 12.11 but I hadn't enabled the
have g++ follow the latest C++ ISO standards
in the compiler settings. After enabling it nullptr is working.
You said:
variable intptr is a local variable of function main. If the program starts execution at the same address then the address of intptr will not be changed. What is being changed is the address of the allocated memory that is the address returned by the operator new.
What I understood is that the address of intptr is fixed cause it is static. But during program execution, what's changing is the dynamically allocated memory's address, and that dynamically allocated memory's address is returned to intptr and I actually accessed intptr's memory location everytime. Is that correct?
@The illusionist mirage
What I understood is that the address of intptr is fixed cause it is static. But during program execution, what's changing is the dynamically allocated memory's address, and that dynamically allocated memory's address is returned to intptr and I actually accessed intptr's memory location everytime. Is that correct?
intptr is not static. It is automatic that is each time the program is run intptr is allocated in the stack. The program can be run in general at different addresses depending on whether what other programs are running at this time.
@vlad
It is defined only once so its address will not be changed. But the address of allocated memory will be different in the first and in the second case.
So it means that the address of the pointer and its allocated memory are two different things?
Thanks a lot! It is ridiculous that my teacher didn't mention this fact and I've been buring my eyes out in front of the monitor, trying to figure out why the address isn't changing.
And, is there any way we can get or print the address of allocated memory to the console?
And I have one more thing to ask; I am pretty new to C++(have been learning it since a year and a half) but am seriously interested in mastering it. I know that without grasping the basics, one can't dream mastering it, would you suggest a book that, when it explains a particular topic, explores all areas related to it. I am asking because the book my school follows isn't sufficient i feel(e.g. when it was explaining functions and types of passing arguments, it didn't mention how to return by value).
I can not suggest any good book on C++ for beginners because I do not read them However there are many topics with the similar question in the forum. For example