Ryan
Ryan

Reputation: 14649

C++ - How to correctly determine the address of a variable?

I'm trying to figure out how to determine the memory address of a variable. I do not know which is the correct way to use.

#include <iostream>
#include <string>
using std::cout;
using std::endl;
void print(std::string *url) {
    std::string url_1 = "http://example.com";
    cout << url << endl;
    cout << "Adress: " << &url << endl;
    *url = url_1;
    cout << "Adress: " << &url << endl;
}
int main() {

  std::string a = "http://google.com";
  print(&a);
  cout << a << endl;
  cout << &a << endl;
  return 0;
}

Output:

0xbfa1ba48
Address: 0xbfa1ba34
Address: 0xbfa1ba34
http://example.com
0xbfa1ba48

When altering the contents of a variable, does the memory address change as well? How do you get the real memory address of a variable?

Upvotes: 0

Views: 3298

Answers (4)

Jarod42
Jarod42

Reputation: 217085

The correct way to take the address of a variable is using std::addressof().

& mostly does the job but (may) fail with overloading operator & (and with virtual inheritance).

Upvotes: 0

Pranit Kothari
Pranit Kothari

Reputation: 9841

I try to disassamble following code,

int pranit = 2;
int& sumit = pranit;

int main(int argc, char** argv) {
    sumit++;

    return sumit;
}

And following instruction suggest pranit has address of sumit.

013B13C8    8B15 04803B01   MOV EDX,DWORD PTR [sumit]                ; ConsoleA.pranit

Moreover both variables have different address,

Names in ConsoleA, item 313
 Address=013B8004
 Section=.data
 Type=Library
 Name=sumit

Names in ConsoleA, item 257
 Address=013B8000
 Section=.data
 Type=Library
 Name=pranit

I have used OllyDbg as disassembler.

Upvotes: -1

TheUndeadFish
TheUndeadFish

Reputation: 8171

In your code url is a pointer containing whatever address was passed to the function, which happens to be the the address of a. Notice you get the same result each time you output url and &a.

On the other hand, the expression &url is taking the address of the url variable itself (so not related to the string object that it points to). But again, the value of &url is the same each time you output it.

So no, altering an object does not change its memory address.

Upvotes: 1

harmic
harmic

Reputation: 30577

Using the '&' operator is the correct way to get the address of a variable. However your program does not demonstrate this too well.

void print(std::string *url) {

You are receiving the address of the string object in the variable URL.

cout << "Adress: " << &url << endl;

Here you are printing the address of the variable that contains the address of the string object, not the address of the string object itself.

*url = url_1;

The '*' on front de-references the pointer url, so this is assigning a new value to the original variable.

It does not change the address of the pointer to the original string object.

It does not even change the address of the original string object. Strings in C++ are implemented as objects which internally store pointers to character arrays containing the strings, so you are just changing the internal contents of the string object, not it's address.

In general: assigning values to variables does not change the address of the variable, only it's content.

Upvotes: 0

Related Questions