Solved

wstring

Posted on 2004-04-06
3
795 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

689 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