How do I find the memory address of a string?

冷暖自知 提交于 2021-02-07 09:15:43

问题


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 the 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.)


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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 :/




回答6:


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



来源:https://stackoverflow.com/questions/6655904/how-do-i-find-the-memory-address-of-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!