From: aslan on

"David Wilkinson" <no-reply(a)effisols.com>, iletisinde şunu yazdı,
news:%23UmhHzCdKHA.2160(a)TK2MSFTNGP02.phx.gbl...
> aslan wrote:
>> OK I tried something else.
>>
>> struct bool_struct
>> {
>> bool a[32];
>> };
>> int main(int argc, char**argv)
>> {
>> bool_struct* p=new bool_struct;
>> return 0;
>> }
>>
>> so "new bool_struct" ends up by calling the following cb=32.
>>
>> void * operator new( unsigned int cb )
>> {
>> void *res = _nh_malloc( cb, 1 );
>>
>> return res;
>> }
>>
>> So 32 byte is allocated for "bool a[32];"
>>
>>
>> Also the following quote from MSDN help installed with VC++ 6 which
>> confirms the size of 1 byte for VC++ 6;
>>
>> Microsoft Specific
>>
>> In Visual C++4.2, the Standard C++ header files contained a typedef that
>> equated bool with int. In Visual C++ 5.0 and later, bool is implemented
>> as a built-in type with a size of 1 byte. That means that for Visual C++
>> 4.2, a call of sizeof(bool) yields 4, while in Visual C++ 5.0 and later,
>> the same call yields 1. This can cause memory corruption problems if you
>> have defined structure members of type bool in Visual C++ 4.2 and are
>> mixing object files (OBJ) and/or DLLs built with the 4.2 and 5.0 or later
>> compilers.
>>
>> END Microsoft Specific
>
> All this is about the bool type. In VC6 and beyond, sieof(bool) is 1.
OK. Tim Roberts was talking about MS STL implementation whereas I use SGI
STL implementation so I didn't notice it then.
>
> The complication is that vector<bool> is not always implemented in the
> straightforward way that it is for other types. Though it should be, IMHO.
>
> --
> David Wilkinson
> Visual C++ MVP

From: Tim Roberts on
"aslan" <aslanski2002(a)yahoo.com> wrote:
>>
>OK I tried something else.
>
>struct bool_struct
>{
> bool a[32];
>};

That is ENTIRELY different. Seriously, are you really unable to see that?
That is a standard C++ array. That is NOT an STL vector. ENTIRELY
different.

>So 32 byte is allocated for "bool a[32];"

Absolutely. And 128 bytes is allocated for an "int a[32]". Neither point
is relevent to the discussion.

The POINT we were talking about is this:

std::vector<bool> a;
a.resize(32);

THAT allocates 4 bytes, not 32 bytes.

>Also the following quote from MSDN help installed with VC++ 6 which confirms
>the size of 1 byte for VC++ 6;

Yes, but you are conveniently and repeatedly ignoring the most important
point. "std::vector<bool> a" is NOT the same as "bool a[32]". There is an
special optimization allowed for std::vectors of bool, which allows them to
be packed 8 to a byte. That CANNOT be done for "bool a[32]". For example,
this is legal:

std::vector<int> vi;
vi.resize(32);
int * pi = &vi[0];

After this, pi will point to a 128-byte piece of memory, with room for 32
ints. But when you do this:

std::vector<bool> vb;
vb.resize(32);
bool * pb = &vb[0];

you get a compile-time errror, to prevent you from accidentally overwriting
memory. You were trying to the same kind of thing using vb.begin(), but
the iterator for a std::vector<bool> is not just a pointer. It have to
maintain the the byte offset and the bit number of the current spot. It's
NOT just a pointer to bool.
--
Tim Roberts, timr(a)probo.com
Providenza & Boekelheide, Inc.
From: Tim Roberts on
"aslan" <aslanski2002(a)yahoo.com> wrote:
>
>OK. Tim Roberts was talking about MS STL implementation whereas I use SGI
>STL implementation so I didn't notice it then.

I'm not convinced. The STL implementation in VC++6 was written by PJ
Plauger and contains HP's copyright. HP and SGI cooperated on STL.

Here's a way to tell. Try to compile this:

std::vector<bool> vb;
vb.resize(32);
bool * pb = &vb[0];

