2

I'm learning C and using CodeBlocks on MacOSX to write and build my test scripts. The instructor says that you cannot increase the elements of an array once set using varname[limitInt].

However, I have the following:

#include <stdio.h>
int main() {
    int num[4] = {1,2,3,4,5};
    int n = 5;
    num[n] = 6;
    num[6] = 7;
    for(n=0;n<10;n++) {
        printf("%d\n",num[n]);
    }
}

Building shows warnings about excess elements in array initializer, and also array index 6 is past the end of the array. But I'm not stopped... The build works and I can run with the result of:

1
2
3
4
-353028040
6
7
-1687410582
-353028040
32766

Though it does throw [1] with an abort, it runs.

Here is what I'm thinking:

  1. The compiler I'm using doesn't gate this, but it should.
  2. My instructor shouldn't be using language like "can't ever extend an array", as that's clearly possible from my tests (of course only if we ignore the abort and return of 1 to stdout).

For 1: Is there a way to hard fail on building with these types of warnings? Maybe use a different compiler?

For 2: If C is allowing it to extend, but returning 1, can someone describe to me why?

NorseGaud
  • 355
  • 1
  • 11
  • 2
    C doesn't have any bounds-checking. Accessing an array out of bounds leads to [*undefined behavior*](https://en.wikipedia.org/wiki/Undefined_behavior). And arrays have a fixed size and can't be extended, just what your teacher says. – Some programmer dude Dec 30 '18 at 15:11
  • In your example, num[6] is beyond the end of the space you allocated for the array. If your code works, you got lucky and it might not work next time. C allows you to ***index*** beyond the size of your array but you are accessing memory that it not allocated to the array. – nicomp Dec 30 '18 at 15:12
  • 1
    It is not extending the array. You are overwriting the next piece of memory after the array, which might crash, might "work", might delete your bank account info. – stark Dec 30 '18 at 15:12
  • before to delete your bank account info please transfer the money on mine lol. – bruno Dec 30 '18 at 15:16
  • 1
    The undefined behaviour means that the standard lets the compiler do whatever it pleases. It *can* implement bounds-checking for arrays if it pleases. It can also *not* implement it. And lastly it can *sometimes* check the array bounds and ignore them in other cases. – Antti Haapala -- Слава Україні Dec 30 '18 at 15:19
  • 1
    This is a classic example of memory leak. Some good compilers may reject it or at least give warning messages. Good code practice is to always check arrays borders. – Gaston Dec 30 '18 at 16:45
  • Ah, yes, that makes sense, Gaston. Thanks for the help everyone! – NorseGaud Dec 30 '18 at 18:14
  • 1
    @Gaston This isn't a [memory leak](https://en.wikipedia.org/wiki/Memory_leak), it's a [buffer overflow](https://en.wikipedia.org/wiki/Buffer_overflow). – Blastfurnace Dec 30 '18 at 21:58
  • @Blastfurnace right, thank you – Gaston Dec 31 '18 at 08:49

0 Answers0