Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's undefined behavior as far as I know. Run a larger program with that and it will crash somewhere along the way. Bounds checking is not a part of raw arrays (or even std::vector).</p> <p>Use std::vector with <code>std::vector::iterator</code>'s instead so you don't have to worry about it.</p> <p>Edit: </p> <p>Just for fun, run this and see how long until you crash:</p> <pre><code>int main() { int array[1]; for (int i = 0; i != 100000; i++) { array[i] = i; } return 0; //will be lucky to ever reach this } </code></pre> <p>Edit2: </p> <p>Don't run that.</p> <p>Edit3:</p> <p>OK, here is a quick lesson on arrays and their relationships with pointers:</p> <p>When you use array indexing, you are really using a pointer in disguise (called a "reference"), that is automatically dereferenced. This is why instead of *(array[1]), array[1] automatically returns the value at that value.</p> <p>When you have a pointer to an array, like this:</p> <pre><code>int array[5]; int *ptr = array; </code></pre> <p>Then the "array" in the second declaration is really decaying to a pointer to the first array. This is equivalent behavior to this:</p> <pre><code>int *ptr = &amp;array[0]; </code></pre> <p>When you try to access beyond what you allocated, you are really just using a pointer to other memory (which C++ won't complain about). Taking my example program above, that is equivalent to this:</p> <pre><code>int main() { int array[1]; int *ptr = array; for (int i = 0; i != 100000; i++, ptr++) { *ptr++ = i; } return 0; //will be lucky to ever reach this } </code></pre> <p>The compiler won't complain because in programming, you often have to communicate with other programs, especially the operating system. This is done with pointers quite a bit. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload