By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,384 Members | 671 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,384 IT Pros & Developers. It's quick & easy.

why do these pointers not point to the addresses as expected?

P: 1
Hello,

i need to get a byte-representation of an integer, so im trying to create a char* from a standard int.
Here's a little testprogram regarding that problem and the results i get from it (using VS12 compiler without optimization):
Code:
Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char* argv) {
  2.     std::cout << "[dbg0]" << std::endl;
  3.     int foo = 2100000000;
  4.     std::cout << "&foo: " << &foo << " - val: " << foo << " - sizeof: " << sizeof(foo) << std::endl;
  5.     std::shared_ptr<int> spi = std::make_shared<int>(foo);
  6.     std::cout << "&spi: " << &spi << " - val: " << *spi << std::endl;
  7.     std::shared_ptr<int> spi2(spi);
  8.     std::cout << "&spi2: " << &spi2 << " - val: " << *spi2 << std::endl;
  9.     int* bar = &foo;
  10.     std::cout << "&bar: " << &bar << " - val: " <<*bar << std::endl;
  11.     bar -= 8;
  12.     std::cout << "&bar-8: " << &bar << " - val: " <<*bar << std::endl;
  13.     char* bytewiseint = (char*)&bar;
  14.     std::cout << "&bytewiseint: " << &bytewiseint << " - val: " <<*bytewiseint << std::endl;
  15.     char* bytewiseint2 = reinterpret_cast<char*>(&foo);
  16.     std::cout << "&bytewiseint2: " << &bytewiseint2 << " - val: " <<*bytewiseint2 << std::endl;
  17.     std::cout << "[dbg1]" << std::endl;
  18.     for(unsigned int c = 0; *(bytewiseint+c) != '\0'; c++) {
  19.         std::cout << "c=" << c << ". " << *(bytewiseint+c) << std::endl;
  20.     }
  21.     std::cout << "[dbg2]" << std::endl;
  22.     std::getchar();
  23.     return 0;
  24. }
Results:
Expand|Select|Wrap|Line Numbers
  1. [dbg0]
  2. &foo: 0028F938 - val: 2100000000 - sizeof: 4
  3. &spi: 0028F928 - val: 2100000000
  4. &spi2: 0028F918 - val: 2100000000
  5. &bar: 0028F90C - val: 2100000000
  6. &bar-8: 0028F90C - val: 4569636
  7. &bytewiseint: 0028F900 - val: ↑
  8. &bytewiseint2: 0028F8F4 - val:
  9. [dbg1]
  10. c=0. ↑
  11. c=1. 
  12. c=2. (
  13. [dbg2]
  14.  
That throws up a couple of questions:
1. Why does spi2 point to a different address as spi when using copy constructor? Should it not point to the exact same memory?
2. I am explicitly deducing 8 from &bar, but terminal still saying its the same address... but the value changed .. ?
3. Even using reinterpret_cast with &foo seems to allocate new memory in this case - im confused


Turned optimization off, but that doesnt change anything.
I ended up throwing "&" and "*" around, so there might be some nonsense in the code snippet shown

any help appreciated
Oct 18 '16 #1

✓ answered by weaknessforcats

First you have:

Expand|Select|Wrap|Line Numbers
  1. int foo = etc...;
The address of this int is &foo.

When you use make_shared<int> using foo, a new foo is created by make_shared and you get a shared_ptr<int> object returned. This object contains the address of the foo created by make_shared<int> and not the foo you created.

I see where you:

Expand|Select|Wrap|Line Numbers
  1. bar -= 8;
and then you:

Expand|Select|Wrap|Line Numbers
  1. cout << &bar ...
This is the address of bar and not the address inside bar. The address of bar does not change when you change the contents of bar. The contents of bar is *bar. If you cout that you will see the address contained in bar.

This is why &sp1 and &sp2 are different. They are different objects. You need to look inside these object to see the pointer they own.

Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
First you have:

Expand|Select|Wrap|Line Numbers
  1. int foo = etc...;
The address of this int is &foo.

When you use make_shared<int> using foo, a new foo is created by make_shared and you get a shared_ptr<int> object returned. This object contains the address of the foo created by make_shared<int> and not the foo you created.

I see where you:

Expand|Select|Wrap|Line Numbers
  1. bar -= 8;
and then you:

Expand|Select|Wrap|Line Numbers
  1. cout << &bar ...
This is the address of bar and not the address inside bar. The address of bar does not change when you change the contents of bar. The contents of bar is *bar. If you cout that you will see the address contained in bar.

This is why &sp1 and &sp2 are different. They are different objects. You need to look inside these object to see the pointer they own.
Oct 18 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.