Article View: borland.public.cppbuilder.nativeapi
Article #4987Re: char *buf is bugged ?
From: Andrue Cope [TeamB]
Date: Mon, 24 Oct 2005 13:37
Date: Mon, 24 Oct 2005 13:37
34 lines
2024 bytes
2024 bytes
Alfred wrote: > char *buf = (char *) calloc( Size, sizeof(char) ); > File->Read(buf,Size); > Form1->Memo->Lines->Add(buf); What about the string terminator? Also:Why use calloc()? You have two choices. Either make the buffer one char longer than needed and ensure that the final char is '\0' or just use the optional length argument for the AnsiString ctor: char * buf=new char[ Size ]; File->Read( buf, Size ); Form1->Memo->Lines->Add( AnsiString( buf, Size ) ); delete[] buf; //////////// BTW:The above code is not exception safe in that any exceptions thrown (quite probable for file I/O) will leak the allocated memory by bypassing the 'delete' (and your 'free()'). This would be better: std::vector<char> buf( Size ); File->Read( &( buf[ 0 ] ), Size ); Form1->Memo->Lines->Add( AnsiString( buf, Size ) ); ..now you don't have to worry about deallocating the buffer because it's automatic when 'buf' is popped from the stack. -- Andrue Cope [TeamB] [Bicester, Uk] http://info.borland.com/newsgroups/guide.html
Technical Details
Message-ID:
<435ce2e1$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsread.com!news-xfer.newsread.com!news-out1.kabelfoon.nl!newsfeed.kabelfoon.nl!bandi.nntp.kabelfoon.nl!194.25.134.62.MISMATCH!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<435cd85e$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> Date: Mon, 24 Oct 2005 14:37:21 +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: <435ce2e1$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1130160865 217.207.223.34 (24 Oct 2005 06:34:25 -0700) Lines: 34 X-Authenticated-User: acope Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsread.com!news-xfer.newsread.com!news-out1.kabelfoon.nl!newsfeed.kabelfoon.nl!bandi.nntp.kabelfoon.nl!194.25.134.62.MISMATCH!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail