mgibson
mgibson

Reputation: 6203

C++ - Why does a static string give slightly different addresses when referred to by different functions?

Just trying to get to grips with the intricacies of C++ and therefore am messing around with strings and pointers. However, I have come up against something I don't really understand.

First off, I am using the same function and I far as I know giving it the same argument each time. I am calling it from the main and from another function and getting slightly different addresses from each. The function that reads out data from certain parts of memory is below.

void printDataFromAddressForLength(char* pointer, long length)
{
    cout << "\n" << endl;

    for (int i=0; i<length; i++)
    {
        cout << "&pointer: " << &pointer + i << "\tData: " << pointer + i << endl;
    }
}

The goal of this function is to print on each line, the current address and value, then iterate and be usable from anywhere doing the same thing - very simple function with no real purpose.

This is the code I have in my main()

MessageData messageData = returnStruct();
printDataFromAddressForLength(messageData.pChar, messageData.length);

I am also calling the printDataFromAddressForLength from my returnStruct() function. This code can be seen below.

// Returns MessageData from function 
MessageData returnStruct()
{
static string staticTestString = "Time and time again";

MessageData metaData;
MessageData* pMetaData = &metaData;

pMetaData->pChar = &staticTestString[0];
pMetaData->length = staticTestString.length();

printDataFromAddressForLength(pMetaData->pChar, pMetaData->length);

return metaData;
}

The output below comes from the printDataFromAddressForLength() function called from inside return struct. Note: 0x0027F*7*

&pointer: 0027F7BC      Data: Time and time again
&pointer: 0027F7C0      Data: ime and time again
&pointer: 0027F7C4      Data: me and time again
&pointer: 0027F7C8      Data: e and time again
&pointer: 0027F7CC      Data:  and time again
&pointer: 0027F7D0      Data: and time again
&pointer: 0027F7D4      Data: nd time again
&pointer: 0027F7D8      Data: d time again
&pointer: 0027F7DC      Data:  time again
&pointer: 0027F7E0      Data: time again
&pointer: 0027F7E4      Data: ime again
&pointer: 0027F7E8      Data: me again
&pointer: 0027F7EC      Data: e again
&pointer: 0027F7F0      Data:  again
&pointer: 0027F7F4      Data: again
&pointer: 0027F7F8      Data: gain
&pointer: 0027F7FC      Data: ain
&pointer: 0027F800      Data: in
&pointer: 0027F804      Data: n

This output comes from the printDataFromAddressForLength() function called from the main itself. Note: 0x0027F*8*

&pointer: 0027F8BC      Data: Time and time again
&pointer: 0027F8C0      Data: ime and time again
&pointer: 0027F8C4      Data: me and time again
&pointer: 0027F8C8      Data: e and time again
&pointer: 0027F8CC      Data:  and time again
&pointer: 0027F8D0      Data: and time again
&pointer: 0027F8D4      Data: nd time again
&pointer: 0027F8D8      Data: d time again
&pointer: 0027F8DC      Data:  time again
&pointer: 0027F8E0      Data: time again
&pointer: 0027F8E4      Data: ime again
&pointer: 0027F8E8      Data: me again
&pointer: 0027F8EC      Data: e again
&pointer: 0027F8F0      Data:  again
&pointer: 0027F8F4      Data: again
&pointer: 0027F8F8      Data: gain
&pointer: 0027F8FC      Data: ain
&pointer: 0027F900      Data: in
&pointer: 0027F904      Data: n

As far as I can tell I am first calling that function, passing in a pointer and length.

Then I am returning the struct that contains those two parameters to the main and calling the same function with what I expected to be the same parameters, length clearly is the same but pChar has a strange address increment.

Would be grateful for any insight!

Cheers

Upvotes: 3

Views: 232

Answers (2)

Roddy
Roddy

Reputation: 68033

Your'e not printing what you think you are. The type of &pointer is char * * : a pointer to a pointer. And you then add i to it, which increments it by the size of the pointed-to type (not char, but char *)

Try this:

cout << "&pointer: " << (void*)&pointer[i]    

Upvotes: 4

Jeff Paquette
Jeff Paquette

Reputation: 7127

You're printing the address of the formal parameter 'pointer', not the address of the string.

void printDataFromAddressForLength(char* pointer, long length)
{
    cout << "\n" << endl;

    for (int i=0; i<length; i++)
    {
        cout << "&pointer: " << (void*)(pointer + i) << "\tData: " << pointer + i << endl;
    }
}

Upvotes: 6

Related Questions