Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 Go to next message
cbpporter is currently offline  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 #17105 is a reply to message #17104] Tue, 29 July 2008 10:52 Go to previous messageGo to next message
mr_ped is currently offline  mr_ped
Messages: 825
Registered: November 2005
Location: Czech Republic - Praha
Experienced Contributor
At( index, init_value );

edit:
BTW, I like current behavior.
The uninitialized memory bugs don't happen to me anymore (unit testing + valgrind), and for performance reasons the current behavior of At is better for me.

[Updated on: Tue, 29 July 2008 10:57]

Report message to a moderator

Re: Using Vector::At doesn't initialize implicit types [message #17108 is a reply to message #17105] Tue, 29 July 2008 11:10 Go to previous messageGo to next message
cbpporter is currently offline  cbpporter
Messages: 1401
Registered: September 2007
Ultimate Contributor
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.
Re: Using Vector::At doesn't initialize implicit types [message #17109 is a reply to message #17108] Tue, 29 July 2008 11:13 Go to previous messageGo to next message
mr_ped is currently offline  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 #17110 is a reply to message #17109] Tue, 29 July 2008 11:20 Go to previous messageGo to next message
cbpporter is currently offline  cbpporter
Messages: 1401
Registered: September 2007
Ultimate Contributor
mr_ped wrote on Tue, 29 July 2008 12:13


No, it does fix your problem, try it.


I know that it fixes my problem!

I wasn't arguing about that problem, I was saying that the lack of uniformity between user and implicit types is a bad thing. We should have uniform semantics so we don't get unexpected security flaws.

[Updated on: Tue, 29 July 2008 11:21]

Report message to a moderator

Re: Using Vector::At doesn't initialize implicit types [message #17111 is a reply to message #17104] Tue, 29 July 2008 11:22 Go to previous messageGo to next message
mr_ped is currently offline  mr_ped
Messages: 825
Registered: November 2005
Location: Czech Republic - Praha
Experienced Contributor
Ok, I see, but I vote not for initialization, but for not calling constructor on user types to make it consistent. Smile
Re: Using Vector::At doesn't initialize implicit types [message #17112 is a reply to message #17108] Tue, 29 July 2008 11:24 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
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
Re: Using Vector::At doesn't initialize implicit types [message #17113 is a reply to message #17112] Tue, 29 July 2008 16:03 Go to previous messageGo to next message
cbpporter is currently offline  cbpporter
Messages: 1401
Registered: September 2007
Ultimate Contributor
Well I guess we can leave it as is for performance reasons, but then we should add an entry to "U++ traps and pitfalls". This behavior should be documented somewhere, made very clear especially for pointers. Using pointers in Vector is not that common, so when you start using them it can be a nasty surprise that the values are not null checkable.
Re: Using Vector::At doesn't initialize implicit types [message #17124 is a reply to message #17113] Tue, 29 July 2008 18:39 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
cbpporter wrote on Tue, 29 July 2008 10:03

Well I guess we can leave it as is for performance reasons, but then we should add an entry to "U++ traps and pitfalls". This behavior should be documented somewhere, made very clear especially for pointers. Using pointers in Vector is not that common, so when you start using them it can be a nasty surprise that the values are not null checkable.


I do not know. If you do not initialize variable, you can consider it unitialized. If you do not initialize struct members, ditto.

What is surprising on At behaviour? Especially if it has another "init" method variant.

But OK, you are perhaps right.

Mirek
Re: Using Vector::At doesn't initialize implicit types [message #17127 is a reply to message #17124] Tue, 29 July 2008 21:55 Go to previous message
cbpporter is currently offline  cbpporter
Messages: 1401
Registered: September 2007
Ultimate Contributor
luzr wrote on Tue, 29 July 2008 19:39


I do not know. If you do not initialize variable, you can consider it unitialized. If you do not initialize struct members, ditto.


I wouldn't say ditto, because if I do not initialize structs, they do get initialized for me automatically via default constructor, so theoretically they are initialized. If the constructor does not initialize properly that is a different problem Smile.
Previous Topic: zlib
Next Topic: Interesting struggle with "Moveable<T>" usage in GCC
Goto Forum:
  


Current Time: Mon Apr 29 18:05:15 CEST 2024

Total time taken to generate the page: 0.03132 seconds