开发者

How do I find the memory address of a string?

I am having a mental block and I know I should know this but I need a little help.

If I declare a string variable like this:

    string word = "Hello";

How do I find the memory address of "Hello"?

Edit: This is what I am trying to do...

Write a function that takes one argument, the address of a string, and prints that string once. (Note: you will need to use a pointer to complete this part.)

However, if a second argument, type int, is provided and is nonzero, the function should print th开发者_运维技巧e string a number of times equal to the number of times that function has been called at that point. (Note that the number of times the string is printed is not equal to the value of the second argument; it is equal to the number of times the function has been called so far.)


Use either:

  • std::string::data() if your data isn't null-terminated c-string like.

or

  • std::string::c_str() if you want the data and be guaranteed to get the null-termination.

Note that the pointer returned by either of these calls doesn't have to be the underlying data the std::string object is manipulating.


Take the address of the first character is the usual way to do it. &word[0]. However, needing to do this if you're not operating with legacy code is usually a sign that you're doing something wrong.


I guess you want a pointer to a plain old C-string? Then use word.c_str(). Note that this is not guaranteed to point to the internal storage of the string, it's just a (constant) C-string version you can work with.


You can use the c_str() function to get a pointer to the C string (const char *); however note that the pointer is invalidated whenever you modify the string; you have to invoke c_str() again as the old string may have been deallocated.


OK, so, I know this question is old but I feel like the obvious answer here is actually:

std::string your_string{"Hello"};

//Take the address of the beginning
auto start_address = &(*your_string.begin())

//Take the address at the end
auto end_address = &(*your_string.end())

In essence this will accomplish the same thing as using:

auto start_address = your_string.c_str();

auto end_address = your_string.c_str() + strlen(your_string.c_str());

However I would prefer the first approach (taking the address of the dereferenced iterator) because:

a) Guaranteed to work with begin/end compatible containers which might not have the c_str method. So for example, if you decided you wanted a QString (the string QT uses) or AWS::String or std::vector to hold your characters, the c_str() approach wouldn't work but the one above would.

b) Possibly not as costly as c_str()... which generally speaking should be implemented similarly to the call I made in the second line of code to get the address but isn't guaranteed to be implemented that way (E.g. if the string you are using is not null terminated it might require the reallocation and mutation of your whole string in order to add the null terminator... which will suddenly make it thread unsafe and very costly, this is not the case for std::string but might be for other types of strings)

c) It communicates intent better, in the end what you want is an address and the first bunch of code expresses exactly that.

So I'd say this approach is better for:

Clarity, compatibility and efficiency.

Edit:

Note that with either approach, if you change your initial string after taking the address the address will be invalidated (since the string might be rellocated). The compiler will not warn you against this and it could cause some very nasty bugs :/


Declare a pointer to the variable and then view it how you would.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