• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
MatejaS

C++ std::vector vs raw array for short arrays

7 posts in this topic

Hello everyone,

I need an array of 2 booleans inside class.

bool array[2];

I don't have C++11 and I cannot use initializer lists so I have to initalize array like this:

Class::Class()
{
  array[0]=array[1]=false;
}

If array was std::vector<bool>,I could do:

Class::Class() :
  array(2,false)
{}

But it's length is just 2,and I don't need any of the std::vector stuff (such as iterators ... )

Which one is a better approach?

 

Thanks,

MatejaS

 

0

Share this post


Link to post
Share on other sites
A std::vector is a kind of dynamic array. One of the key aspects is that the size can be changed.

Even with a standard array you have iterators. The other features you mentioned are mostly just syntactic candy. If you need to initialize each one then you do it. Passing it to the constructor of a vector just means that the constructor is running that loop instead of you.

If you have no need to resize the container then a simple fixed array works just fine.

If you do need to resize the container use the vector class or a container that works better for your usage patterns. The vector class will do all the work necessary to resize it, including the process of allocating new memory, moving all the objects over to the new block of memory, and releasing the old block of memory.
2

Share this post


Link to post
Share on other sites

If the size of the array will not be changed at runtime and you don't need any of the other functionality that comes along with a vector then there is no good reason to use one. If at some later time you wish to change your array to a vector you should be able to do so with minimal impact to your code, as it overloads the subscript operator.

0

Share this post


Link to post
Share on other sites
std::fill() would be a standard and clear way to specify intent when initializing an array:
Class::Class()
{
  std::fill(array, array + 2, false);
}
 
At the same time, I wonder if a two-element array is actually appropriate, rather than two separately named bools.  If you have multiple two-element arrays, it might be cleaner to wrap one element of each into a struct, and then just have a single two-element array of the struct.  Each instance of the struct could then have its own default constructor that would initialize the bool to false, and any other variables to their proper defaults also.
0

Share this post


Link to post
Share on other sites
Not sure if multithreading is an issue here, but be aware that for:
bool array[2];
the assignment
 array[0] = true;
is atomic.
 
std::vector<> on the other hand has a template specialization for bools, which merges them into integer values to safe space. Using std::vector<bool>  
std::vector<bool> vecArray;
//...
vecArray[0] = true;
becomes:
unsigned char tmp = vecArray[0];
tmp = tmp | (1 << 0);
vecArray[0] = tmp;
which is not atomic.

Edit: On the notion of merging the bools into integer types, have you considered C-style flags or, for a more array like experience, std::bitset? Edited by Ohforf sake
0

Share this post


Link to post
Share on other sites

Why exactly can't you brace-initialize this array? That isn't a C++11 only thing.

 

C++03 8.5.1/1 [dcl.init.aggr] states:

 

An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), no private or protected
non-static data members (clause 11), no base classes (clause 10), and no virtual functions (10.3).

All of that is certainly true for an array of bool with size 2.

 

Therefore, 8.5.1/2, which states that aggregates can be brace-initialized is applicable.

 

bool array[2] = {}; should compile just fine (and it does indeed compile fine using GCC with either --std=c++03 or --std=c++98).

 

 

Not exactly relevant, but also note that array[0]=array[1]=false; most probably doesn't initialize the array in the order you expect.

Edited by samoth
0

Share this post


Link to post
Share on other sites

Why exactly can't you brace-initialize this array? That isn't a C++11 only thing.

It is when your array is a member variable and you're trying to initialize it in a constructor.
2

Share this post


Link to post
Share on other sites

Why exactly can't you brace-initialize this array? That isn't a C++11 only thing.

 

...

 

Therefore, 8.5.1/2, which states that aggregates can be brace-initialized is applicable.

 

bool array[2] = {}; should compile just fine (and it does indeed compile fine using GCC with either --std=c++03 or --std=c++98).

 

 

Not exactly relevant, but also note that array[0]=array[1]=false; most probably doesn't initialize the array in the order you expect.

 

Were you declaring that as a member variable, or just a local variable?  From what I understand, non-static inline member initialization is also a C++11 thing.  Without that feature, the member variable needs to either be initialized in the constructor's initializer list, or within the constructor body, neither of which would support the syntax provided in your post.

 

Edit:  Ninja-ed by SiCrane!

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0