简体   繁体   English

临时字符串的内存分配

[英]memory allocation of temporary strings

I have a question about strings or specifically about the memory used by a string. 我有一个关于字符串的问题,或者特别是关于字符串使用的内存的问题。 I'm using MSVC2010. 我正在使用MSVC2010。 Consider this piece of code: 考虑一下这段代码:

void Test() {
    LPWCSTR String = L"Testing";
    PrintString(String);
}

void PrintString(LPWCSTR String) {
    // print String to console or similar
}

Is it safe to create and use a string in this way? 以这种方式创建和使用字符串是否安全? Is the memory allocated for the storage of the string freed when the string goes out of scope? 当字符串超出范围时,为字符串的存储分配的内存是否被释放?

Yes it is safe, but actually there are no allocations ;) 是的它是安全的,但实际上没有分配;)

The L"Testing" will be kept in read only part of your exe file (as a constant set of characters). L“Testing”将保存在exe文件的只读部分(作为一组常量字符)。 LPWCSTR String is just a pointer to it, and it doesn't need to be destroyed/deallocated LPWCSTR String只是指向它的指针,不需要销毁/取消分配

I'll assume that LPWCSTR is typo for LPCWSTR ; 我假设LPWCSTRLPCWSTR拼写错误; a freaky Microsoft name for a pointer to a C-style string. 指向C风格字符串的指针的怪异Microsoft名称。 It stands for "long pointer to constant wide string", and is an obfuscated way of writing const wchar_t* . 它代表“指向常量宽字符串的长指针”,并且是编写const wchar_t*的混淆方式。

Is it safe to create and use a string in this way? 以这种方式创建和使用字符串是否安全?

Like any pointer, it's safe to use as long as it points to a valid array. 像任何指针一样,只要它指向一个有效的数组就可以安全使用。 In this case, it points to a string literal, which is an array with a lifetime as long as the program. 在这种情况下,它指向一个字符串文字,这是一个与程序一样长寿的数组。 So this usage is safe. 所以这种用法是安全的。

If it were to point to an array that might be destroyed while the pointer were still in use, then it would not be safe. 如果它指向一个可能在指针仍在使用时被销毁的数组,那么它就不安全了。

Is the memory allocated for the storage of the string freed when the string goes out of scope? 当字符串超出范围时,为字符串的存储分配的内存是否被释放?

No; 没有; the pointer will not manage memory for you. 指针不会为您管理内存。 If you need to do that, use std::wstring . 如果需要这样做,请使用std::wstring

Yes, it is safe to use a string that way. 是的,以这种方式使用字符串是安全的。 "Testing" will be stored in the data segment of your binary and String will be initialized to point to it. “Testing”将存储在二进制文件的数据段中,String将被初始化为指向它。

L"Testing" is a wide string literal and has static storage duration which means it's lifetime is the lifetime of the program, you do not have to worry about deallocating it. L"Testing"是一个宽字符串文字,具有静态存储持续时间,这意味着它的生命周期是程序的生命周期,您不必担心解除分配它。 The C++ draft standard in section 2.14.5 String literals paragraph 11 says ( emphasis mine ): 2.14.5节中的C ++草案标准 字符串文字11段说( 强调我的 ):

A string literal that begins with L, such as L"asdf", is a wide string literal . 以L开头的字符串文字,例如L“asdf”,是一个宽字符串文字 A wide string literal has type “array of n const wchar_t”, where n is the size of the string as defined below; 宽字符串文字的类型为“n const wchar_t的数组”,其中n是字符串的大小,如下所示; it has static storage duration and is initialized with the given characters. 它具有静态存储持续时间,并使用给定的字符进行初始化。

Yes it's safe. 是的,这是安全的。

The object is a string literal. 该对象是一个字符串文字。 It means that it has the lifetime of the program. 这意味着它具有程序的生命周期。

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

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