🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #4999

Re: char *buf is bugged ?

#4999
From: Andrue Cope [TeamB]
Date: Tue, 25 Oct 2005 08:03
41 lines
2659 bytes
Alfred wrote:

> What about : char * buf=new char[Size]; 
> It is good like a vector or it is not a good idea to use ?

It's quite okay to use it but as Thomas has said it's harder to ensure
it doesn't leak. Taking the example code I posted:

char * buf=new char[ Size ];
File->Read( buf, Size );
Form1->Memo->Lines->Add( AnsiString( buf, Size ) );
delete[] buf;

Both of the the middle two statements could raise exceptions. The
Read() in particular could since it's file I/O. If either of them does
the code will exit before ever reaching the 'delete[]' and that will
leak memory. The same is true for your original code.

The reason std::vector doesn't have this problem is that although an
exception would still of course cause the code to exit, any local stack
based variables will be correctly destroyed. For a 'char *' that
doesn't mean anything but a std::vector<char> is a class and when it's
destroyed its destructor will be called. That will ensure that any
related allocations are cleared up.

FWIW there is another way to do this and when 'upgrading' code I often
prefer it. There is a library called Boost (www.boost.org) and one of
its template classes is designed to wrap array pointers and ensure they
are deallocated:

boost::scoped_array<char> buf( new char[ Size ] );
File->Read( buf.get(), Size );
Form1->Memo->Lines->Add( AnsiString( buf.get(), Size ) );

The advantage of this (for me) is that it keeps a lot of the old code
and thus makes it obvious that it's been upgraded. I work with a lot of
old code and knowing the vintage of that code can be very handy.
-- 
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Technical Details
Message-ID: <435de61f$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed.inode.at!reader.uucp.at!feeder02.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mail
References: <435cd85e$1@newsgroups.borland.com> <435ce2e1$1@newsgroups.borland.com> <435d2986$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,a803499fd1f61208
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Andrue Cope [TeamB]" <no.spam@not.a.valid.address>
Subject: Re: char *buf is bugged ?
Newsgroups: borland.public.cppbuilder.nativeapi
References: <435cd85e$1@newsgroups.borland.com> <435ce2e1$1@newsgroups.borland.com> <435d2986$1@newsgroups.borland.com>
Date: Tue, 25 Oct 2005 09:03:32 +0100
User-Agent: XanaNews/1.17.6.5
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
NNTP-Posting-Host: 217.207.223.34
Message-ID: <435de61f$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1130227231 217.207.223.34 (25 Oct 2005 01:00:31 -0700)
Lines: 41
X-Authenticated-User: acope
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed.inode.at!reader.uucp.at!feeder02.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mail