If that compiles, then your implementation of STL does not have this
optimization, and the vector probably occupies 32 bytes. If that gets a
compile-time error, then your STL has the same optimization as Microsoft's,
and the vector occupies 4 bytes.
--
Tim Roberts, timr(a)probo.com
Providenza & Boekelheide, Inc.
From: aslan on

"Tim Roberts" <timr(a)probo.com>, iletisinde sunu yazdi,
news:fbfmh5p0d0bjnujo55h0s9g2tj1hi9jrup(a)4ax.com...
> "aslan" <aslanski2002(a)yahoo.com> wrote:
>>>
>>OK I tried something else.
>>
>>struct bool_struct
>>{
>> bool a[32];
>>};
>
> That is ENTIRELY different. Seriously, are you really unable to see that?
> That is a standard C++ array. That is NOT an STL vector. ENTIRELY
> different.
>
Yes, Sir.
>>So 32 byte is allocated for "bool a[32];"
>
> Absolutely. And 128 bytes is allocated for an "int a[32]". Neither point
> is relevent to the discussion.
>
> The POINT we were talking about is this:
>
> std::vector<bool> a;
> a.resize(32);
>
> THAT allocates 4 bytes, not 32 bytes.
>
>>Also the following quote from MSDN help installed with VC++ 6 which
>>confirms
>>the size of 1 byte for VC++ 6;
>
> Yes, but you are conveniently and repeatedly ignoring the most important
> point. "std::vector<bool> a" is NOT the same as "bool a[32]". There is
> an
> special optimization allowed for std::vectors of bool, which allows them
> to
> be packed 8 to a byte. That CANNOT be done for "bool a[32]". For
> example,
> this is legal:
>
> std::vector<int> vi;
> vi.resize(32);
> int * pi = &vi[0];
>
> After this, pi will point to a 128-byte piece of memory, with room for 32
> ints. But when you do this:
>
> std::vector<bool> vb;
> vb.resize(32);
> bool * pb = &vb[0];
>
No Sir,
I have tried it and I don't get any compile or run-time error with SGI STL
on my machine.

The following is fine for me:

void f()
{
std::vector<bool> vb;
vb.resize(32);
bool * pb = &vb[0];
pb[31]=true;
}

> you get a compile-time errror, to prevent you from accidentally
> overwriting
> memory. You were trying to the same kind of thing using vb.begin(), but
> the iterator for a std::vector<bool> is not just a pointer. It have to
> maintain the the byte offset and the bit number of the current spot. It's
> NOT just a pointer to bool.
> --
> Tim Roberts, timr(a)probo.com
> Providenza & Boekelheide, Inc.

From: Ulrich Eckhardt on
aslan wrote:
> "Tim Roberts" <timr(a)probo.com>, iletisinde sunu yazdi,
>> Yes, but you are conveniently and repeatedly ignoring the most important
>> point. "std::vector<bool> a" is NOT the same as "bool a[32]". There is
>> an special optimization allowed for std::vectors of bool, which allows
>> them to be packed 8 to a byte. That CANNOT be done for "bool a[32]".
>> For example, this is legal:
>>
>> std::vector<int> vi;
>> vi.resize(32);
>> int * pi = &vi[0];
>>
>> After this, pi will point to a 128-byte piece of memory, with room for 32
>> ints. But when you do this:
>>
>> std::vector<bool> vb;
>> vb.resize(32);
>> bool * pb = &vb[0];
>>
> No Sir,
> I have tried it and I don't get any compile or run-time error with SGI STL
> on my machine.

The STL is not standard C++. You can install it, but if you then rely on
things where it differs from the C++ standard library, you get exactly what
you got. Let me repeat that: The STL, as far as the C++ standard is
concerned is just a library. Since it also puts symbols into the 'std'
namespace, it could even be called a bad library, as that namespace is
reserved.

Aslan, why are you using the STL anyway? Development of it was abandoned
more than half a decade ago. Most parts of it have been incorporated into
the C++ standard (which is probably the reason many people confuse the
two), so they are available in a standard-conformant way there. I see no
reason to use the STL in modern code.

Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932