Click here to Skip to main content
15,900,325 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Delete or lock a running executable file Pin
paaexis6-Nov-07 22:25
paaexis6-Nov-07 22:25 
QuestionDesign Fancy Buttons (Windows Forms Application (.Net)) Pin
Kunal Bajpai6-Nov-07 1:11
Kunal Bajpai6-Nov-07 1:11 
QuestionString Conversion Pin
Paulraj G6-Nov-07 0:54
Paulraj G6-Nov-07 0:54 
AnswerRe: String Conversion Pin
manish.patel6-Nov-07 1:05
manish.patel6-Nov-07 1:05 
GeneralRe: String Conversion Pin
Paulraj G6-Nov-07 1:09
Paulraj G6-Nov-07 1:09 
GeneralRe: String Conversion Pin
toxcct6-Nov-07 1:53
toxcct6-Nov-07 1:53 
GeneralRe: String Conversion Pin
manish.patel6-Nov-07 23:28
manish.patel6-Nov-07 23:28 
GeneralRe: String Conversion Pin
toxcct6-Nov-07 23:56
toxcct6-Nov-07 23:56 
you seem to be a bit confused. Let me explain you some things then.

When you need a string which size may vary, of course, you have no other way than dynamically allocate the buffer (through malloc() in C, or new/new[] in C++). if your need is purely for a string, then i'd suggest you to use std::string at first by the way.

anyway, let's assume here that we must use new explicitely.
here is how to allocate the buffer, and then how we set a string in it :
TCHAR* str = new TCHAR[n];
::_tcsncpy_s(str, n, _T("Hello World"), n);

as you can see, i allocate the memory before using it.

Here is what you did. check the codes below :
Your Code :
CString str;
str = "Manish";
TCHAR *tch = new TCHAR[10];
tch = str.GetBuffer();

How you should have done it:
CString str = _T("Manish");
TCHAR* tch = new TCHAR[10];
::_tcsncpy_s(tch, 10, (LPCTSTR)str, 10);

See the differences step by step:
1- you first allocate a 10 TCHAR buffer on the stack (with the new TCHAR[10] statement).
2- the address of that memory freshly allocated is stored into the tch variable
3- you then get the address of the internal buffer of the CString object (str) with GetBuffer()
4- you store the address of that internal buffer into the tch variable. by doing this, you overwrite the address allocate in the step 1-.

this leads into 2 falls. not only you loose the memory allocate with new, but you will never be able to release the memory that allocated.
and worst, if you make a delete[] tch, you're not going to free the memory allocated in step 1-, but you're going to delete the CString's internal buffer !!!

so, to fix this, you have to know and remember that we never assign a C-Style string using the = operator. you must use the strcpy() derived functions.
But of course, if you can use std::string class instead of char* strings, it's even better...

do you understand better now ?


GeneralRe: String Conversion Pin
manish.patel7-Nov-07 0:16
manish.patel7-Nov-07 0:16 
GeneralRe: String Conversion Pin
Nelek7-Nov-07 0:25
protectorNelek7-Nov-07 0:25 
GeneralA remark Pin
CPallini6-Nov-07 1:54
mveCPallini6-Nov-07 1:54 
GeneralRe: String Conversion Pin
Cedric Moonen6-Nov-07 1:57
Cedric Moonen6-Nov-07 1:57 
GeneralRe: String Conversion Pin
David Crow6-Nov-07 3:06
David Crow6-Nov-07 3:06 
AnswerRe: String Conversion [modified] Pin
toxcct6-Nov-07 1:46
toxcct6-Nov-07 1:46 
GeneralRe: String Conversion Pin
David Crow6-Nov-07 3:11
David Crow6-Nov-07 3:11 
GeneralRe: String Conversion Pin
toxcct6-Nov-07 3:13
toxcct6-Nov-07 3:13 
QuestionListView Pin
nitin36-Nov-07 0:03
nitin36-Nov-07 0:03 
AnswerRe: ListView Pin
CPallini6-Nov-07 0:11
mveCPallini6-Nov-07 0:11 
AnswerRe: ListView Pin
Nelek6-Nov-07 1:57
protectorNelek6-Nov-07 1:57 
AnswerRe: ListView Pin
Sam Hobbs6-Nov-07 16:06
Sam Hobbs6-Nov-07 16:06 
QuestionContext Menu problem Pin
neha.agarwal275-Nov-07 23:49
neha.agarwal275-Nov-07 23:49 
Questioncan anybody tell me how to print the datagrid.I am using ado to load the datagrid Pin
SnaKeBeD5-Nov-07 23:49
SnaKeBeD5-Nov-07 23:49 
AnswerRe: can anybody tell me how to print the datagrid.I am using ado to load the datagrid Pin
chandu0046-Nov-07 1:40
chandu0046-Nov-07 1:40 
QuestionHow could I use ffmpeg library in my MFC application? Pin
H4u325-Nov-07 23:24
H4u325-Nov-07 23:24 
QuestionDLL into the Activex Project Pin
ashishbhatt5-Nov-07 23:19
ashishbhatt5-Nov-07 23:19 

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.