Newsgroups: comp.lang.c++.moderated
From: wizof...@hotmail.com (Dylan Nicholson)
Date: 23 Feb 2004 22:32:49 -0500
Local: Mon, Feb 23 2004 10:32 pm
Subject: Re: Using vector<unsigned char> as raw memory
<news:memo.20040223121819.1892B@brangdon.m>...
> wizof...@hotmail.com (Dylan Nicholson) wrote (abridged): True enough, but that would certainly be outside of the what any > > > The C++ standard does not require C compatibility for PODs. > > It doesn't? What about if the declaration is extern "C"? > These things /enable/ compatibility but don't require it. They standard should dictate. > > If there were a compiler for which PODs declared in C++ code > It's Quality of Implementation. Vendors keep compatibility with C standards-compliance when they frequently make assumptions about compiler behaviour that are not part of any standard (and futhermore assume that standards-compliant code is necessarily more likely to be more portable). If standards were so important, then all modern OSes should have set-in-stone C ABI's, and any compiler written for them should be able to label itself as "OS XXX-C ABI-compliant". And ideally of course, all modern OSes should have set-in-stone C++ ABI's. One can but hope... Dylan [ See http://www.gotw.ca/resources/clcm.htm for info about ] You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.c++.moderated
From: Francis Glassborow <fran...@robinton.demon.co.uk>
Date: 24 Feb 2004 14:31:59 -0500
Local: Tues, Feb 24 2004 2:31 pm
Subject: Re: Using vector<unsigned char> as raw memory
In message <7d428a77.0402221746.713a0...@posting.google.com>, Dylan
Nicholson <wizof...@hotmail.com> writes >brang...@cix.co.uk (Dave Harris) wrote in message <news:memo.20040222033438.780C@brangdon.m>... But extern "C" does not guarantee your code will link with just any C. > > The C++ standard does not require C compatibility for PODs. >It doesn't? What about if the declaration is extern "C"? It assumes a compatible compiler. The PODs declared in C++ code must work with a compatible C compiler (if there is one, but C++ does not require there to be). -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.c++.moderated
From: Gabriel Dos Reis <g...@cs.tamu.edu>
Date: 23 Feb 2004 06:36:06 -0500
Local: Mon, Feb 23 2004 6:36 am
Subject: Re: Using vector<unsigned char> as raw memory
| I also thought that the question would be of more general interest. One
| of the motivations behind the requirement for contiguity in std::vector, | if not THE motivation, was support for cases where C style arrays were | being used (legacy and C interfaces, for example). I stumbled on a case | where a C style interface was being used that didn't seem to be covered. std::vector<T> is, no doubt, meant to be a safe good replacement for -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.c++.moderated
From: ka...@gabi-soft.fr
Date: 24 Feb 2004 14:24:31 -0500
Local: Tues, Feb 24 2004 2:24 pm
Subject: Re: Using vector<unsigned char> as raw memory
Gabriel Dos Reis <g...@cs.tamu.edu> wrote in message
<news:m3hdxiq169.fsf@merlin.cs.tamu.edu>... > ka...@gabi-soft.fr writes: It doesn't. But since the size of the object isn't known until runtime, > | I also thought that the question would be of more general interest. > | One of the motivations behind the requirement for contiguity in > | std::vector, if not THE motivation, was support for cases where C > | style arrays were being used (legacy and C interfaces, for example). > | I stumbled on a case where a C style interface was being used that > | didn't seem to be covered. > std::vector<T> is, no doubt, meant to be a safe good replacement for > C-arrays -- for the most uses. I however doubt that even a C-array of > char is required to have its address suitably aligned to meet the > requirement of an arbitrary type. the alternative was a C-style object created with malloc, and the return value of malloc is guaranteed suitably aligned. I could also use the operator new function, or new char[]. The only real advantage of std::vector here was that it implements RAII, all by itself. (In this case, boost::array_ptr would also be an option. Except that Boost won't compile with my antediluvian compilers -- even our version of the STL has been significantly compromized in order to pass.) -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.c++.moderated
From: Maciej Sobczak <no.s...@no.spam.com>
Date: 6 Feb 2004 14:58:55 -0500
Local: Fri, Feb 6 2004 2:58 pm
Subject: Re: Using vector<unsigned char> as raw memory
Hi,
I cannot answer your specific question (I can only share your assumptions that it is OK), but there is a place for spinning off a related question. Why do you want to use unsigned char as the underlying type? Is it any Consider: 3.9/2 states that POD can be copied back and forth using array of char 3.9.1/1: There are other places where such properties are defined to be the same The only relevant place that shows some assymetry is 3.9/4: This would state that unsigned char is better than plain char for "raw It was my habit to use unsigned char, but I resigned from it and now I will be glad to know your opinion on this. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.c++.moderated
From: Gennaro Prota <gennaro_pr...@yahoo.com>
Date: 7 Feb 2004 06:26:24 -0500
Local: Sat, Feb 7 2004 6:26 am
Subject: Re: Using vector<unsigned char> as raw memory
On 6 Feb 2004 14:58:55 -0500, Maciej Sobczak <no.s...@no.spam.com>
wrote: >I cannot answer your specific question (I can only share your This is a thorny issue. The question comes down to whether a signed However, C90 wasn't particularly clear on this point and there was a http://wwwold.dkuug.dk/JTC1/SC22/WG14/www/docs/dr_069.html The response clarified many things that were in the committee CHAR_BIT = 10 would be conforming (I don't know if it really exists, though). In that scenario, and since C++ certainly aims at C compatibility in PS: You won't believe me but I simply gave up consulting the C++ http://std.dkuug.dk/jtc1/sc22/wg21/docs/cwg_active.html#350 I had the scruple to check the DR list before hitting the send button, Genny. [ See http://www.gotw.ca/resources/clcm.htm for info about ] You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.c++.moderated
From: ka...@gabi-soft.fr
Date: 10 Feb 2004 14:43:43 -0500
Local: Tues, Feb 10 2004 2:43 pm
Subject: Re: Using vector<unsigned char> as raw memory
<news:bvvjc0$gka$1@atlantis.news.tpi.pl>...
> ka...@gabi-soft.fr wrote: In C++, no. At least not from a language standpoint, both are equally > > Is it possible to use std::vector< unsigned char > as raw memory? > I cannot answer your specific question (I can only share your > assumptions that it is OK), but there is a place for spinning off a > related question. > Why do you want to use unsigned char as the underlying type? Is it any > better than plain char when used as "raw memory" (where by "raw > memory" I mean that the only later use will involve reinterpret casts > or object copy). good. > Consider: The equivalent guarantee in C only holds for unsigned char. A signed > 3.9/2 states that POD can be copied back and forth using array of char > OR unsigned char, preserving its value. char (and thus, a plain char) is allowed to have trapping representations. I don't know the reasons for the difference between C and C++ here, but > 3.9.1/1: For a very strange meaning of "object representation"; the text > "A char [...] AND unsigned char [...] have the same object representation." immediately before this makes it plain that all that is meant is that they have the same size and alignment requirements. The key guarantee is just below (in the same paragraph): "For character > There are other places where such properties are defined to be the I think, despite the one worrying sentence I quoted above, that char and > same for both char and unsigned char. > The only relevant place that shows some assymetry is 3.9/4: "The > object representation of an object of type T is the sequence of N > unsigned char objects taken up by the object of type T, where N equals > sizeof(T)." > This would state that unsigned char is better than plain char for "raw > memory" uses, but somehow I cannot believe it due to the 3.9.1/1 cited > above. unsigned char are equally valid in C++. However: - I knew C before I knew C++, and got into the habit of unsigned char, - it also seems to be the prevailing usage. The latter point is, IMHO, very important. I only use plain char for > It was my habit to use unsigned char, but I resigned from it and now The "normal" convention when passing a pointer to raw memory is to use > consequently use char buffers when the "raw memory" is what I need. I > just found it more consistent with various API functions, where > pointer to char is expected as a buffer parameter. void*. But obviously, neither void[] nor std::vector<void> will work, so some type conversions are necessary. The actual type requested by the interface in my case is a dirent*. Formally, the interface takes a struct ending with a VLA. But we don't > I will be glad to know your opinion on this. Unless there are definite reasons for doing otherwise, stick with tradition, and what the others do. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.c++.moderated
From: Francis Glassborow <fran...@robinton.demon.co.uk>
Date: 6 Feb 2004 15:16:19 -0500
Local: Fri, Feb 6 2004 3:16 pm
Subject: Re: Using vector<unsigned char> as raw memory
In message <d6652001.0402040847.49b9...@posting.google.com>,
ka...@gabi-soft.fr writes >Is it possible to use std::vector< unsigned char > as raw memory? Or, what I do not understand is why you do not just use operator new direct: >more specifically, if I have a std::vector< unsigned char > v, is &v[0] >guaranteed to be aligned for all possible data types. (Since an >implementation is required ultimately to use operator new to obtain the >buffer, I can't see how it couldn't be in practice, but I rather doubt >that the standard gives me this guarantee, even indirectly. But maybe >this was the intent.) >For those who are curious: certain Posix functions require some very unsigned char * ptr = (unsigned char) operator new(requirement); -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.c++.moderated
From: "Vladimir Kouznetsov" <vladimir.kouznet...@ngrain.com>
Date: 7 Feb 2004 06:26:51 -0500
Local: Sat, Feb 7 2004 6:26 am
Subject: Re: Using vector<unsigned char> as raw memory
news:FrnA8HDas3IAFwJa@robinton.demon.co.uk...
> In message <d6652001.0402040847.49b9...@posting.google.com>, > ka...@gabi-soft.fr writes > >Is it possible to use std::vector< unsigned char > as raw memory? Or, > >more specifically, if I have a std::vector< unsigned char > v, is &v[0] > >guaranteed to be aligned for all possible data types. (Since an > >implementation is required ultimately to use operator new to obtain the > >buffer, I can't see how it couldn't be in practice, but I rather doubt > >that the standard gives me this guarantee, even indirectly. But maybe > >this was the intent.) > > > what I do not understand is why you do not just use operator new direct: Perhaps the storage should grow, preserving the old content. > Francis Glassborow ACCU thanks, [ See http://www.gotw.ca/resources/clcm.htm for info about ] You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.c++.moderated
From: ka...@gabi-soft.fr
Date: 9 Feb 2004 19:46:39 -0500
Local: Mon, Feb 9 2004 7:46 pm
Subject: Re: Using vector<unsigned char> as raw memory
<news:FrnA8HDas3IAFwJa@robinton.demon.co.uk>...
I could, but I'd then require a try/catch block, with an explicit call to operator delete in both the normal branch and in the catch block. This is probably what I will end up doing. The context is limited -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
| Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy |
| ©2009 Google |