Click to See Complete Forum and Search --> : Initializing arrays


MrDoomMaster
February 22nd, 2005, 08:52 PM
Initializing a boolean array to false is easy:

bool bArray[40] = {0};

All elements will be false. But what if you want them all to be true?

bool bArray[40] = {1};

This does not work. It works for the first element ( I think) but not for all! How would you initialize an array to any value you want?

galapogos
February 23rd, 2005, 12:44 AM
You could do bArray[40] = {1, 1, 1, ..., 1}. Correct me if I'm wrong but when you declare any variable, most compilers on most OSes will clear it, so with your 1st statement, the {0} is redundant.

Kheun
February 23rd, 2005, 01:21 AM
Correct me if I'm wrong but when you declare any variable, most compilers on most OSes will clear it, so with your 1st statement, the {0} is redundant.No, declaration of any variable doesn't automatically initialize the value to 0.

bool bArray[40] = {1};
The above doesn't work. To initialize all the elements to 1, you may like to use a loop instead.

galapogos
February 23rd, 2005, 02:37 AM
Well, it's never smart to assume that it's zero, but I've programmed on various unix platforms, and gcc/cc almost always initializes variables to zero if otherwise uninitialized. I'm not sure what MSVC does. Now memory locations OTOH are never initialized, until you malloc, and even then it's probably not a good idea to assume a malloc'ed memory location as zero if you want any sort of portability.

Andreas Masur
February 23rd, 2005, 03:16 AM
This does not work. It works for the first element ( I think) but not for all! How would you initialize an array to any value you want?
There is no trivial way with old-style characters. If possible, simply use a 'bitset' instead...

#include <bitset>

std::bitset<40> bits;

// Set all bits to true
bits.set();

MrDoomMaster
February 23rd, 2005, 08:59 PM
There is no trivial way with old-style characters. If possible, simply use a 'bitset' instead...

#include <bitset>

std::bitset<40> bits;

// Set all bits to true
bits.set();


Using a bitset is a bit of an overload don't you think? What are the performance differences between using bitset and an old-fashioned good ol' array?

Think in terms of gaming, where speed is what you really optimize the code for.

wien
February 23rd, 2005, 09:57 PM
It wouldn't surprise me if bitset is faster. It is a highly optimized class. Never assume things when it comes to the performance of standard containers.

Paul McKenzie
February 23rd, 2005, 11:50 PM
Using a bitset is a bit of an overload don't you think? What are the performance differences between using bitset and an old-fashioned good ol' array?Try to do an xor or any boolean operation on more than 32-bits (or whatever the maximum size in bits of the long). The bitset actually does store the elements in individual bits, while your array uses an entire bool for each bit. Depending on the operation, the way a bitset stores its elements will more than likely beat the array implementation.

Assume the long has a bit-size of 32, and you have 100 bits. You want to do an xor of a bool[100] array with another bool[100] array. Your implementation will more than likely do one value at a time in a loop, setting each bit depending on the xor result. So that's 100 loop iterations.

On the other hand, the bitset will do a "chunk of bits" at a time, up to 32 bits in one loop iteration, making only 4 loop iterations max. Four xor's as opposed to 100 xor's with the array implementation.. This is just one example of where bitset would more than likely beat an array implementation.

But in general, you need to time this yourself with the operations that you are planning to use.

Regards,

Paul McKenzie

Andreas Masur
February 24th, 2005, 04:00 AM
Using a bitset is a bit of an overload don't you think? What are the performance differences between using bitset and an old-fashioned good ol' array?

Think in terms of gaming, where speed is what you really optimize the code for.
Take a look at the following FAQs:

How do I determine the speed of a particular function or operation? (http://www.codeguru.com/forum/showthread.php?s=&threadid=291294)
Which is faster <...> or [...]? / How can I optimize this? (http://www.codeguru.com/forum/showthread.php?t=316185)

However....remember the following:

The two rules of optimisation:

1. Don't optimise
2. Don't optimise - yet.

And don't do anything unless a profiler tells you that it's worth it. Ask ten programmers how they would optimise a piece of code and you'll get eleven answers - all wrong.

MrDoomMaster
February 24th, 2005, 03:01 PM
Thanks everyone for your input. Lots of links in this thread, I'm going to look at them all. I appreciate all of you guys.