2

I have a pointer to an int.

int index = 3;
int * index_ptr = &index;

index_ptr is a member variable of a class IndexHandler.

class A has a std::vector of IndexHandlers, Avector. class B has a std::vector of pointers to IndexHandlers, Bvector, which I set to point to the items in class A's vector, thusly:

Bvector.push_back(Avector[i].GetPtr())

With me so far?

I want to check that when I resolve the pointers in Bvector, and retreive the internal IndexHandlers index_ptr, that they point to the same index_ptr as class A's...

How can I check they are pointing to the same memory address?... How do i print the address of the value pointed to by both sets of pointers?

Ronald Wildenberg
  • 31,634
  • 14
  • 90
  • 133
Krakkos
  • 1,439
  • 3
  • 19
  • 24
  • Check out these SO questions: http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome http://stackoverflow.com/questions/759663/pointer-arithmetic-in-c http://stackoverflow.com/questions/tagged?tagnames=pointers&page=2&sort=votes&pagesize=30 – lothar Jun 03 '09 at 15:31

5 Answers5

3

To print the address of an int pointer and the int value an int pointer points to, use

printf("%p %i\n", ptr, *ptr);
schnaader
  • 49,103
  • 10
  • 104
  • 136
  • You can also use %d, %i or anything different to format it if you don't like hex format. However, %p seems to be the "correct way" as it doesn't give a compiler warning (using gcc and -Wall). – schnaader Jun 03 '09 at 15:28
  • yeah, I need hex, so I've always used %X – Krakkos Jun 03 '09 at 17:19
2

the non-iterator method to print them out side-by-side:

for (size_t i = 0; i < Avector.size(); i++)
{
  std::cout << "Avector ptr:" << Avector[i].GetPtr() << ", Bvector ptr:" << Bvector[i] << std::endl;
}

This will print out the pointer values of each one.

One thing you should be aware of though. If the pointer in the IndexHandler is pointing to a value within the IndexHandler then it can become invalidated if the vector is resized, and pointers to anything above an index WILL be invalidated if an item is inserted or deleted at that index. Because of this, it's generally a bad idea to keep pointers to items in a vector and if you want to do this then it's better practice to use a std::list container instead (which doesn't invalidate pointers to items in the list when you insert or delete new values)

workmad3
  • 25,101
  • 4
  • 35
  • 56
  • except that Bvecotr[i] is actually an IndexHandler*... so I'd need to put `Bvector[i]->GetPtr()` right? – Krakkos Jun 03 '09 at 13:12
1

A very 'C' way to do this would be to use printf. Like this:

printf("A = %p, B = %p", (void *)A, (void *)B);

The above assumes A and B are pointer types.

kbyrd
  • 3,321
  • 27
  • 41
1

How can I check they are pointing to the same memory address?

if (index_ptr1 == index_ptr2)
{
  // They point to the same address
}
else
{
  // They don't
}
Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
0

Inside a Class:

//from this to uint value
void*   me=(void*)this;
uint* me_pAddress=(uint*)&me;
uint ThisAddress=*me_pAddress; //address value of 'this' pointer

//from uint value to this pointer
void*** me_ppAddress=(void***)&ThisAddress;
myClass* thisPointer=(myClass* )(*me_ppAddress);
thisPointer->myFunction();
rockdino
  • 91
  • 1
  • 3