简体   繁体   中英

wchar_t pointer

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

Your code has two issues.

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

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

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.

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.

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

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

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

This allocates a new wchar_t and assigns it to 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

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"

This creates a string of wide chars (wchar_t) to hold a copy of 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.

The c way to do this would be

const wchar_t* t = L"Tony"

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

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

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

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, eg 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.

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

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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