[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

CStringArray  and CString deallocation??

Posted on 1998-07-14
2
Medium Priority
?
839 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
ID: 1319171
Edited text of question
0
 
LVL 23

Accepted Solution

by:
chensu earned 400 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

Industry Leaders: 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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

873 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