?
Solved

CStringArray  and CString deallocation??

Posted on 1998-07-14
2
Medium Priority
?
834 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 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

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

762 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