#include <iostream>
int main()
{
int localVariable = 5;
int * pLocal = &localVariable;
int * pHeap = new int;
*pHeap = 7;
std::cout << "localVariable: " << localVariable << std::endl;
std::cout << "*pLocal: " << *pLocal << std::endl;
std::cout << "&pLocal: " << &pLocal << std::endl;
std::cout << "*pHeap: " << *pHeap << std::endl;
std::cout << "&pHeap: " << &pHeap << std::endl;
std::cout << "*new int: " << *new int << std::endl;
std::cout << "&new int: " << &new int << std::endl << std::endl;
std::cout << "Delete pHeap and then initialize it " << std::endl;
std::cout << " ************ Line Break ************ " << std::endl << std::endl;
delete pHeap;
pHeap = new int;
*pHeap = 9;
std::cout << "*pLocal: " << *pLocal << std::endl;
std::cout << "&pLocal: " << &pLocal << std::endl;
std::cout << "*pheap: " << *pHeap << std::endl;
std::cout << "&pHeap: " << &pHeap << std::endl;
std::cout << "*new int: " << *new int << std::endl;
std::cout << "&new int: " << &new int << std::endl << std::endl;
delete pHeap;
std::cout << "Delete pHeap without initializing it " << std::endl;
std::cout << " ************ Line Break ************ " << std::endl << std::endl;
std::cout << "*pLocal: " << *pLocal << std::endl;
std::cout << "&pLocal: " << &pLocal << std::endl;
std::cout << "*pheap: " << *pHeap << std::endl;
std::cout << "&pHeap: " << &pHeap << std::endl;
std::cout << "*new int: " << *new int << std::endl;
std::cout << "&new int: " << &new int << std::endl << std::endl;
char response;
std::cin >> response;
return 0;
}
Memory address of a pointermemory address of a pointer
Page 1 of 1
4 Replies - 553 Views - Last Post: 19 April 2010 - 11:08 AM
#1
Memory address of a pointer
Posted 19 April 2010 - 10:31 AM
In the following code, why is it that the 'new int' memory address changes through each section of code. I may not be understanding what new int is doing... is it creating an int named new, creating an int named new int, just picking random memory space on the free store to place information, or is it doing something else? When I started printing the address of new int I became confused. Thanks.
Replies To: Memory address of a pointer
#2
Re: Memory address of a pointer
Posted 19 April 2010 - 10:35 AM
new is a keyword as evidenced by the fact it is a differetn color then other words.
It creates a memory space for that object. You generally, if not always, use the new operator with pointers. Because if you simply declare a new memory space, how do you access it? As you notice in that code everytime the new keyword is used it is assigned to a pointer. That memory address is being pointed to by that pointer... Hopefully this helps.
http://en.wikipedia....i/New_(C%2B%2B)
Quote
new is an operator that allows dynamic memory allocation on the heap
It creates a memory space for that object. You generally, if not always, use the new operator with pointers. Because if you simply declare a new memory space, how do you access it? As you notice in that code everytime the new keyword is used it is assigned to a pointer. That memory address is being pointed to by that pointer... Hopefully this helps.
http://en.wikipedia....i/New_(C%2B%2B)
This post has been edited by joesyuh: 19 April 2010 - 10:36 AM
#3
Re: Memory address of a pointer
Posted 19 April 2010 - 10:36 AM
new int creates space for an integer on the heap. you then assigned a value to it. "& new int" shouldn't even compile. And if it does you allocating space on the heap you don't have a handle to, thus it's a memory leak.
#4
Re: Memory address of a pointer
Posted 19 April 2010 - 10:41 AM
Ok great, I see what's going on now.
Also, thanks to you KYA - I was going to have a follow up question on the &new int but you've answered it.
Also, thanks to you KYA - I was going to have a follow up question on the &new int but you've answered it.
#5
Re: Memory address of a pointer
Posted 19 April 2010 - 11:08 AM
You're missing one.
You see, &pHeap isn't going to change any more than &localVariable is; you're pointing the the address of the variable. The address in the pointer, however, is open to change.
Here's another tester that might show some allocation...
Results:
Now you have stack and heap action. There some's bonus here, as the parameter count-1 also get allocated. And the math for that will usually happen on the stack.
std::cout << "pHeap: " << pHeap << std::endl;
You see, &pHeap isn't going to change any more than &localVariable is; you're pointing the the address of the variable. The address in the pointer, however, is open to change.
Here's another tester that might show some allocation...
void testStackLocal(int count = 5) {
if (count>0) {
int * pHeap = new int;
std::cout << "pHeap: " << pHeap << std::endl;
std::cout << "&pHeap: " << &pHeap << std::endl;
testStackLocal(count-1);
// we can be nice
std::cout << "delete pHeap " << pHeap << std::endl;
delete pHeap;
}
}
Results:
pHeap: 0x602010 &pHeap: 0x7fff49596a00 pHeap: 0x602030 &pHeap: 0x7fff495969d0 pHeap: 0x602050 &pHeap: 0x7fff495969a0 pHeap: 0x602070 &pHeap: 0x7fff49596970 pHeap: 0x602090 &pHeap: 0x7fff49596940 delete pHeap 0x602090 delete pHeap 0x602070 delete pHeap 0x602050 delete pHeap 0x602030 delete pHeap 0x602010
Now you have stack and heap action. There some's bonus here, as the parameter count-1 also get allocated. And the math for that will usually happen on the stack.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|