如何最好地利用 wcsdup?
How do you best utilize wcsdup?
我正在编写代码,其中很大一部分需要 returning wchar 数组。返回 wstrings 并不是一个真正的选项(尽管我可以使用它们)而且我知道我可以将指针作为参数传递并填充它,但我正在寻找 return 指向这个宽字符数组的指针.前几次迭代,我发现我可以 return 数组,但是当它们被处理和打印时,内存将被覆盖,我会留下乱码。为了解决这个问题,我开始使用 wcsdup,它修复了所有问题,但我很难准确掌握正在发生的事情,因此,应该在什么时候调用它以便它工作并且我没有泄漏内存。实际上,每次我 return 一个字符串以及每次 returned 一个字符串时,我几乎都使用 wcsdup,我知道这会泄漏内存。这就是我正在做的。我应该在哪里以及为什么使用 wcsdup,或者是否有比 wcsdup 更好的解决方案?
wchar_t *intToWChar(int toConvert, int base)
{
wchar_t converted[12];
/* Conversion happens... */
return converted;
}
wchar_t *intToHexWChar(int toConvert)
{
/* Largest int is 8 hex digits, plus "0x", plus /0 is 11 characters. */
wchar_t converted[11];
/* Prefix with "0x" for hex string. */
converted[0] = L'0';
converted[1] = L'x';
/* Populate the rest of converted with the number in hex. */
wchar_t *hexString = intToWChar(toConvert, 16);
wcscpy((converted + 2), hexString);
return converted;
}
int main()
{
wchar_t *hexConversion = intToHexWChar(12345);
/* Other code. */
/* Without wcsdup calls, this spits out gibberish. */
wcout << "12345 in Hex is " << hexConversion << endl;
}
既然你用 'C++' 标记了你的问题,答案是响亮的:不,你根本不应该使用 wcsdup
。相反,要传递 wchar_t
值的数组,请使用 std::vector<wchar_t>
.
如果需要,您可以通过获取第一个元素的地址将它们变成 wchar_t*
(因为向量保证存储在连续内存中),例如
cout << "12345 in Hex is " << &hexConversion[0] << endl;
wchar_t *intToWChar(int toConvert, int base)
{
wchar_t converted[12];
/* Conversion happens... */
return converted;
}
这 return 是一个指向局部变量的指针。
wchar_t *hexString = intToWChar(toConvert, 16);
在这一行之后,hexString
将指向无效的内存并且使用它是未定义的(可能仍然有价值或者可能是垃圾!)。
你对来自 intToHexWChar
的 return 做同样的事情。
解决方案:
- 使用
std::wstring
- 使用
std::vector<wchar_t>
- 传入一个数组给函数使用
- 使用智能指针
- 使用动态内存分配(请不要!)
注意:您可能还需要更改为 wcout
而不是 cout
我正在编写代码,其中很大一部分需要 returning wchar 数组。返回 wstrings 并不是一个真正的选项(尽管我可以使用它们)而且我知道我可以将指针作为参数传递并填充它,但我正在寻找 return 指向这个宽字符数组的指针.前几次迭代,我发现我可以 return 数组,但是当它们被处理和打印时,内存将被覆盖,我会留下乱码。为了解决这个问题,我开始使用 wcsdup,它修复了所有问题,但我很难准确掌握正在发生的事情,因此,应该在什么时候调用它以便它工作并且我没有泄漏内存。实际上,每次我 return 一个字符串以及每次 returned 一个字符串时,我几乎都使用 wcsdup,我知道这会泄漏内存。这就是我正在做的。我应该在哪里以及为什么使用 wcsdup,或者是否有比 wcsdup 更好的解决方案?
wchar_t *intToWChar(int toConvert, int base)
{
wchar_t converted[12];
/* Conversion happens... */
return converted;
}
wchar_t *intToHexWChar(int toConvert)
{
/* Largest int is 8 hex digits, plus "0x", plus /0 is 11 characters. */
wchar_t converted[11];
/* Prefix with "0x" for hex string. */
converted[0] = L'0';
converted[1] = L'x';
/* Populate the rest of converted with the number in hex. */
wchar_t *hexString = intToWChar(toConvert, 16);
wcscpy((converted + 2), hexString);
return converted;
}
int main()
{
wchar_t *hexConversion = intToHexWChar(12345);
/* Other code. */
/* Without wcsdup calls, this spits out gibberish. */
wcout << "12345 in Hex is " << hexConversion << endl;
}
既然你用 'C++' 标记了你的问题,答案是响亮的:不,你根本不应该使用 wcsdup
。相反,要传递 wchar_t
值的数组,请使用 std::vector<wchar_t>
.
如果需要,您可以通过获取第一个元素的地址将它们变成 wchar_t*
(因为向量保证存储在连续内存中),例如
cout << "12345 in Hex is " << &hexConversion[0] << endl;
wchar_t *intToWChar(int toConvert, int base)
{
wchar_t converted[12];
/* Conversion happens... */
return converted;
}
这 return 是一个指向局部变量的指针。
wchar_t *hexString = intToWChar(toConvert, 16);
在这一行之后,hexString
将指向无效的内存并且使用它是未定义的(可能仍然有价值或者可能是垃圾!)。
你对来自 intToHexWChar
的 return 做同样的事情。
解决方案:
- 使用
std::wstring
- 使用
std::vector<wchar_t>
- 传入一个数组给函数使用
- 使用智能指针
- 使用动态内存分配(请不要!)
注意:您可能还需要更改为 wcout
而不是 cout