Static memory address of pointer

Hello

I made a dynamic memory allocation using an integer pointer as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main()
{
    int* intptr = NULL;

    intptr = new int(30);

    cout << "\n&intptr = " << &intptr << endl;
    cout << "intptr = " << *intptr << endl;

    delete intptr;

    return 0;
}



Now I have two problems:

First: since I made a dynamic memory allocation, the memory address of the pointer should change every time I execute the code. But it is not changing

Second: if I am initializing intptr with nullptr instead of NULL, my compiler is giving the following error:

error: 'nullptr' was not declared in this scope


So where's the problem in both the cases?

Thanks
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.
@vlad


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?

Thanks
Last edited on
@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.

If you rewrite your program the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

using namespace std;

int main()
{
    int* intptr = NULL;
    int* intptr2 = NULL;

    intptr2 = new int(10);
    intptr = new int(30);

    cout << "\n&intptr = " << &intptr << endl;
    cout << "intptr = " << *intptr << endl;

    delete intptr2;
    delete intptr;

    intptr = new int(30);

    cout << "\n&intptr = " << &intptr << endl;
    cout << "intptr = " << *intptr << endl;

    delete intptr;

    return 0;
}


you will see that the value of &intptr will be the same because it is the address of the variable intptr itself

int* intptr = NULL;

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.
@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?
Last edited on
Yes you are right. After executing definition

int* intptr = NULL;

the address of intptr is fixed. What can be changed is the address of allocated memory returned by the operator new.




@vlad


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?


Thanks
Last edited on
@vlad


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).

Thanks :)
Last edited on
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

http://www.cplusplus.com/forum/beginner/106771/
Topic archived. No new replies allowed.