Home » U++ Library support » U++ Core » Using Vector::At doesn't initialize implicit types
Using Vector::At doesn't initialize implicit types [message #17104] |
Tue, 29 July 2008 10:51 |
cbpporter
Messages: 1401 Registered: September 2007
|
Ultimate Contributor |
|
|
If I use At or SetLength, Vecor will call the default constructor for the new items created which are not manually initialized for user classes, but will leave implicit types uninitialized.
Example:
#include <Core/Core.h>
using namespace Upp;
struct Foo: public Moveable<Foo>
{
int x;
Foo() { x = 0; }
Foo(int xx) { x = xx; }
};
CONSOLE_APP_MAIN
{
Vector<int> v;
v.At(0) = 7000;
v.At(2) = 7000;
DUMP(v[0]);
DUMP(v[1]);
DUMP(v[2]);
Vector<Foo> w;
w.At(0) = Foo(7000);
w.At(2) = Foo(7000);
DUMP(w[0].x);
DUMP(w[1].x);
DUMP(w[2].x);
}
I think we should change this. It can lead to problems and nasty crashes if you have a Vector<Foo>, where Foo is a value type class, and for some reason you need to start using Vector<Foo*>. If you Vector is not initialized in order, which is not a requirement for this class, you will most likely get memory corruption when you try to access the gaps and you cant test for NULL either.
|
|
|
|
|
Re: Using Vector::At doesn't initialize implicit types [message #17109 is a reply to message #17108] |
Tue, 29 July 2008 11:13 |
mr_ped
Messages: 825 Registered: November 2005 Location: Czech Republic - Praha
|
Experienced Contributor |
|
|
cbpporter wrote on Tue, 29 July 2008 11:10 | Yes, using that At works if you want to initialize it there.
I was talking about the way user defined classes get their default constructor called, while implicit types get left initialized.
It should be more uniform.
And since user types do get their constructor called, I don't think there is any performance difference between what I'm proposing and the current situation.
|
No, it does fix your problem, try it.
...
Vector<int> v;
v.At(0, 0) = 7000;
v.At(2, 0) = 7000;
...
LOG:
v[0] = 7000
v[1] = 0
v[2] = 7000
I'm using basic types a lot, and with vectors over 1mil in size, so unwanted initialization would slow me down considerably.
Actually I think calling the user defined constructor may be inconsistent behavior, and maybe it should not happen in your example.
edit:
And the "int" nature of vector size is starting to scary me, I would rather prefer full 32b size_t (although I understand the pick behavior would interfere much more in such scheme without additional flag (increasing the memory footprint of Vector more)) :/ This new HW in recent years makes 2+GB of data to look like an easy chew, yet SW like UPP::Vector will give you some headache in such case, and you will end up probably with custom containers anyway. On 64b system the int size of Vector will become problem in future.
[Updated on: Tue, 29 July 2008 11:20] Report message to a moderator
|
|
|
|
|
Re: Using Vector::At doesn't initialize implicit types [message #17112 is a reply to message #17108] |
Tue, 29 July 2008 11:24 |
|
mirek
Messages: 13980 Registered: November 2005
|
Ultimate Member |
|
|
cbpporter wrote on Tue, 29 July 2008 05:10 | Yes, using that At works if you want to initialize it there.
I was talking about the way user defined classes get their default constructor called, while implicit types get left initialized.
It should be more uniform.
And since user types do get their constructor called, I don't think there is any performance difference between what I'm proposing and the current situation.
|
Actually, this aspect of C++ is quite confusing. For any user type, creating a variable of that type without further params calls default constructor.
Not so for fundamentals. But you can "call default constructor" for them as well - which assigns zero to them.
Anyway, I think there is some small performance impact for At - assigning zero is not free. Usually, this would be negligible, but I can imagine several usages where it could have significant impact.
Mirek
|
|
|
|
|
|
Goto Forum:
Current Time: Thu May 16 19:43:04 CEST 2024
Total time taken to generate the page: 0.02437 seconds
|