Using vector<unsigned char> as raw memory
flag
Messages 41 - 50 of 64 - Collapse all
/groups/adfetch?hl=en&adid=erYZHxEAAABtmNrPJuYU5shzHlDLW5AqAOjDTfX8sw3zbKwVokmbOA
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
41.  Dylan Nicholson  
View profile  
 More options Feb 23 2004, 10:30 pm
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
brang...@cix.co.uk (Dave Harris) wrote in message

<news:memo.20040223121819.1892B@brangdon.m>...
> wizof...@hotmail.com (Dylan Nicholson) wrote (abridged):
> >  > 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
> cannot. A given platform might not even have a C compiler. Or it
> may not have one from the same vendor. Typically a vendor's C++
> compiler isn't even layout-compatible with itself, if you fiddle
> with the compile options between compiles.

True enough, but that would certainly be outside of the what any
standard should dictate.

> > If there were a compiler for which PODs declared in C++ code
> > wouldn't work when passed to C code (and vice versa), a hellavu
> > lot of code would surely be broken.

> It's Quality of Implementation. Vendors keep compatibility with C
> for commercial reasons rather than because the standard requires it.

Well, I suppose - it's always bemused me the way programmers fret over
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 ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
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.
42.  Francis Glassborow  
View profile  
 More options Feb 24 2004, 2:29 pm
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>...

> > The C++ standard does not require C compatibility for PODs.

>It doesn't? What about if the declaration is extern "C"?
>If there were a compiler for which PODs declared in C++ code wouldn't
>work when passed to C code (and vice versa), a hellavu lot of code
>would surely be broken.

But extern "C" does not guarantee your code will link with just any 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).

--
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
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.
43.  Gabriel Dos Reis  
View profile  
 More options Feb 23 2004, 6:33 am
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

ka...@gabi-soft.fr writes:

| 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.

--
                                                        Gabriel Dos Reis
                                                         g...@cs.tamu.edu
         Texas A&M University -- Computer Science Department
        301, Bright Building -- College Station, TX 77843-3112

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
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.
44.  kanze  
View profile  
 More options Feb 24 2004, 2:21 pm
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:
> | 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.

It doesn't.  But since the size of the object isn't known until runtime,
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.)

--
James Kanze           GABI Software        mailto:ka...@gabi-soft.fr
Conseils en informatique orientée objet/     http://www.gabi-soft.fr
                    Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
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.
45.  Maciej Sobczak  
View profile  
 More options Feb 6 2004, 2:56 pm
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,

ka...@gabi-soft.fr wrote:
> 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).

Consider:

3.9/2 states that POD can be copied back and forth using array of char
OR unsigned char, preserving its value.

3.9.1/1:
"A char [...] AND unsigned char [...] have the same object representation."

There are other places where such properties are defined to be the 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.

It was my habit to use unsigned char, but I resigned from it and now
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.

I will be glad to know your opinion on this.

--
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
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.
46.  Gennaro Prota  
View profile  
 More options Feb 7 2004, 6:23 am
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
 >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).

This is a thorny issue. The question comes down to whether a signed
char may have padding bits and/or trap representations. I'm not sure
the C++ standard was written having in mind a definite answer to that
question. I guess the committee simply thought to make explicit, or
reword, what were the C90 requirements.

However, C90 wasn't particularly clear on this point and there was a
fundamental DR by Clive Feather:

http://wwwold.dkuug.dk/JTC1/SC22/WG14/www/docs/dr_069.html

The response clarified many things that were in the committee
intentions but not explicitly spelt out in the standard text (note:
they didn't mean to change/fix the rules; just to clarify what was
always the case for any conforming C90 implementation). Based on that,
C99 is much more explicit and clear on this topics and there's text,
written by Clive Feather himself, that explicitly allows trap
representations in signed chars (see 6.2.6.2). A C99 implementation
where

    CHAR_BIT  =  10
    UCHAR_MAX =  1023
    SCHAR_MIN = -128
    SCHAR_MAX =  127

