Anonymous asked in Computers & InternetProgramming & Design · 2 months ago

If you attempt to store data past an array's boundaries, it is guaranteed that the compiler will issue an error. True or False? Why?

I know the answer is false, but I do not completely understand why it is false.

3 Answers

  • 2 months ago

    It's false, alright, and for at least two reasons:

    First, the compiler can't reasonably know whether an expression for an index is going to be out of bounds or not.  It's hard enough for a person to check for errors like that--even the person who wrote the code in the first place.  In a decent language, this is an error that would be caught and reported at run time rather than by the compiler.

    Second, the C family of languages (C, C++, Objective C, maybe more) and some ancient (but still used) languages (Fortran immediately comes to mind) do not even perform such tests at run time.  If the compiler is for one of those languages, the only indication of the error might be a badly-behaved program.  If you're lucky, you'll get a segment fault right away; making the type of problem (if not the precise location of the error) easy to determine.

  • EddieJ
    Lv 7
    2 months ago

    Is it guaranteed that the restaurant will have burgers on its menu?

    "the compiler" is as vague as "the restaurant".  

    However, in the context of your class, the question *MIGHT* be talking about a specific compiler.

    Any particular compiler may or may not make the guarantee.  Apparently, you know that it's false, which implies you know which compiler the question is addressing.

    A compiler is a computer program and the way it works depends on the person or people who designed it.  They can decide to do it either way.

    Checking for boundaries takes CPU cycles and they may have decided that it's not worth it.  The compiler might automatically allocate a bit more space than the array needs so that minor violations will not overwrite storage for another variable.

  • 2 months ago

    I would have said true. False makes no sense. Why would you have boundaries in the first place,  if it's not a problem going outside of them?

Still have questions? Get answers by asking now.