Re: Type cast problem with VC++ 2005 Express Edition
"Tim Roberts" <timr@probo.com>, iletisinde sunu yazdi,
news:fbfmh5p0d0bjnujo55h0s9g2tj1hi9jrup@4ax.com...
"aslan" <aslanski2002@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@probo.com
Providenza & Boekelheide, Inc.