Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #12827 > unrolled thread
| Started by | "Asger Joergensen" <Junk@Asger-P.dk> |
|---|---|
| First post | 2011-12-27 13:26 +0000 |
| Last post | 2011-12-28 08:56 -0500 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.c++
vector as a char buffer ? "Asger Joergensen" <Junk@Asger-P.dk> - 2011-12-27 13:26 +0000
Re: vector as a char buffer ? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-27 09:04 -0500
Re: vector as a char buffer ? "Asger Joergensen" <Junk@Asger-P.dk> - 2011-12-27 20:25 +0000
Re: vector as a char buffer ? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-12-27 21:05 +0000
Re: vector as a char buffer ? XeCycle <xecycle@gmail.com> - 2011-12-28 12:27 +0800
Re: vector as a char buffer ? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-28 08:56 -0500
| From | "Asger Joergensen" <Junk@Asger-P.dk> |
|---|---|
| Date | 2011-12-27 13:26 +0000 |
| Subject | vector as a char buffer ? |
| Message-ID | <xn0hncrwwbo878g000@news.astraweb.com> |
Hi I'm not that used to the stl so I ask to be on the safe side: typedef std::vector<char> 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 ? Thanks in advance Best regards Asger-P
[toc] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-12-27 09:04 -0500 |
| Message-ID | <jdcj91$eaa$1@dont-email.me> |
| In reply to | #12827 |
On 12/27/2011 8:26 AM, Asger Joergensen wrote: > I'm not that used to the stl so I ask to be on the safe side: > > typedef std::vector<char> 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 ? Not really. Just remember that 'p' is not owned by you and that its validity is tightly linked to the lifetime of 'buf'. V -- I do not respond to top-posted replies, please don't ask
[toc] | [prev] | [next] | [standalone]
| From | "Asger Joergensen" <Junk@Asger-P.dk> |
|---|---|
| Date | 2011-12-27 20:25 +0000 |
| Message-ID | <xn0hnd2ycc377hv001@news.astraweb.com> |
| In reply to | #12828 |
Hi Victor Victor Bazarov wrote: > On 12/27/2011 8:26 AM, Asger Joergensen wrote: > > I'm not that used to the stl so I ask to be on the safe side: > > > >typedef std::vector<char> 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 ? > > Not really. Just remember that 'p' is not owned by you and that its validity is > tightly linked to the lifetime of 'buf'. Thanks, and yes I know that I can only use p withing the scoope of buf. Best regards Asger-P
[toc] | [prev] | [next] | [standalone]
| From | Jorgen Grahn <grahn+nntp@snipabacken.se> |
|---|---|
| Date | 2011-12-27 21:05 +0000 |
| Message-ID | <slrnjfkcop.1ls.grahn+nntp@frailea.sa.invalid> |
| In reply to | #12828 |
On Tue, 2011-12-27, Victor Bazarov wrote: > On 12/27/2011 8:26 AM, Asger Joergensen wrote: >> I'm not that used to the stl so I ask to be on the safe side: >> >> typedef std::vector<char> 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 ? > > Not really. Just remember that 'p' is not owned by you and that its > validity is tightly linked to the lifetime of 'buf'. And to 'buf' not being resized, swapped etc. But it's fairly easy to manage such things -- easier than to deal with new[] IMHO. /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o .
[toc] | [prev] | [next] | [standalone]
| From | XeCycle <xecycle@gmail.com> |
|---|---|
| Date | 2011-12-28 12:27 +0800 |
| Message-ID | <87r4zp9v7d.fsf@gmail.com> |
| In reply to | #12827 |
"Asger Joergensen" <Junk@Asger-P.dk> writes: > Hi Hello, > I'm not that used to the stl so I ask to be on the safe side: > > typedef std::vector<char> 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
[toc] | [prev] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-12-28 08:56 -0500 |
| Message-ID | <jdf76m$qvn$1@dont-email.me> |
| In reply to | #12835 |
On 12/27/2011 11:27 PM, XeCycle wrote: > "Asger Joergensen"<Junk@Asger-P.dk> writes: > >> Hi > > Hello, > >> I'm not that used to the stl so I ask to be on the safe side: >> >> typedef std::vector<char> 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(). WHAT is not recommended? And why the hell not? And what use would Asger have for 'reserve()'? std::vector has a proper constructor that allocates the buffer and makes it ready to use. > However if you want a buffer, there's circular buffer in boost > library. Asger needed something analogous to an array allocated by 'new[]'. A standard vector is just about what the library can offer. Boost does not exist on every platform, while std::vector does. There is no sense in using a non-portable library when the Standard library suffices. V -- I do not respond to top-posted replies, please don't ask
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web