Solved

CStringArray  and CString deallocation??

Posted on 1998-07-14
2
809 Views
Last Modified: 2013-11-20
How do I deallocate the memory for the following code?

CStringArray ob ;      
CString s ;
CString *pStr ;
ob.SetSize( 0,100 );
for (INT ni=0;ni< 3/*1000*/;++ni) {
pStr = new CString ;
pStr->Format("Test String %d",ni) ;
ob.Add ( pStr->GetBuffer(0) );
pStr->ReleaseBuffer(-1 ) ;
}

CString s1 ;
s1 = ob.GetAt(1) ;

I tried:
for (INT n=0;n<3;++n)
delete (CString*) ob.GetAt(n) ;
and
delete ob.GetAt(n) ;
but the compiler complains.  
0
Comment
Question by:Surfer
2 Comments
 

Author Comment

by:Surfer
Comment Utility
Edited text of question
0
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
Comment Utility
Actually, CStringArray is more friendly than you thought. When you call CStringArray::Add(), it allocates memory for you, it does not directly use the CString you pass it. Finally the destructor of CStringArray will deallocate the memory. So, your code can be simplified like this.

CStringArray ob;
for (INT ni = 0; ni< 3/*1000*/; ++ni)
{
    CString str;
    str.Format(_T("Test String %d"), ni);
    ob.Add(str);
}

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now