Solved

wstring

Posted on 2004-04-06
3
793 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
ID: 10765740
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
ID: 10765758
>>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
ID: 10765801
Excellent, but I have to cast the buffer like so:

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

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Writing a parser for java language 4 78
Dialogbox API leak? 18 94
Add values of each row in an array 3 57
draw a Christmas tree by using a nested loop? 26 62
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

776 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