would be conforming (I don't know if it really exists, though).

In that scenario, and since C++ certainly aims at C compatibility in
this delicate area, I would say that if the C++ standard says anything
different from C99 that isn't intentional.

PS: You won't believe me but I simply gave up consulting the C++
standard for signed vs. unsigned char issues a long time ago, for the
reasons I said above; never noticed there's a related DR:

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,
and it was there :) As you may see, it confirms that much of the
wording you quote was written without a clear intent.

Genny.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
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.
47.  kanze  
View profile  
 More options Feb 10 2004, 2:40 pm
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
Maciej Sobczak <no.s...@no.spam.com> wrote in message

<news:bvvjc0$gka$1@atlantis.news.tpi.pl>...

> ka...@gabi-soft.fr wrote:
> > 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).

In C++, no.  At least not from a language standpoint, both are equally
good.

> Consider:
> 3.9/2 states that POD can be copied back and forth using array of char
> OR unsigned char, preserving its value.

The equivalent guarantee in C only holds for unsigned char.  A signed
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
for some reason, they are different.

> 3.9.1/1:
> "A char [...] AND unsigned char [...] have the same object representation."

For a very strange meaning of "object representation"; the text
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
types, all bits of the object representation participate in the value
representation."  In the C standard, the equivalent guarantee, for this
and for the memcpy, only holds for unsigned char.  In the C++ standard,
the next sentence also gives food for thought: "For unsigned character
types, all possible bit patterns of the value representation represent
numbers."  I'm not too sure what this is meant to mean: could a 1's
complement machine arrange for all 0's to be positive, and trap on a
negative 0?  (But this would invalidate the memcpy guarantee, supposing
plain char was signed.)

> There are other places where such properties are defined to be the
> 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.

I think, despite the one worrying sentence I quoted above, that char and
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
characters; if I want a small integer, I will use either unsigned char
or signed char, and if I want raw memory, I use unsigned char.
Independantly of what the language allows, this seems to correspond to a
long tradition, and thus communicates my intent better.

> It was my habit to use unsigned char, but I resigned from it and now
> 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.

The "normal" convention when passing a pointer to raw memory is to use
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*.
However, the interface requires that there some free bytes behind the
dirent -- I can't just pass it "new dirent".  

Formally, the interface takes a struct ending with a VLA.  But we don't
have VLA's in C++ yet.  The correct solution would be to write a small
wrapper in C, and call it from C++.  But I don't have access to a C
compiler, so I'm stuck.  (And to be honest, the interface was designed
before VLA was adopted in C, and was intended to use some awful C
hackery, that is in fact undefined behavior in C, but happens to work on
most systems.)

> 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.

--
James Kanze           GABI Software        mailto:ka...@gabi-soft.fr
Conseils en informatique orientée objet/     http://www.gabi-soft.fr
                    Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
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.
48.  Francis Glassborow  
View profile  
 More options Feb 6 2004, 3:13 pm
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,
>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
>strange memory tricks.  The second parameter of readdir_r is the one
>that's giving me the problems -- and the actual size needed isn't known
>until runtime, because it depends on the filesystem where the directory
>is hosted.  So I need to allocate dynamcally, and I need RAII (and my
>compiler is too old to support Boost, so scoped_array isn't an option).

what I do not understand is why you do not just use operator new direct:

unsigned char * ptr = (unsigned char) operator new(requirement);

--
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
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.
49.  Vladimir Kouznetsov  
View profile  
 More options Feb 7 2004, 6:23 am
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
"Francis Glassborow" <fran...@robinton.demon.co.uk> wrote in message

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:
 >
 > unsigned char * ptr = (unsigned char) operator new(requirement);

Perhaps the storage should grow, preserving the old content.

 > Francis Glassborow      ACCU

thanks,
v

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
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.
50.  kanze  
View profile  
 More options Feb 9 2004, 7:43 pm
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
Francis Glassborow <fran...@robinton.demon.co.uk> wrote in message

<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
enough that it isn't really a problem.  I just liked the idea that all
uses of new[] could be subsumed by std::vector; until this case, it has
always been the case in my code.

--
James Kanze           GABI Software        mailto:ka...@gabi-soft.fr
Conseils en informatique orientée objet/     http://www.gabi-soft.fr
                    Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
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