Solved

wstring

Posted on 2004-04-06
3
789 Views
Last Modified: 2010-05-19
I get memory access error every time I type more than 20 characters in the edit control.

Here's the code:

wstring to;
GetDlgItemText( hWnd, IDC_EDIT_TO, (LPSTR) to.c_str(), 150 );

Error occurs in dbgdel.cpp, line 52.

Please advise.
0
Comment
Question by:jd9288
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
Comment Utility
It is *not* a good idea to use 'wstring::c_str()' for write-access to the string's buffer, better use

wchar_t awcBuffer [ 150];
wstring to;
GetDlgItemText( hWnd, IDC_EDIT_TO, awcBuffer,  150 );

to = awcBuffer;
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>It is *not* a good idea

That is an euphemism, by the way. 'wstring::c_str()' returns a 'const wchar_t*', and that's the way it is supposed to be treated like - as a 'const' buffer. Writing to it will result in unexpected behaviour, actually you are lucky that it does not crash on the 1st or 2nd char entered.
0
 

Author Comment

by:jd9288
Comment Utility
Excellent, but I have to cast the buffer like so:

GetDlgItemText( hWnd, IDC_EDIT_TO, (LPSTR) awcBuffer, 150 );
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now