Memory address of a pointer

memory address of a pointer

Page 1 of 1

4 Replies - 553 Views - Last Post: 19 April 2010 - 11:08 AM Rate Topic: -----

#1 s3xynanigoat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-April 10

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.


#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;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Memory address of a pointer

#2 joesyuh  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 174
  • Joined: 30-September 08

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.

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

Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • su wtf -am -i /doing/with/my/life
  • member icon

Reputation: 2979
  • View blog
  • Posts: 19,035
  • Joined: 14-September 07

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.
Was This Post Helpful? 1
  • +
  • -

#4 s3xynanigoat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-April 10

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.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4892
  • View blog
  • Posts: 11,287
  • Joined: 16-October 07

Re: Memory address of a pointer

Posted 19 April 2010 - 11:08 AM

You're missing one.
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.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1