473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Uninitialized vector?

int a; // #1 uninitialized value
int a(0) // #2 zero-initialized value
std::vector<int> v(10); // #3 zero-initialized values (why?)
std::vector<int> v(10, 0); // #4 zero-initialized values

How do I construct a vector of ints without initializing them to zero? Why
is #3 zero-initializing the ints? This is unintutitive, unexpected and slow.
If one wanted to intialize he would use syntax #4.

World would be better if int() created uninitialized value.

cheers,
Marcin
Aug 14 '05 #1
3 5956
Marcin Kalicinski wrote:
int a; // #1 uninitialized value
int a(0) // #2 zero-initialized value
std::vector<int> v(10); // #3 zero-initialized values (why?)
std::vector<int> v(10, 0); // #4 zero-initialized values

How do I construct a vector of ints without initializing them to zero?
You can't, short of providing some other initialization value instead.
Why is #3 zero-initializing the ints?
Because vectors may hold arbitrary objects not just PODs and it's not
very sensible to initialize general objects with garbage memory. They
need to be default constructed if not explicitly constructed.
This is unintutitive, unexpected and slow.
If one wanted to intialize he would use syntax #4.
I find it to be neither unintuitive nor unexpected. As for slow that's
debatable (though I wouldn't assume it until proven so) but also
avoidable if it's really a concern. You can use an array. Or you can
use a vector and reserve the required amount of space without putting
anything into it.
World would be better if int() created uninitialized value.
Maybe, but though it's not obvious how much you gain from this here
since vector will still have to construct an int.

cheers,
Marcin


Mark
Aug 14 '05 #2
"Marcin Kalicinski" <ka****@poczta.onet.pl> wrote in message
news:x%****************@newsfe2-win.ntli.net...
int a; // #1 uninitialized value
int a(0) // #2 zero-initialized value
std::vector<int> v(10); // #3 zero-initialized values (why?)
std::vector<int> v(10, 0); // #4 zero-initialized values

How do I construct a vector of ints without initializing them to zero?
std::vector int v;
v.reserve(10);

However, v.size() will still return 0 and v.at(0) will still throw. In other
words, you have allocated at least enough room for 10 integers without
initializing but the vector is still of length 0. You can write 10 values to
v without any reallocation using v.push_back or v.insert. Then you will have
what you are looking for.
Why is #3 zero-initializing the ints? This is unintutitive, unexpected and
slow.
If you want a collection class for objects and a separate collection class
for each built in type you should try Java. C++ collection classes work on
both built in types and user defined types (with some restrictions).
If one wanted to intialize he would use syntax #4.

World would be better if int() created uninitialized value.
Good news: the world is better than you think!
cheers,
Marcin


--
Cy
http://home.rochester.rr.com/cyhome/
Aug 14 '05 #3
"Marcin Kalicinski" <ka****@poczta.onet.pl> wrote in message
news:x%****************@newsfe2-win.ntli.net...
World would be better if int() created uninitialized value.


Why do you think so? If that were the case, then

int x = int();

would be undefined behavior, because it would attempt to copy an
uninitialized value. For that matter, I can't think of any context in which
it would be well-defined to use int(), so why bother allowing it at all?
Aug 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: rk | last post by:
Hi, I'm a beginner for perl/cgi programs and i tried to write a cgi script and when i ran it, i got the following error. But when i verified it from the book i typed exactly whatever it is there...
2
by: Liang | last post by:
Hi, I use "defined $r_libs->{$name}" to check first if a key exists in a hash table. But Perl gives a warning WHENEVER the key exists: "Use of uninitialized value". Would u please help to...
13
by: rswanster | last post by:
When I compile and run the following: #include <iostream> int main() { bool f; std::cout << f << std::endl; f = not(f); std::cout << f << std::endl; }
5
by: Michael | last post by:
Hi, once I read here that it is not 'a good idea' to pass variables that are not initialized to a function. I have void something ( double *vector ); ....
12
by: jyu.james | last post by:
I'm trying to detect reads of uninitialized global variables (that are declared in one file, and used in another as an extern). I know that ANSI C initializes all global variables to 0, however,...
3
by: julien | last post by:
Hello, Is it possible if a boolean was initialized or not? For other types of variable, I usually check if it is null. But this not possible for a boolean. Thank you Julien
21
by: sanjaymeher | last post by:
Hi, Right now addDynamicMemory(char **ptr, int size) method can able to handle if input ptr is intitialized to NULL or something. But how to improve this method to handle uninitialized pointed...
2
by: pnsreee | last post by:
Hi All, I have the following code. The array @sub_data will contain integers or a string"NO". I have to validate if the array contain integer. If it contain "NO" then no need to validate. ...
6
by: samthemist | last post by:
When perl comes out with errors like this: Use of uninitialized value in concatenation (.) or string at NQII.pl line 18, <STDIN> line 2. Argument "" isn't numeric in numeric ne (!=) at NQII.pl...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.