Solved

wstring

Posted on 2004-04-06
3
794 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

733 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