util::array

Discussion of all aspects of the game engine, including development of new and existing features.

Moderator: Forum Moderators

Post Reply
Dave
Founding Developer
Posts: 7071
Joined: August 17th, 2003, 5:07 am
Location: Seattle
Contact:

util::array

Post by Dave »

After a discussion with Ayin, we've decided to add a directive to the coding standards (http://wesnoth.slack.it/?CodingStandards) not to use C-style arrays in Wesnoth.

A wrapper to C-style arrays has been added in array.hpp, util::array. It should be used instead of C-style arrays at all times.

E.g.

Code: Select all

util::array<int,10> myints;
instead of,

Code: Select all

int myints[10];
David
“At Gambling, the deadly sin is to mistake bad play for bad luck.” -- Ian Fleming
mlangsdorf
Posts: 129
Joined: April 27th, 2004, 4:24 pm

Post by mlangsdorf »

What's the rational behind this? I'm not questioning the decision, I just don't have any idea why it was made.
Dave
Founding Developer
Posts: 7071
Joined: August 17th, 2003, 5:07 am
Location: Seattle
Contact:

Post by Dave »

mlangsdorf wrote:What's the rational behind this? I'm not questioning the decision, I just don't have any idea why it was made.
Because C-style arrays are ugly to use, and the interface is inconsistent with C++-style container classes.

E.g. to iterate over an array of integers:

Code: Select all

for(int* i = myarray; i != myarray + sizeof(myarray)/sizeof(*myarray); ++i) {
    ...
}
Which is very different from the vector way:

Code: Select all

for(vector<int>::iterator i = myarray.begin(); i != myarray.end(); ++i) {
    ...
}
The new way to do it with an array is consistent with this:

Code: Select all

for(array<int,n>::iterator i = myarray.begin(); i != myarray.end(); ++i) {
    ...
}
(Note that if anyone wants to say that either way is hideously ugly, I won't disagree with them.)

David
“At Gambling, the deadly sin is to mistake bad play for bad luck.” -- Ian Fleming
torangan
Retired Developer
Posts: 1365
Joined: March 27th, 2004, 12:25 am
Location: Germany

Post by torangan »

What's the reasoning behind adding an own wrapper? It's not as if std::vector<> would be more then a quite sophisticated wrapper. I doubt that you can implement any of the wrapper functions more efficient then std::vector<> because as part of the c++ library, it'll most likely use compiler specific optimizations normal user won't ever know of.
Dave
Founding Developer
Posts: 7071
Joined: August 17th, 2003, 5:07 am
Location: Seattle
Contact:

Post by Dave »

torangan wrote:What's the reasoning behind adding an own wrapper? It's not as if std::vector<> would be more then a quite sophisticated wrapper. I doubt that you can implement any of the wrapper functions more efficient then std::vector<> because as part of the c++ library, it'll most likely use compiler specific optimizations normal user won't ever know of.
std::vector is less efficient because it allocates memory dynamically (on the free store), while a C-style array allocates it on the stack, which is extremely fast.

The util::array I added is a very very thin wrapper over a C-style array. It's similiar to the wrapper in Boost. I just wrote my own because it was about as simple to write it myself as it was to extract the code from Boost while getting rid of the Boost header dependencies and weird code to handle strange compilers.

David
“At Gambling, the deadly sin is to mistake bad play for bad luck.” -- Ian Fleming
torangan
Retired Developer
Posts: 1365
Joined: March 27th, 2004, 12:25 am
Location: Germany

Post by torangan »

Well sure, I just didn't think about small, stack allocated arrays. You should have written that it's intended to be used for stack allocated arrays and std::vector for heap allocated arrays. Or I should have thought a moment longer, either would've done it. :D
Dave
Founding Developer
Posts: 7071
Joined: August 17th, 2003, 5:07 am
Location: Seattle
Contact:

Post by Dave »

torangan wrote:Well sure, I just didn't think about small, stack allocated arrays. You should have written that it's intended to be used for stack allocated arrays and std::vector for heap allocated arrays. Or I should have thought a moment longer, either would've done it. :D
Huh? My entire first post in this thread said exactly that.

David
“At Gambling, the deadly sin is to mistake bad play for bad luck.” -- Ian Fleming
torangan
Retired Developer
Posts: 1365
Joined: March 27th, 2004, 12:25 am
Location: Germany

Post by torangan »

Yes, if read correctly it does say it. I got set of by "at all times" and got the picture of int[] foo = new int[...] into my mind which should be replaced by vector instead of another wrapper IMHO.
Dave
Founding Developer
Posts: 7071
Joined: August 17th, 2003, 5:07 am
Location: Seattle
Contact:

Post by Dave »

torangan wrote:got the picture of int[] foo = new int[...] into my mind
Sounds like you've been programming too much Java or C# ;)

David
“At Gambling, the deadly sin is to mistake bad play for bad luck.” -- Ian Fleming
torangan
Retired Developer
Posts: 1365
Joined: March 27th, 2004, 12:25 am
Location: Germany

Post by torangan »

Well it's legal syntax in C++ as well, but yes, I've been forced to program in Java for more then a year now. I do like C++ way more but the decision is made by those who pay the money I want. When I'm coding open source, it's always C++ though.
Dave
Founding Developer
Posts: 7071
Joined: August 17th, 2003, 5:07 am
Location: Seattle
Contact:

Post by Dave »

torangan wrote:Well it's legal syntax in C++ as well, but yes, I've been forced to program in Java for more then a year now. I do like C++ way more but the decision is made by those who pay the money I want. When I'm coding open source, it's always C++ though.
It's not legal syntax in C++. In C++ you go,

int* foo = new int[...];

or you can go,

int foo[] = {...};

but never int[] foo

David
“At Gambling, the deadly sin is to mistake bad play for bad luck.” -- Ian Fleming
Post Reply