Solved

CStringArray  and CString deallocation??

Posted on 1998-07-14
2
831 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Author Comment

by:Surfer
ID: 1319171
Edited text of question
0
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
ID: 1319172
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

728 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