From: XeCycle Newsgroups: comp.lang.c++ Subject: Re: vector as a char buffer ? References: Date: Wed, 28 Dec 2011 12:27:50 +0800 Message-ID: <87r4zp9v7d.fsf@gmail.com> User-Agent: Gnus/5.110018 (No Gnus v0.18) Emacs/24.0.92 (gnu/linux) Cancel-Lock: sha1:Kd+LoB4Aby5S3s9Ab/hGwyzHewo= MIME-Version: 1.0 Content-Type: text/plain NNTP-Posting-Host: $$5lonjgdxpj7l67.news.x-privat.org Organization: X-Privat.Org NNTP Server - http://www.x-privat.org Lines: 33 X-Authenticated-User: $$1tz_xxnxz085nc1qk X-Complaints-To: abuse@x-privat.org Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsfeed.straub-nv.de!newsfeed.x-privat.org!x-privat.org!not-for-mail Xref: x330-a1.tempe.blueboxinc.net comp.lang.c++:12835 "Asger Joergensen" writes: > Hi Hello, > I'm not that used to the stl so I ask to be on the safe side: > > typedef std::vector TMembuf; > > TMembuf buf(1000); > char* p = &buf[0]; > > I have tested it and it seem to work fine just like: > > char* p = new char[1000]; > > and without the delete[]; > > But is there any downside to doing like this ? This is not recommended, of course. You didn't even use any of the vector facilities, in this case. Vectors provide the method reserve(). However if you want a buffer, there's circular buffer in boost library. -- Carl Lei (XeCycle) Department of Physics, Shanghai Jiao Tong University OpenPGP public key: 7795E591 Fingerprint: 1FB6 7F1F D45D F681 C845 27F7 8D71 8EC4 7795 E591