473,395 Members | 1,631 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

wchar_t crashes when return from a function

I have a function like this:

const wchar_t* getId()
{
std::wstring id_w = L"12345678";
return id_w.c_str();
}

int _tmain(int argc, _TCHAR* argv[])
{
std::wcout.imbue(std::locale(""));
wprintf(L"This is wchar: %s", getId() );
}

It output "This is wchar: ?2345678", The first character is crashed.

But if I do it without function call, like this, it works fine.
int _tmain(int argc, _TCHAR* argv[])
{
std::wcout.imbue(std::locale(""));
std::wstring id_w = L"12345678";
wprintf(L"This is wchar: %s", id_w.c_str() );
}

Any ideas what is wrong?

Thanks
Aug 13 '10 #1
1 2182
newb16
687 512MB
std::wstring id_w = L"12345678";
this is local variable, it's destroyed after function returns and the pointer to string obtained by c_str() is no longer valid.
Aug 13 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: 8leggeddj | last post by:
Hello, I am having a problem when using access xp as a frontend for sql server 2000. I have been trying to update a number of stored procedures (Just simple adding fields etc) which results in...
2
by: Chris Hankey | last post by:
I having a strange and annoying problem where Access crashes when the user copies (to the clipboard) the results of a query. The message is not very helpful. It simply says - "Microsoft Access...
0
by: Google Groups | last post by:
Hi, I have the following configuraiton: Server side: Apache 2.x with deflate module configured for cotet-stream. Client Side: IE 6.0.2 SP2 dot net framework v1.1.4322
3
by: TheSebaster | last post by:
I register to a com event under a win32 C# application. Here is the code I wrote: InstrumentUX.StorageControl pStorageControl = null; pStorageControl =...
0
by: Greg | last post by:
I've overriden the DateTimePicker control. Its been working fine so far, but when I override the Format property to hide it from the design time properties in the IDE, the IDE crashes when I try to...
0
by: =?Utf-8?B?QmFjaA==?= | last post by:
Hi, I have a ASP.NET 2.0 web service, which runs beautifully when hosted in the development server of VS2005 (out side of IIS) but crashes when hosted inside IIS 5.1 or IIS 6.0 when it takes...
1
by: drmcgee2000 | last post by:
Greetings, I saw a post from last year where VS 2005 crashes when editing CSS. I am wondering if anyone else is having the same issue, if there is a work around, or a fix (LOL) from Microsoft. ...
41
by: z | last post by:
I use Visual C 2005 to develop my programs. One in particular is crashing in very specific and hard to replicate situations, made worse by the fact it only crashes when run -outside- the dev - as...
7
by: whutto | last post by:
I have a VB.NET app that crashes when run from the Windows scheduled tasks, but runs fine when the run-as user is logged on. The app also runs fine from the scheduled jobs if the run as user is...
2
by: slizorn | last post by:
hi guys, another problem i am facing with this program.. i have created a method to read in values from a file and store them into TreeNodes of a Tree please help me to solve the problem below.....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.