简体   繁体   English

如何找到字符串的 memory 地址?

[英]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"?如何找到“Hello”的memory地址?

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.编写一个 function,它接受一个参数,一个字符串的地址,并打印该字符串一次。 (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.但是,如果提供了第二个参数类型 int 且非零,则 function 应打印字符串的次数等于 function 在该点被调用的次数。 (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.) (注意,字符串被打印的次数不等于第二个参数的值;它等于 function 到目前为止被调用的次数。)

Use either:使用任一:

or或者

Note that the pointer returned by either of these calls doesn't have to be the underlying data the std::string object is manipulating.请注意,这些调用返回的指针不一定std::string object 正在操作的基础数据。

Take the address of the first character is the usual way to do it.取第一个字符的地址是通常的做法。 &word[0] . &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?我猜你想要一个指向普通旧 C 字符串的指针? Then use word.c_str() .然后使用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.请注意,这不能保证指向字符串的内部存储,它只是您可以使用的(常量)C 字符串版本。

You can use the c_str() function to get a pointer to the C string ( const char * );您可以使用c_str() function 获取指向 C 字符串的指针( 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.您必须再次调用c_str() ,因为旧字符串可能已被释放。

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. a) 保证可以使用可能没有 c_str 方法的开始/结束兼容容器。 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.因此,例如,如果您决定想要一个 QString(字符串 QT 使用)或 AWS::String 或 std::vector 来保存您的字符,则 c_str() 方法不起作用,但上面的方法会起作用。

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 (Eg 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) b) 可能不像 c_str() 那样昂贵...一般来说,它的实现方式应该类似于我在第二行代码中为获取地址所做的调用,但不能保证以这种方式实现(例如,如果字符串您使用的不是 null 终止它可能需要重新分配和更改整个字符串以添加 null 终止符......这会突然使其线程不安全且非常昂贵,这不是 std::string 的情况,而是可能适用于其他类型的字符串)

c) It communicates intent better, in the end what you want is an address and the first bunch of code expresses exactly that. c)它更好地传达意图,最终你想要的是一个地址,第一组代码准确地表达了这一点。

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.声明一个指向变量的指针,然后查看它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM