Click here to Skip to main content
15,903,662 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Is correct lpszString = lpszStringInit ? Pin
_Flaviu9-Oct-11 23:44
_Flaviu9-Oct-11 23:44 
GeneralRe: Is correct lpszString = lpszStringInit ? Pin
_Flaviu9-Oct-11 23:47
_Flaviu9-Oct-11 23:47 
AnswerRe: Is correct lpszString = lpszStringInit ? Pin
Chandrasekharan P10-Oct-11 0:12
Chandrasekharan P10-Oct-11 0:12 
GeneralRe: Is correct lpszString = lpszStringInit ? Pin
_Flaviu10-Oct-11 0:17
_Flaviu10-Oct-11 0:17 
AnswerRe: Is correct lpszString = lpszStringInit ? Pin
_AnsHUMAN_ 10-Oct-11 1:36
_AnsHUMAN_ 10-Oct-11 1:36 
GeneralRe: may i also add Pin
App_10-Oct-11 7:12
App_10-Oct-11 7:12 
GeneralRe: may i also add Pin
_Flaviu10-Oct-11 20:43
_Flaviu10-Oct-11 20:43 
AnswerRe: Is correct lpszString = lpszStringInit ? Pin
Stefan_Lang10-Oct-11 1:42
Stefan_Lang10-Oct-11 1:42 
Syntactically it's correct. Semantically however... only you can say if it is.

What you are doing there is take a pointer to a string and copy it to another pointer. Now both pointers point to the same string. This may be ok, but what if the argument passed to this function was a temporary object and is deleted later? In that case the pointer lpszString in your class will be invalid! Or, even worse, if the string later gets changed somewhere, this will effect your ItemData struct as well! Do you want that?

Advice: Unless you are totally certain no one else but you will ever use this struct ItemData and potentially use it in the way described above, you should take precautions against that: instead of just copying the pointers, you should copy the entire string! Of course, if you do that you should also delete that string in your destructor. It should be roughly like this (didn't actually try it out):
C++
// constructor of ItemData struct
CMyControl::ItemData::ItemData(LPCTSTR lpszStringInit)
{
   if (lpszStringInit != 0  {
      std::size_t length = _tcslen(lpszStringInit);
      lpszString = new TCHAR[length+1];
      _tcscpy(lpszString, lpszStringInit);
   }
   else {
      lpszString = 0;
   }
}
// destructor
CMyControl::ItemData::~ItemData() {
   delete [] lpszString;
}

Now you're sure your ItemData object contains it's own copy that cannot be changed or deleted from the outside, no matter who uses this struct. And you know this copy will be deleted exactly when the ItemData object gets deleted. so there will be no memory leaks caused by your struct.
GeneralRe: Is correct lpszString = lpszStringInit ? Pin
_Flaviu10-Oct-11 2:30
_Flaviu10-Oct-11 2:30 
GeneralRe: Is correct lpszString = lpszStringInit ? Pin
Stefan_Lang10-Oct-11 2:33
Stefan_Lang10-Oct-11 2:33 
QuestionButton Creation, Handling the Click event Pin
jkirkerx9-Oct-11 9:29
professionaljkirkerx9-Oct-11 9:29 
AnswerRe: Button Creation, Handling the Click event Pin
Niklas L9-Oct-11 9:43
Niklas L9-Oct-11 9:43 
GeneralRe: Button Creation, Handling the Click event Pin
jkirkerx9-Oct-11 10:30
professionaljkirkerx9-Oct-11 10:30 
GeneralRe: Button Creation, Handling the Click event Pin
jkirkerx9-Oct-11 13:19
professionaljkirkerx9-Oct-11 13:19 
AnswerRe: Button Creation, Handling the Click event Pin
«_Superman_»9-Oct-11 16:47
professional«_Superman_»9-Oct-11 16:47 
GeneralRe: Button Creation, Handling the Click event Pin
jkirkerx9-Oct-11 17:44
professionaljkirkerx9-Oct-11 17:44 
QuestionCannot add variable in MFC Class view wizard – again ( VC6.0) Pin
Vaclav_9-Oct-11 8:14
Vaclav_9-Oct-11 8:14 
AnswerRe: Cannot add variable in MFC Class view wizard – again ( VC6.0) SOLVED Pin
Vaclav_9-Oct-11 8:30
Vaclav_9-Oct-11 8:30 
QuestionC/C++ Optimization Pin
Hans Dietrich9-Oct-11 4:49
mentorHans Dietrich9-Oct-11 4:49 
AnswerRe: C/C++ Optimization Pin
Stephen Hewitt9-Oct-11 4:56
Stephen Hewitt9-Oct-11 4:56 
AnswerRe: C/C++ Optimization Pin
Hans Dietrich9-Oct-11 5:38
mentorHans Dietrich9-Oct-11 5:38 
GeneralRe: C/C++ Optimization Pin
«_Superman_»9-Oct-11 16:50
professional«_Superman_»9-Oct-11 16:50 
AnswerRe: C/C++ Optimization Pin
Erudite_Eric9-Oct-11 5:48
Erudite_Eric9-Oct-11 5:48 
AnswerRe: C/C++ Optimization Pin
cmk9-Oct-11 8:42
cmk9-Oct-11 8:42 
GeneralRe: C/C++ Optimization Pin
Hans Dietrich10-Oct-11 3:03
mentorHans Dietrich10-Oct-11 3:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.