• 0

Initialize Array of Boolean Values


Question

primitive bool variables have "false" by default.

Is there a better way of initializing an array of boolean value to "true"?

Normally this is the way :

for(int i = 0; i < n; i++) array = true;

Is there a way say, during declaration

bool array[10000] = {false};

and have all 10000 initialized to false without using the for loop?

Link to comment
https://www.neowin.net/forum/topic/278351-initialize-array-of-boolean-values/
Share on other sites

12 answers to this question

Recommended Posts

  • 0

Although you can't initialize them all to true, you can treat them all as true. Just do everything opposite in boolean statements, if that couple of seconds of runtime is that important to you. Btw, what in the world would you need a size 10000 boolean array for anyway?

  • 0
bool array[10000];

memset(&array, 0, 10000 * sizeof(bool));

585385868[/snapback]

Actually that should be memset(&array,1,10000*sizeof(bool)) if he wants true. Interesting way of doing it though, I didn't think of doing it that way, although it might not be anymore efficient than a loop anyways.

  • 0
Although you can't initialize them all to true, you can treat them all as true.  Just do everything opposite in boolean statements, if that couple of seconds of runtime is that important to you.  Btw, what in the world would you need a size 10000 boolean array for anyway?

585385419[/snapback]

its theoretical i was just wondering. :)

i prefer to not do opposites for readability's sake.

  • 0
Doesn't bool array[10000] = {true}; work too?

Doesn't work because it sets the first element to true and the rest gets initialized to 0, thus false.

You could try a

bool array[10000];
std::memset(array,static_cast&lt;unsigned int&gt;(-1), sizeof(array));

Or consider using a std::vector<bool> or std::bitset.

  • 0
if i understand correctly, the only way of initializing arrays on the stack is to use a loop. memset for heap.

585388902[/snapback]

No. memset doesn't care how you allocate memory. You can call memset on any value that has a memory address (e.g. ints, bools, bool arrays, int arrays, class objects, array of class objects, etc). That's also why memset is dangerous.

Or consider using a std::vector<bool> or std::bitset.

++

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • There is a lot of reasons not to use Edge but faster fixes and security updates is not one of them.
    • Can't reproduce. I installed Edge, went to neowin.net > accepted the cookie consent > used menu to go to forums, everything loads and I can browse around the forums. If you can't interact with the dialog on the forums for some reason, go to the main site and accept the cookie consent there? It is true that the site will not function properly until the cookie consent is accepted or rejected,. it's a legal requirement and I also know that certain VPN/ad blockers block it, which is a user related issue and not a neowin.net problem.   This is not our cookie consent dialog. Gotta love browser hijacking... /s Edit: this may be what Californians see, I will confirm with our consent provider.
    • Google Chrome 149.0.7827.115 (offline installer) by Razvan Serea The web browser is arguably the most important piece of software on your computer. You spend much of your time online inside a browser: when you search, chat, email, shop, bank, read the news, and watch videos online, you often do all this using a browser. Google Chrome is a browser that combines a minimal design with sophisticated technology to make the web faster, safer, and easier. Use one box for everything--type in the address bar and get suggestions for both search and Web pages. Thumbnails of your top sites let you access your favorite pages instantly with lightning speed from any new tab. Desktop shortcuts allow you to launch your favorite Web apps straight from your desktop. Chrome has many useful features built in, including automatic full-page translation and access to thousands of apps, extensions, and themes from the Chrome Web Store. Google Chrome is one of the best solutions for Internet browsing giving you high level of security, speed and great features. Important to know! The offline installer links do not include the automatic update feature. Download web installer: Google Chrome Web 32-bit | Google Chrome 64-bit | Freeware Download: Google Chrome Offline Installer 64-bit | Direct Link | 131.0 MB Download: Google Chrome Offline Installer 32-bit | Direct Link | 119.0 MB Download page: Google Chrome Portable Download: Chrome ARM64 | Direct Link View: Chrome Website | Release Notes Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Oh, it's happening on more than one dialog? This is the dialog that I'm unable to interact with.
    • WSCC - Windows System Control Center 10.0.3.8 by Razvan Serea Windows System Control Center is a free, portable program that allows you to install, update, execute and organize the utilities from various system utility suites. WSCC can install and update the supported utilities automatically. Alternatively, WSCC can use the http protocol to download and run the programs. WSCC is portable, installation is not required. Extract the content of the downloaded zip archive to any directory on your computer. Free for personal use. The setup packages and updates are downloaded directly from their author's website! This edition of WSCC supports the following utility suites: Windows Sysinternals Suite (including support for "Sysinternals Live" service) NirSoft Utilities Mitec and more... WSCC - Windows System Control Center 10.0.3.8 changelog: [NEW] update progress is now visible on the Windows taskbar [FIXED] fixed an issue with the Update dialog [FIXED] minor fixes Download: WSCC (64-bit) | 5.4 MB (Free for personal use) Download: WSCC (32-bit) | 6.3 MB View: WSCC Homepage | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • One Month Later
      Clizby earned a badge
      One Month Later
    • One Month Later
      Timaximus earned a badge
      One Month Later
    • Week One Done
      Timaximus earned a badge
      Week One Done
    • Rookie
      FBSPL went up a rank
      Rookie
    • First Post
      davidbazooked earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      492
    2. 2
      PsYcHoKiLLa
      170
    3. 3
      +Edouard
      164
    4. 4
      Steven P.
      85
    5. 5
      ATLien_0
      77
  • Tell a friend

    Love Neowin? Tell a friend!