Weird Character Allocation Problem.
-
I have the following code: TCHAR * ptszVersionInfo = _T(""); //Executes OK ptszVersionInfo = new TCHAR[10]; //Executes OK delete ptszVersionInfo; //Executes OK ptszVersionInfo = _T("Anton"); //Executes OK delete ptszVersionInfo; //Weird erro. From debug assert.It basically says there is an error with _CrtIsValidHeapPointer. Now how come. (I am not very good with string allocation X| ).
-
I have the following code: TCHAR * ptszVersionInfo = _T(""); //Executes OK ptszVersionInfo = new TCHAR[10]; //Executes OK delete ptszVersionInfo; //Executes OK ptszVersionInfo = _T("Anton"); //Executes OK delete ptszVersionInfo; //Weird erro. From debug assert.It basically says there is an error with _CrtIsValidHeapPointer. Now how come. (I am not very good with string allocation X| ).
HAHAHA_NEXT wrote: _T("Anton"); is a pointer to STATIC memory, u can't free it! HAHAHA_NEXT wrote: ptszVersionInfo = new TCHAR[10]; //Executes OK delete ptszVersionInfo; //Executes OK this would rather be
delete [] ptszVersionInfo;
Don't try it, just do it! ;-) -
HAHAHA_NEXT wrote: _T("Anton"); is a pointer to STATIC memory, u can't free it! HAHAHA_NEXT wrote: ptszVersionInfo = new TCHAR[10]; //Executes OK delete ptszVersionInfo; //Executes OK this would rather be
delete [] ptszVersionInfo;
Don't try it, just do it! ;-)Is there a way i can allocate _T("Anton") Into a dynamic memory ? because my code is all mixed up. (I allocate the string using both methods).
-
Is there a way i can allocate _T("Anton") Into a dynamic memory ? because my code is all mixed up. (I allocate the string using both methods).
HAHAHA_NEXT wrote: Is there a way i can allocate _T("Anton") Into a dynamic memory ? This does not quite make sense, but I think what you want is:
TCHAR *p = new TCHAR[6];
_tcscpy(p, _T("Anton"));
...
delete [] p;
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
HAHAHA_NEXT wrote: Is there a way i can allocate _T("Anton") Into a dynamic memory ? This does not quite make sense, but I think what you want is:
TCHAR *p = new TCHAR[6];
_tcscpy(p, _T("Anton"));
...
delete [] p;
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
Ths. It is exactly what i needed. The reason for this kind of weird stuff is because i can allocate the same string 2 ways: the first is
p = "Anton"
and the second isp = new TCHAR[6]; _stprintf(p, ....)
This will avoid me having different variable and one clean delete statement :). -
I have the following code: TCHAR * ptszVersionInfo = _T(""); //Executes OK ptszVersionInfo = new TCHAR[10]; //Executes OK delete ptszVersionInfo; //Executes OK ptszVersionInfo = _T("Anton"); //Executes OK delete ptszVersionInfo; //Weird erro. From debug assert.It basically says there is an error with _CrtIsValidHeapPointer. Now how come. (I am not very good with string allocation X| ).
HAHAHA_NEXT wrote: ... delete ptszVersionInfo; //Executes OK ptszVersionInfo = _T("Anton"); //Executes OK delete ptszVersionInfo; Two problems: 1) You are using ptszVersionInfo after you've done a delete on it. 2) You are doing a second delete on a ptr that you've already done a delete on. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com