Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

c++ array elements could be added indefinitly?

I’m new to c++ and I’ve read that arrays are fixed sized. So I’m confused with the following.

int a[] = {0};
    a[1] = 1;
    a[2] = 2;
    a[3] = 3;
    //.....

The array could be populated like this many times in a loop. So how is it fixed sized?

Edit:
Understood that it’s undefined behaviour from the comments. It is fixed size but the compiler doesn’t throw errors (which is what confused me) even if the limit is exceeded and the program is compiled.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The array in you example is of fixed size (1 element), but it doesn’t perform bounds check when assigning new data via subscript, so what you do here is assigning values to memory your array doesn’t own, which is undefined behavior.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading