|
-
May 10th, 2006, 01:28 PM
#31
Re: array vs vector
 Originally Posted by dcjr84
There is a simple reason reason to initialize an array, and that's because
if left uninitialized you just don't know what the elements contain.
This is very untrue, and your whole post is based around this argument.
It shuld be initialised. But not by vector. By user. having an array of 0's is also garbage.
-
May 10th, 2006, 01:29 PM
#32
Re: array vs vector
 Originally Posted by laserlight
It isnt garbage since in this case the programmer requires a vector of a million 10s.
... so you just created million 10's while you only need the number 10, good job.
-
May 10th, 2006, 01:31 PM
#33
Re: array vs vector
It shuld be initialised. But not by vector. By user. having an array of 0's is also garbage.
If the user writes:
Code:
std::vector<int> x(1000000);
Then the user is creating a vector of a million 0s. It is as simple as that. What the user requests be done surely cannot be garbage.
-
May 10th, 2006, 01:33 PM
#34
Re: array vs vector
... so you just created million 10's while you only need the number 10, good job.
If I only need the number 10 stored in a variable, I would write:
If I needed an int vector with a capacity of million elements, the first of which I currently know to be 10, I would write:
Code:
std::vector<int> x;
x.reserve(1000000);
x.push_back(10);
The above vector only has a size of 1, and rightly so since only 1 int is what I need at the moment.
This is, frankly, much ado about nothing.
-
May 10th, 2006, 01:35 PM
#35
Re: array vs vector
you know im not going to discuss anymore, if you guys dont even want to try think my position fine, stay one minded.
-
May 10th, 2006, 01:37 PM
#36
Re: array vs vector
dcjr84
I disagree with your reasoning. Having initialized values is important, but not because it allows sloppy code to work. In your case you better should have array initialized to something like minimum possible value of integer to show error right when it occurs. Having consistent result guarantees repeatability of error behaviour.
"Programs must be written for people to read, and only incidentally for machines to execute."
-
May 10th, 2006, 01:41 PM
#37
Re: array vs vector
Vectors, and especially arrays, should always be
initialized. period. Doesn't matter if its by the user or done
automatically by it's implementation. The performance impact is
negligible.
And even if it did reduce performance; I would rather
have a program that runs at 50% speed but is guaranteed to
an extremely high percentage to never make a mistake
in calculation because it might at some point use an unintialized
garbage value from an array or vector, than have one that runs
at 100% speed but the results might be incorrect at some point
because there is the possibility it might use an uninitialized element
from an array or vector.
Last edited by dcjr84; May 10th, 2006 at 01:45 PM.
Please rate my post if you felt it was helpful
-
May 10th, 2006, 01:41 PM
#38
Re: array vs vector
 Originally Posted by RoboTact
dcjr84
I disagree with your reasoning. Having initialized values is important, but not because it allows sloppy code to work. In your case you better should have array initialized to something like minimum possible value of integer to show error right when it occurs. Having consistent result guarantees repeatability of error behaviour.
yes i very agree. but in my opinion this is a user fault not an array object fault.
-
May 10th, 2006, 01:46 PM
#39
Re: array vs vector
 Originally Posted by dcjr84
Vectors, and especially arrays, should always be
initialized. period.
More generally, nondetermenistic behaviour shouldn't be exposed. If said uninitialized value lurks shot within object and isn't visible outside it, fine, as far as object itself works right.
"Programs must be written for people to read, and only incidentally for machines to execute."
-
May 10th, 2006, 01:46 PM
#40
Re: array vs vector
 Originally Posted by dcjr84
And even if it did reduce performance; I would rather
have a program that runs at 50% speed but is guaranteed to
an extremely high percentage to never make a mistake
in calculation, than have one that runs at 100% speed but
the results might be incorrect at some point because there
is the possible it might use an uninitialized element from an
array or vector.
This is an programmers fault in this case. And will not happen in good desgined software. Doesnt mather if it is a vector or not having 0's is still garbage and will get wrong calculations if software is not rightly designed. Besides arrays are almost never used for calculations, but mostly for storing objects for games. POD's for images, files etc.
Using vector will only decrease performance, and if this is not visible, the cpu cycles will waiste energy, and decrease computer's life time.
-
May 10th, 2006, 01:47 PM
#41
Re: array vs vector
 Originally Posted by Mitsukai
yes i very agree. but in my opinion this is a user fault not an array object fault.
There is no fault in question, is there?
"Programs must be written for people to read, and only incidentally for machines to execute."
-
May 10th, 2006, 01:50 PM
#42
Re: array vs vector
 Originally Posted by Mitsukai
Besides arrays are almost never used for calculations, but mostly for storing objects for games. POD's for images, files etc.
Using vector will only decrease performance, and if this is not visible, the cpu cycles will waiste energy, and decrease computer's life time.
You should reconsider level of conviction in such assertions.
"Programs must be written for people to read, and only incidentally for machines to execute."
-
May 10th, 2006, 01:51 PM
#43
Re: array vs vector
I guess i late in game now
The sicussion went somewhere else than original post. though first i agree with Paul we need to find out how these timing taken.
>>yes i very agree. but in my opinion this is a user fault not an array object fault.
The evolution of all programming language is based on easy to use & hard to make mistkes by "user". There no need to use C++. We can do all things in C & assembly. Lets imagine handling memory managment in C for all the objects than using C'tor & D'tor. "user" is more likely to make the mistakes in C. In think there is no harm in providing automatic initilation to some value rather than having uninitiliaze. I bet for most of the application the overhead is very very less.
Vinod
-
May 10th, 2006, 01:55 PM
#44
Re: array vs vector
 Originally Posted by aewarnick
There is absolutely no need for vector to initialize elements to 0. Now I'm REALLY glad I created and use my own Array class.
But, if you use vector::reserve, then it does create an uninitialised array. It's just that you can't randomly access the values until you put something in it, and then only the values that you've added. This is a good thing. Plus, as has already been pointed out to you, your "vector replacement" is seriously flawed.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
May 10th, 2006, 01:57 PM
#45
Re: array vs vector
Using vector will only decrease performance, and if this is not visible, the cpu cycles will waiste energy, and decrease computer's life time.
Again, this is negligible. Hundreds if not thousands of cpu cycles are
probably wasted every minute of every day by your computer for a
multitude of reasons: bad CPU scheduler decisions, etc, etc. I highly
doubt that using a vector is going to decrease your computers lifetime
by so much that it would even be noticeable.
Please rate my post if you felt it was helpful
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|