C++ wchar_t 指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3112407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:04:44  来源:igfitidea点击:

wchar_t pointer

c++pointersarrays

提问by Tony The Lion

What's wrong with this:

这有什么问题:

wchar_t * t = new wchar_t;

t = "Tony";

I thought I could use a wchar_t pointer as a string...

我以为我可以使用 wchar_t 指针作为字符串...

回答by Michael

Your code has two issues.

您的代码有两个问题。

First, "Tony"is a pointer to a string of char's. L"Tony"is the appropriate wide string.

首先,"Tony"是一个指向char's字符串的指针。 L"Tony"是合适的宽字符串。

Second, you allocate a single wchar_tvia new, then immediately lose track of it by reassigning the pointer to Tony. This results in a memory leak.

其次,您wchar_t通过 new分配了一个单曲,然后通过将指针重新分配给 Tony 立即失去对它的跟踪。这会导致内存泄漏。

回答by GManNickG

A pointer just points to a single value. This is important.

一个指针只指向一个值。这个很重要。

All you've done is allocated room for a single wchar_t, and point at it. Then you try to set the pointer to point at a string (remember, just at the first character), but the string type is incorrect.

您所做的只是为单个 分配了空间wchar_t,然后指向它。然后您尝试将指针设置为指向一个字符串(记住,只是在第一个字符处),但字符串类型不正确。

What you have is a string of char, it "should" be L"Tony". But all you're doing here is leaking your previous memory allocation because the pointer holds a new value.

你所拥有的是一串char,它“应该”是L"Tony". 但是你在这里所做的只是泄漏你以前的内存分配,因为指针保存了一个新值。

Rather you want to allocate enough room to hold the entire string, then copythe string into that allocated memory. This is terrible practice, though; never do anything that makes you need to explicitly free memory.

相反,您希望分配足够的空间来容纳整个字符串,然后字符串复制到分配的内存中。不过,这是一种可怕的做法;永远不要做任何需要明确释放内存的事情。

Just use std::wstringand move on. std::wstring t = L"Tony";. It handles all the details, and you don't need to worry about cleaning anything up.

只需使用std::wstring并继续。std::wstring t = L"Tony";. 它处理所有细节,您无需担心清理任何东西。

回答by josefx

Since you are a C# developer I will point out a few things c++ does different.

由于您是 C# 开发人员,因此我将指出 C++ 的一些不同之处。

This allocates a new wchar_t and assigns it to t

这将分配一个新的 wchar_t 并将其分配给 t

wchar_t* t = new wchar_t

This is an array of constant char

这是一个常量字符数组

"Tony" 

To get a constant wchar_t array prefix it with L

获得一个常量 wchar_t 数组前缀为 L

L"Tony"

This reasigns t to point to the constant L"Tony" instead of your old wchar_t and causes a memory leak since your wchar_t will never be released.

这将 t 重新指定为指向常量 L"Tony" 而不是您的旧 wchar_t 并导致内存泄漏,因为您的 wchar_t 永远不会被释放。

t = L"Tony"

This creates a string of wide chars (wchar_t) to hold a copy of L"Tony"

这将创建一串宽字符 (wchar_t) 来保存 L"Tony" 的副本

std::wstring t = L"Tony"

I think the last line is what you want. If you need access to the wchar_t pointer use t.c_str(). Note that c++ strings are mutable and are copied on each assignment.

我认为最后一行是你想要的。如果您需要访问 wchar_t 指针,请使用 t.c_str()。请注意,c++ 字符串是可变的,并且在每次赋值时都会被复制。

The c way to do this would be

这样做的c方法是

const wchar_t* t = L"Tony"

This does not create a copy and only assigns the pointer to point to the const wchar array

这不会创建副本,只会分配指向 const wchar 数组的指针

回答by Mark B

What this does is first assign a pointer to a newly allocated wchar_tinto t, and then try to assign a non-wide string into t.

它的作用是首先分配一个指向新分配的wchar_tinto的指针t,然后尝试分配一个非宽字符串 into t

Can you use std::wstringinstead? That will handle all your memory management needs for you.

你可以用std::wstring吗?这将为您处理所有内存管理需求。

回答by gbjbaanb

you can, its just that "Tony" is a hardcoded string, and they're ANSI by default in most editors/compilers. If you want to tell the editor you're typing in a Unicode string, then prefix it with L, e.g. t = L"Tony".

你可以,只是“Tony”是一个硬编码的字符串,在大多数编辑器/编译器中它们默认是ANSI。如果你想告诉编辑器你正在输入一个 Unicode 字符串,那么在它前面加上 L 前缀,例如t = L"Tony".

You have other problems with your code, your allocation is allocating a single Unicode character (2 bytes), then you're trying to point the original variable to the constant string, thus leaking those 2 bytes.

您的代码还有其他问题,您的分配分配了一个 Unicode 字符(2 个字节),然后您试图将原始变量指向常量字符串,从而泄漏了这 2 个字节。

If you want to create a buffer of Unicode data and place data into it, you want to do:

如果要创建 Unicode 数据缓冲区并将数据放入其中,则需要执行以下操作:

wchar_t* t = new wchar_t[500];
wcscpy(t, "Tony");

回答by Pavel Radzivilovsky

this is completely wrong. There's no need to allocate two bytes, make t to point to them, and then overwrite the pointer t leaking the lost memory forever.

这是完全错误的。没有必要分配两个字节,让 t 指向它们,然后覆盖指针 t 永远泄漏丢失的内存。

Also, "Tony" has a different type. Use:

此外,“托尼”有不同的类型。用:

wchar_t *t = L"Tony";

wchar_t *t = L"Tony";

IMHO better don't use wchars at all - See https://stackoverflow.com/questions/1049947/should-utf-16-be-considered-harmful

恕我直言,最好根本不要使用 wchars - 请参阅https://stackoverflow.com/questions/1049947/should-utf-16-be-thinked-harmful