Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to initialize std::vector?

Reply
Thread Tools

how to initialize std::vector?

 
 
Jacek Dziedzic
Guest
Posts: n/a
 
      01-20-2007
Victor Bazarov wrote:
> Just to explain to those who missed it, the "no" is for the last
> question (about 'memset' or 'ZeroMemory' application to a vector).
> I hope it's clearer now.
>
> My answer to how to set the contents of the vector to 0 was given
> in the same message and for whatever reason snipped away by Ivan.
>
> V


Strictly speaking, I believe it is (technically) legal
to use memset() starting at &(vector_of_int[0]) to set
the values of the vector's elements to some values
(admittedly, those who's representation consists of equal bytes).

So it would be (technically) legal to zero-initialize a
vector of ints like that, no?

Though I'd bet the default-initialization does exactly
that.

- J.
 
Reply With Quote
 
 
 
 
Ivan Novick
Guest
Posts: n/a
 
      01-20-2007
Victor Bazarov wrote:
> Ivan Novick wrote:
>> Victor Bazarov wrote:
>>> JDT wrote:
>>>> Can someone show me how to set any integer (or float) in an
>>>> std::vector as zero in a way other than using a for loop? Can we
>>>> apply memset() or ZeroMemory() to the vector?
>>> No.
>>>

>> Hmmm.... the question says how to initialize the vector.

>
> No, it doesn't. It asks how to "set any integer".

The subject of the message is how to initialize.

In which case I would say John Carson's reply is the best:

std::vector<int> v(100);

> Just to explain to those who missed it, the "no" is for the last
> question (about 'memset' or 'ZeroMemory' application to a vector).
> I hope it's clearer now.
>
> My answer to how to set the contents of the vector to 0 was given
> in the same message and for whatever reason snipped away by Ivan.

Woops, overzealous snipping on my part. Sorry.

--
Ivan
http://www.0x4849.net
 
Reply With Quote
 
 
 
 
Jacek Dziedzic
Guest
Posts: n/a
 
      01-21-2007
Jacek Dziedzic wrote:
> Victor Bazarov wrote:
>> Just to explain to those who missed it, the "no" is for the last
>> question (about 'memset' or 'ZeroMemory' application to a vector).
>> I hope it's clearer now.
>>
>> My answer to how to set the contents of the vector to 0 was given
>> in the same message and for whatever reason snipped away by Ivan.
>>
>> V

>
> Strictly speaking, I believe it is (technically) legal
> to use memset() starting at &(vector_of_int[0]) to set
> the values of the vector's elements to some values
> (admittedly, those who's representation consists of equal bytes).
>
> So it would be (technically) legal to zero-initialize a
> vector of ints like that, no?
>
> Though I'd bet the default-initialization does exactly
> that.


Just to clarify -- I obviously meant the case where the
vector is large enough (resized appropriately) so that the
memset does not trash what comes after the vector.

- J.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-22-2007
Jacek Dziedzic wrote:
> Jacek Dziedzic wrote:
>> Victor Bazarov wrote:
>>> Just to explain to those who missed it, the "no" is for the last
>>> question (about 'memset' or 'ZeroMemory' application to a vector).
>>> I hope it's clearer now.
>>>
>>> My answer to how to set the contents of the vector to 0 was given
>>> in the same message and for whatever reason snipped away by Ivan.
>>>
>>> V

>>
>> Strictly speaking, I believe it is (technically) legal
>> to use memset() starting at &(vector_of_int[0]) to set
>> the values of the vector's elements to some values
>> (admittedly, those who's representation consists of equal bytes).
>>
>> So it would be (technically) legal to zero-initialize a
>> vector of ints like that, no?
>>
>> Though I'd bet the default-initialization does exactly
>> that.

>
> Just to clarify -- I obviously meant the case where the
> vector is large enough (resized appropriately) so that the
> memset does not trash what comes after the vector.


I wouldn't bet the default initialisation does that, but it is most
likely safe to do with vectors of POD.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      01-22-2007

Jim Langston skrev:
> "JDT" <(E-Mail Removed)> wrote in message
> news:ZKfsh.68668$(E-Mail Removed). net...
> > Hi,
> >
> > Can someone show me how to set any integer (or float) in an std::vector as
> > zero in a way other than using a for loop? Can we apply memset() or
> > ZeroMemory() to the vector? Thanks for your help.
> >
> > JD

>
> You *could* use memset, but personally I don't like it. The reason being it
> can break classes.
>
> Say, for instance you have a std::vector of a simple POD class such as:
>
> class MyClass
> {
> public:
> int MyInt;
> float MyFloat;
> char MyChar;
> };
>
> std::vector<MyClass> MyVector;
>
> So you go ahead and use memset something like (this may be off):
>
> memset( &MyVector[0], 0, sizeof MyClass * MyVector.count() );
>
> Okay, it works for now.

[snip]

That depends on what you mean when it works. You can't expect MyFloat
to be zero - although it likely is on your platform, and I am not even
sure that you can expect MyInt or MyChar to be zero. (I do not believe
a number format where "all zeros" means lowest possible value is
nonconforming).

/Peter

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      01-22-2007
peter koch wrote:

>
> Jim Langston skrev:
>> "JDT" <(E-Mail Removed)> wrote in message
>> news:ZKfsh.68668$(E-Mail Removed). net...
>> > Hi,
>> >
>> > Can someone show me how to set any integer (or float) in an std::vector
>> > as
>> > zero in a way other than using a for loop? Can we apply memset() or
>> > ZeroMemory() to the vector? Thanks for your help.
>> >
>> > JD

>>
>> You *could* use memset, but personally I don't like it. The reason being
>> it can break classes.
>>
>> Say, for instance you have a std::vector of a simple POD class such as:
>>
>> class MyClass
>> {
>> public:
>> int MyInt;
>> float MyFloat;
>> char MyChar;
>> };
>>
>> std::vector<MyClass> MyVector;
>>
>> So you go ahead and use memset something like (this may be off):
>>
>> memset( &MyVector[0], 0, sizeof MyClass * MyVector.count() );
>>
>> Okay, it works for now.

> [snip]
>
> That depends on what you mean when it works. You can't expect MyFloat
> to be zero - although it likely is on your platform, and I am not even
> sure that you can expect MyInt or MyChar to be zero. (I do not believe
> a number format where "all zeros" means lowest possible value is
> nonconforming).


From the standard

[3.9.1/7]

[...] The representations of integral types shall define values by use of
a pure binary numeration system.44) [...]

[44] A positional representation for integers that uses the binary digits
0 and 1, in which the values represented by successive bits are additive,
begin with 1, and are multiplied by successive integral power of 2, except
perhaps for the bit with the highest position.
(Adapted from the American National Dictionary for Information Processing
Systems.)

[3.9.1/8]

[...] The value representation of floating-point types is
implementation-defined. [...]


Best

Kai-Uwe Bux
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      01-22-2007

Victor Bazarov wrote:
> JDT wrote:
> > Can someone show me how to set any integer (or float) in an
> > std::vector as zero in a way other than using a for loop? Can we
> > apply memset() or ZeroMemory() to the vector?

>
> No.


Actually, you can. It bypasses much of the safety of using a vector
but with care it can be done.

std::vector<int> x;
x.resize(100);

memset(&v[0], 0, sizeof(int) * 100);

Of course, there isn't much reason to do this (since resize() already
did it in a more well defined manner), but it *can* be done. There are
many other times when such techniques are reasonable even if rather
dangerous. Any time you need a buffer of some type a std::vector can
be used in place; you can pass &v[0] and know that, assuming you've
told the vector to allocate enough room, it will do what you expect.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How do you initialize signals in VHDL? Madhu VHDL 7 05-28-2008 06:43 PM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
Initialize array using file i/o procedure/function? Brandon VHDL 5 09-29-2005 05:32 PM
initialize memory units Daniel Koethe VHDL 1 11-08-2004 08:36 PM



Advertisments