• 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
  Andareed said:
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
  Citrusmoose said:
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
  Quote
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
  juan said:
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.

  Quote
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.
  • Popular Now

  • Posts

    • how am i gonna teach my grandma understand a new OS now ?
    • Well, Satya Nadella did say 30% of Microsoft's code is written by AI. Considering that code is also reviewed and tested by the same AI: garbage in, Microsoft code out.
    • Spotify 1.2.66.445 by Razvan Serea Spotify for Windows is your ultimate music and podcast streaming app, designed for a seamless listening experience. Enjoy millions of songs, curated playlists, and personalized recommendations tailored to your taste. Spotify delivers high-quality audio streaming with bitrates up to 320 kbps for Premium users and 160 kbps on the Free tier. Music is streamed in Ogg Vorbis format on the app and AAC on web browsers, ensuring efficient delivery and compatibility. Adaptive bitrate technology adjusts quality based on network conditions, while Premium users enjoy offline downloads for uninterrupted playback. Podcasts are streamed at slightly lower bitrates, typically 96-128 kbps, to balance quality and data usage. Spotify works across smartphones, desktops, smart speakers, game consoles, and more, with Spotify Connect enabling smooth cross-device playback. Features like real-time lyrics, curated playlists, and personalized EQ settings elevate the listening experience, making Spotify a versatile platform for music and podcasts alike. Spotify Free features: Stream Millions of Songs - Access a vast library of music across various genres and artists. Curated Playlists - Discover ready-made playlists for every mood and moment. Shuffle Play Mode - Enjoy songs in random order with shuffle-enabled playback. Personalized Recommendations - Get music suggestions based on your listening habits. Access to Podcasts - Explore a wide range of podcasts on topics you love. Ad-Supported Listening - Stream music with occasional audio and display ads. No Subscription Required - Enjoy music without any payment commitment. Cross-Device Sync - Listen seamlessly on mobile, desktop, or web browser. Basic Playback Controls - Play, pause, and skip tracks within shuffle limitations. Discover Weekly & Release Radar - Stay updated with custom playlists for new music. Download: Spotify 1.2.66.445 | ARM64 | ~100.0 MB (Free, paid upgrade available) View: Spotify Website | Web Player Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Understandable, they need to make money somehow to keep the service "free". If you're not paying with money, you'll pay with something else. In this case, it's ads. People will bitch about this (as usual), but it's basic economics, everything has a price, like YouTube (non-premium).
    • A sign of a man with absolutely no taste. Also the first American president to whine about American "jobs" while selling Chinese stuff for 5x or 10x the actual worth while slapping his stupid name on it. Like, what even is this, some really bad, humorless parody? Nope, it's the actual reality. Selling Chinese bible, selling EV's on White House front yard, selling Chinese phones that are wrapped in few microns of gold for 3x the value... It's so hilarious it's cringy af.
  • Recent Achievements

    • Week One Done
      patrickft456 earned a badge
      Week One Done
    • One Month Later
      patrickft456 earned a badge
      One Month Later
    • One Month Later
      Jdoe25 earned a badge
      One Month Later
    • Explorer
      Legend20 went up a rank
      Explorer
    • One Month Later
      jezzzy earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      637
    2. 2
      ATLien_0
      278
    3. 3
      +FloatingFatMan
      171
    4. 4
      Michael Scrip
      156
    5. 5
      Steven P.
      128
  • Tell a friend

    Love Neowin? Tell a friend!