Click here to Skip to main content
15,881,172 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionQuestion about Getting Main Icon using Findresource() Pin
lek25820-Jun-11 21:51
lek25820-Jun-11 21:51 
AnswerRe: Question about Getting Main Icon using Findresource() Pin
Richard MacCutchan21-Jun-11 2:01
mveRichard MacCutchan21-Jun-11 2:01 
AnswerRe: Question about Getting Main Icon using Findresource() Pin
MicroVirus21-Jun-11 5:50
MicroVirus21-Jun-11 5:50 
Questionproblem while freeing a link list within a vector structure [modified] Pin
Madan Chauhan20-Jun-11 21:27
Madan Chauhan20-Jun-11 21:27 
AnswerRe: problem while freeing a link list within a vector structure Pin
CPallini20-Jun-11 22:12
mveCPallini20-Jun-11 22:12 
AnswerRe: problem while freeing a link list within a vector structure Pin
Stefan_Lang21-Jun-11 0:35
Stefan_Lang21-Jun-11 0:35 
GeneralRe: problem while freeing a link list within a vector structure Pin
Madan Chauhan21-Jun-11 1:07
Madan Chauhan21-Jun-11 1:07 
GeneralRe: problem while freeing a link list within a vector structure Pin
Stefan_Lang21-Jun-11 1:27
Stefan_Lang21-Jun-11 1:27 
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) {} // constructor initializes string pointer to 0
   ~TestStruct() { delete [] tchTemp; } // destructor frees string if needed
   void set(const TCHAR* stringvalue) {
      delete [] tchTemp; // release previously allocated memory
      if (stringvalue != 0) {
         // create a copy
         tchTemp = new TCHAR[_tcslen(stringvalue)+1];
         _tcscpy(stringvalue, tchTemp);
      }
      else {
         // reset pointer to 0
         tchTemp = 0;
      }
   }
   TCHAR* tchTemp;
};

void test() {
   std::vector<TestStruct> struct_vect;
   // now fill vector and work with it
   // ...
   struct_vect.clear(); // will delete and deconstruct all objects
}

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.
AnswerRe: problem while freeing a link list within a vector structure Pin
CPallini21-Jun-11 1:15
mveCPallini21-Jun-11 1:15 
QuestionRe: problem while freeing a link list within a vector structure Pin
David Crow21-Jun-11 4:04
David Crow21-Jun-11 4:04 
QuestionI am using VC++ coding,Can any one help me? Pin
lucky_122120-Jun-11 21:14
lucky_122120-Jun-11 21:14 
AnswerRe: I am using VC++ coding,Can any one help me? Pin
CPallini20-Jun-11 22:10
mveCPallini20-Jun-11 22:10 
AnswerRe: I am using VC++ coding,Can any one help me? Pin
Cool_Dev20-Jun-11 22:18
Cool_Dev20-Jun-11 22:18 
QuestionConverting char array to CSting Pin
VCProgrammer20-Jun-11 1:43
VCProgrammer20-Jun-11 1:43 
AnswerRe: Converting char array to CSting Pin
Pete O'Hanlon20-Jun-11 1:54
mvePete O'Hanlon20-Jun-11 1:54 
GeneralRe: Converting char array to CSting [modified] Pin
Albert Holguin20-Jun-11 4:36
professionalAlbert Holguin20-Jun-11 4:36 
GeneralRe: Converting char array to CSting Pin
Pete O'Hanlon20-Jun-11 6:10
mvePete O'Hanlon20-Jun-11 6:10 
AnswerRe: Converting char array to CSting Pin
CPallini20-Jun-11 1:55
mveCPallini20-Jun-11 1:55 
AnswerRe: Converting char array to CSting Pin
Rajesh R Subramanian20-Jun-11 3:54
professionalRajesh R Subramanian20-Jun-11 3:54 
Question[Win32]Add context menu to edit control Pin
Member 296547120-Jun-11 1:24
Member 296547120-Jun-11 1:24 
AnswerRe: [Win32]Add context menu to edit control Pin
CPallini20-Jun-11 1:58
mveCPallini20-Jun-11 1:58 
AnswerRe: [Win32]Add context menu to edit control Pin
వేంకటనారాయణ(venkatmakam)20-Jun-11 2:23
వేంకటనారాయణ(venkatmakam)20-Jun-11 2:23 
GeneralRe: [Win32]Add context menu to edit control Pin
Member 296547120-Jun-11 3:07
Member 296547120-Jun-11 3:07 
GeneralRe: [Win32]Add context menu to edit control Pin
Mark Salsbery20-Jun-11 10:16
Mark Salsbery20-Jun-11 10:16 
AnswerRe: [Win32]Add context menu to edit control Pin
User 742933821-Jun-11 1:04
professionalUser 742933821-Jun-11 1:04 

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.