-2

I have this very large array, called grid. When I declare the array as below, every value in the array should be set to 0 according to the array constructor for integers

int testGrid[226][118];

However when I iterate through the entire array, I seem to get 0s for the majority of the array, however towards the lower part of the array I get arbitrary trash. The solution it is seems is to iterate over the array and manually set each value to 0. Is there a better way to do this?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
user3328187
  • 193
  • 2
  • 2
  • 6
  • Please make the title and body of the post compatible. You are asking two different questions. – juanchopanza Nov 30 '15 at 09:36
  • How so? The question is why my array is not initializing all values to 0 – user3328187 Nov 30 '15 at 09:38
  • 1
    Really? So what is "Is there a better way to do this?" – juanchopanza Nov 30 '15 at 09:39
  • 3
    There's no such thing as an "array constructor for integers". – Daniel Daranas Nov 30 '15 at 09:41
  • When you do this, this array only point to an memory, but the value, at that time, in the memory won't be changed. That's why you got those trash value. – Roger Dwan Nov 30 '15 at 09:41
  • By defaults, variables and memory allocation in C/C++ variables are not zero-initialized by default. (Exception: global and static variables). – selbie Nov 30 '15 at 09:41
  • 3
    @RogerDwan "When you do this, this array only point to an memory". Arrays aren't pointers. People on this site need to stop telling beginners arrays are pointers. – Simple Nov 30 '15 at 09:42
  • `memset(testGrid, '\0', sizeof(testGrid));` – selbie Nov 30 '15 at 09:43
  • @Simple a pity they removed the feature where you could downvote comments – M.M Nov 30 '15 at 09:44
  • @selbie: *"variables are not zero-initialized by default"* that depends on context: if they're static or namespace scoped they are, and if there're members of a class that's initialised a la `new X();` they effectively are too due to the initiial zero-initialisation of `X`'s memory. – Tony Delroy Nov 30 '15 at 09:45
  • @M.M.: and there are situations in which value initialisation ends up doing zero initialisation (see 8.5/8). Separately, thread local variables are another exception to selbie's comment). – Tony Delroy Nov 30 '15 at 10:00
  • @M.M.: sure... so you're saying "zero-initialisation of X's memory" sounded less like "zero initialisatoin" and more like "memory zeroing"? Ok - good to have it clarified. – Tony Delroy Nov 30 '15 at 10:20
  • @TonyD In the [C++ standard](http://stackoverflow.com/questions/17801075/zero-initialization-and-static-initialization-of-local-scope-static-variable), the term "zero-initialization" means the all-bits-zeroing of static objects - this happens early on in program startup, before any constructors are run. Confusingly, we casually use the term to describe value-initialization. (as I did in my previous comment...) I thought at first you were disambiguating by adding "of X's memory", but I see now that you meant value-initialization in both cases in your original comment – M.M Nov 30 '15 at 10:36
  • @M.M. there's nothing casual about this - zero initialisation is explicitly mentioned in the Standard as behaviour after startup when "there are fewer initializers than there are array elements" 8.5.2/3, and as an conditional step in value initialisation 8.5/8; I did not mean "value initialisation" in my original comment - I meant to refer only to those times when value initialisation involves a zero initialisation step. Anyway, this isn't helping the OP.... – Tony Delroy Nov 30 '15 at 10:48

1 Answers1

5

You could do:

int testGrid[226][118] = {};

which will initialize your entries to 0.


Please see this C answer, which may come in handy for C++ too.

By the way, since this is C++, consider using an std::array, or an std::vector.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • But wouldn't that only initialize only the first value of the array? – user3328187 Nov 30 '15 at 09:36
  • 3
    The first value will bet set to `0`, the rest will be set to `0`. If you'd do `.. = {1}` the first element would be set to `1`, the rest would be set to `0`. – Leśny Rumcajs Nov 30 '15 at 09:38
  • Thank you very much @juanchopanza, I updated my answer, you were right! – gsamaras Nov 30 '15 at 09:42
  • This is what I was thinking @juanchopanza, I will update. – gsamaras Nov 30 '15 at 09:45
  • It's not a better version. The version with `{}` cannot be used in C++ prior to C++11. – Leśny Rumcajs Nov 30 '15 at 09:49
  • 1
    Are you sure @LeśnyRumcajs? I just compiled it with `g++ -Wall main.cpp` and it was fine. – gsamaras Nov 30 '15 at 09:50
  • @M.M No it had not. Please verify your theorem as I did. Though probably I was wrong about C++03 - `{}` does not work under C++98. – Leśny Rumcajs Nov 30 '15 at 09:56
  • 1
    @LeśnyRumcajs I checked. `= {}` actually does initialize an array of ints to all `0` in C++98. (See 8.5.1/8 "An empty initializer-list can be used to initialize any aggregate", 8.5.1/7 "If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitly initialized shall be default-initialized", and 8.5/5 (too big to quote fully in comment) says that *default-initialized* for non-class types means zero-initialization. (Note: This terminology changed in C++03, but not the effect) – M.M Nov 30 '15 at 10:07
  • @M.M Well then, my bad. Though on some compilers such initialization does not work (e.g. Borland C++ Builder 5). Error `expression expected`. – Leśny Rumcajs Nov 30 '15 at 10:13
  • 1
    bcc32 is famous for terrible standards compliance. Luckily the 2015 release of the product finally ditches the old bcc32 and uses clang front end. – M.M Nov 30 '15 at 10:17