|
I just noticed that of course you need to free the strings within your struct, and that is something your vector will not take care of.
The best way would be to define both a constructor and destructor for your struct, and use new/delete instead of calloc/free. In the destructor you can then just take care of the string:
struct TestStruct {
TestStruct() : tchTemp(0) {}
~TestStruct() { delete [] tchTemp; }
void set(const TCHAR* stringvalue) {
delete [] tchTemp;
if (stringvalue != 0) {
tchTemp = new TCHAR[_tcslen(stringvalue)+1];
_tcscpy(stringvalue, tchTemp);
}
else {
tchTemp = 0;
}
}
TCHAR* tchTemp;
};
void test() {
std::vector<TestStruct> struct_vect;
struct_vect.clear();
}
Note that in order to actually deconstruct (i. e. call the constructor of) the TestStruct objects, you must call delete , not free , on any dynamically allocated objects, and thus you must call new to allocate it. Also you must use new to allocate your strings if you want to use delete to free them. I added a set function to simplify the process of assigning this string properly, using new .
Of course, if you don't want to change your struct definition, you can also loop over the element vectory and free() tchTemp from your first nodes manually.
|
|
|
|
|
Why are you using your own list, cannot you use the STL one?
Why are you using C allocation functions?
What is wVectorSt ?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Madan Chauhan wrote: All the nodes become free but one that is very first node in the list does not free.
How are you verifying this?
Madan Chauhan wrote: Please give me some idea...
Just a tad bit of cleanup:
void main( void )
{
for (int i = 0; i < 2; i++)
{
TEST_STRUCT *ptrCurrent;
TEST_VECTORST wVectorSt = {0};
wVectorSt.nVal = i + 1;
for (int j = 0; j < 10; j++)
{
TEST_STRUCT *ptrTemp = (TEST_STRUCT *) calloc(1, sizeof(TEST_STRUCT));
_tcscpy_s(ptrTemp->_tchTemp, _countof(ptrTemp->_tchTemp), _T(""));
ptrTemp->dwTemp = 10 + j;
ptrTemp->i64Temp = 100 + j;
ptrTemp->Next = NULL;
if (wVectorSt.m_ListInVector == NULL)
wVectorSt.m_ListInVector = ptrTemp;
else
ptrCurrent->Next = ptrTemp;
ptrCurrent = ptrTemp;
}
m_StVector.push_back(wVectorSt);
}
for (unsigned int i = 0; i < m_StVector.size(); i++)
{
TEST_STRUCT *ptrList = m_StVector[i].m_ListInVector;
while (ptrList != NULL)
{
TEST_STRUCT *ptrTemp = ptrList;
ptrList = ptrList->Next;
free(ptrTemp);
}
}
}
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
|
|
|
|
|
I have the List o f names(if list type of list is CString),How to sort the list of names.Anybody knows please send me the syntax of the coding.
Thanks&Regards,
Lucky.
|
|
|
|
|
What kind of container holds the CString s?
If you are using MFC containers, then have a look at Newcomer's "A guide to sorting"[^].
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
List classes (std::list or MFC CList etc) are temlate classes and can't have a sort function in a general basis. You have to iterate through the elements in list and implement any of the sorting algorithms as you do with noraml arrays. Since you are dealing with CString, you can use the overloaded < and > operators for comaparing string objects in list.
|
|
|
|
|
Hi all,
I want to convert a char array into CString.
i have tried various ways like
char array[620];
CString str(array,620);
It is giving a compiler error.
Can anybody help me with this?
thanks in advance
|
|
|
|
|
I don't think you need to specify the length in the CString constructor, so you would have
CString str(array);
|
|
|
|
|
Shouldn't have to as long as the array contents is null terminated.
modified on Monday, June 20, 2011 10:43 AM
|
|
|
|
|
|
Please let we know the error message ('compiler error' is a bit vague). Anyway I guess you are making a UNICODE build hence the ctor CString::CString(const char *, int) is not available. If the character array is zero-terminated (or you can make it zero-terminated and there aren't other '\0' characters inside) then you could use CString::CString(const char *) instead, namely
CString str(array);
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
VCProgrammer wrote: char array[620];
CString str(array,620);
Why are you writing that piece of code?
If you want the str object to be initialised with the array contents, you just have to say CString str(array);
Also, if you're talking about a compiler error or so, you should provide the exact details so that others don't have to guess what would that be.
"Real men drive manual transmission" - Rajesh.
|
|
|
|
|
How can i add a context menu to an edit control?
I try to handle WM_RBUTTONDOWN in the main window, but don't work if the mouse click is on the edit control.
|
|
|
|
|
You have to subclass the Edit Control, see, for instance Create your own controls - the art of subclassing[^].
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Derive a new class from the CEdit and handle the ON_WM_CONTEXTMENU() message.Check this article for referance,
<href="http: www.codeproject.com="" kb="" edit="" cmenuedit.aspx"="">How to modify a CEdit context menu[^]
http://www.mono-project.com/Main_Page
|
|
|
|
|
I'm not using MFC.
So i've to call SetWindowSubclass function and set my own EditProc in which handle WM_CONTEXTMENU?
|
|
|
|
|
Member 2965471 wrote: So i've to call SetWindowSubclass function and set my own EditProc in which handle WM_CONTEXTMENU?
Yes.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
As an alternative to the mentioned subclassing, it is also possible to handle WM_RBUTTONDOWN directly in your dialogs message loop:
HWND hwndDlg = CreateDialog(...);
MSG msg;
while(GetMessage(&msg,0,0,0)){
if(msg.hwnd == GetDlgItem(hwndDlg,ID_MYEDITCONTROL) && msg.message == WM_RBUTTONDOWN)
;
else{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
modified 13-Sep-18 21:01pm.
|
|
|
|
|
Hi All,
I have a doubt.
PostMessage() will post the message and returns immediately won't wait for Message to complete.
where as SendMessage() will post the message and will not return till the message is completed.
My doubt is why we have to wait in SendMessage() till the message is completed.
Can any body can clarify my doubt?
|
|
|
|
|
Sometimes we need to know the retult of the message and using the return value or wparam or lparam of the message.
Take WM_GETTEXT as an example.
Only after the destination window processed this messasge and filled the lparam with it's text,can our program do something accordingly.
In such case we must use SendMessage.
|
|
|
|
|
|
SendMessage() is a synchronous call, meaning it waits for the code and result of the operation (framework equivalent of making a blocking function call), whereas PostMessage() is asynchronous, meaning there's no waiting for result.
Where is either useful?
-SendMessage() : Your continuation of execution depends on the result of the operation and/or the CWnd's lie in the same thread (blocking calls are a non-issue).
-PostMessage() : Non-blocking is essential, such is the case for threaded programming.
|
|
|
|
|
how to mdichildframe in pane of splitter aplication in vc++
|
|
|
|
|
Your question is not clear, please add some more detail about what you are trying to achieve.
The best things in life are not things.
|
|
|
|
|
Can you please re-phrase your query? Is not clear on what you are asking. make it a little elaborate.
|
|
|
|