answersLogoWhite

0


Best Answer

An array is simply a contiguous block of memory that is divided into one or more elements of equal size. The array name is itself a reference to the start address of the array, which has the same address as the first element in the array (the element with index 0). The index is essentially an offset from the start of the array, multiplied by the size of an element. However, there is no built-in mechanism in C to prevent you from accessing elements beyond the upper bound of the array at runtime -- essentially overflowing the array. Since C++ inherits from C, the same problem exists in C++.

For instance, in a 10 element array, the upper bound is 9. If you attempt to write to element 10, you are overflowing the array, the buffer, because that memory does not belong to the array. You then introduce undefined behaviour. At best, nothing bad will happen. At worst, people could die. Once you introduce undefined behaviour there's simply no telling what could happen -- it's a time-bomb waiting to go off.

The only way to avoid such problems is to ensure all your array offsets remain within the bounds of the array. That is, the onus is upon the C++ programmer -- just as it still is with the C programmer.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why upperbound of array in c plus plus overflow during runtime?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you start array list from 1?

Array indices are zero-based because the first element is at offset zero from the start of the array. There is no such thing as a one-based array in C. Some other languages do allow you to specify the base of an array, but converting a non-zero-based index to a zero-based index adds a runtime overhead to the implementation.


What is the difference between a fixed size array and a variable size array?

The obvious answer is that one has a constant size while the other does not. More specifically, a fixed-size array is one where the size is known at compile time and does not change at runtime. By contrast, the size of a variable-sized array may or may not be known at compile time but may change at runtime. We often refer to a variable-size array as being a dynamic array, however some people (myself included) incorrectly refer to a fixed-size array as being a static array. The misunderstanding largely comes from the fact that we often refer to the heap (or free store) as being dynamic memory because all dynamic variables are allocated there (including variable-size arrays). But the term dynamic array does not refer to the memory, it refers to the dynamic -- as in changeable -- nature of the array itself. By contrast, a fixed-size array is only deemed static if it is statically allocated, in which case it will be allocated in the program's data segment along with all other static variables, global variables and constants. But a local fixed-size array is allocated on the program's stack and is therefore, by definition, non-static. Moreover, you can allocate a fixed-size array on the heap!


Why should a function that accepts an array as an argument and processes that array also accept an argument specifying the array?

Basically in c++ passing an array as an argument only provides a pointer to the first value and that function won't know how many values it has.If you read beyond the size you will just get garbage from memory.


What does buffer overflow mean?

A buffer overflow occurs when a program tries to store some amount of data in a location which cannot contain that amount of data. For example, trying to store an array of 10 integers in an array with room for 5 integers will cause a buffer overflow. Buffer overflows are common in C/C++, specifically in the strcpy function. This function copies the data from one string (char array) into another. It is often abused by clever hackers to either crash a program or to inject their own instructions onto the execution stack. The "extra" data copied over from the larger array doesn't magically disappear. The "overflow" refers to that data overwriting data on the stack. While this will often just crash the program (or computer), those clever hackers mentioned above may happen to know enough about the program structure to be able to insert their own low-level computer instructions to execute arbitrary code on the machine.


How an array is created in c?

For any type T, the type T[n] is an array of n Ts. If n is known at compile time, the array is fixed-length and the compiler will allocate n * sizeof(T) bytes to the array name. If n is not known at compile time, the array is variable-length and the programmer must manually request n * sizeof(T) bytes from the system at runtime, storing the start address in a pointer. The programmer must keep track of the stored address at all times and must release the memory as soon as it is no longer required.

Related questions

How can I fix Stack overflow at line 156?

A stack overflow is usually the cause of an array that is too small to be able to hold the intended data. To fix a stack overflow, the array must be locally declared (this means not dynamically allocated off of the heap) and then you must change the amount of "slots" in the array to something that is big enough to hold your data.


What is Stack overflow at line?

A stack overflow is a type of buffer overflow in which an array writes memory outside of the array boundaries. The keyword here is "stack". The stack is a section in memory in which local variables and other program data are kept for future reference. When the stack gets overflown, adjacent program memory, such as variables, pointers, etc, will be overwritten and cause your program to crash.


What is the meaning of a stack overflow?

A stack overflow is a programming term used to identify when a function tries to access memory from a stack that does not exist. A stack, such as a queue or array, contains a limited number of memory spaces set aside when it is created. For example, if an array has 8 objects in it and a function tried to access an item at slot nine, which doesn't exist, it would cause a stack overflow.


Where can one shop for RAID array recovery software?

RAID array recovery software is best purchased directly from the companies that create each program, to avoid a bad download. Some places to try include Runtime Software and Disk Internals.


How do you start array list from 1?

Array indices are zero-based because the first element is at offset zero from the start of the array. There is no such thing as a one-based array in C. Some other languages do allow you to specify the base of an array, but converting a non-zero-based index to a zero-based index adds a runtime overhead to the implementation.


Array in c plus plus must be defined at compil time?

No. Arrays can be defined at runtime, just as they can in C. It's just that it's generally more convenient to use vectors instead of dynamic arrays at runtime, thus arrays are generally used statically, at compile time.


What is an user defined array?

The term user-defined really means programmer-defined when referring to programming. An user-defined array is therefore an array that the programmer has declared, rather than one that is provided by a third party or is built-in to the language. In essence, any array you yourself declare is an user-defined array. The following are examples of user-defined arrays: int x[10]; // static array of 10 integer elements. Allocated at compile time. int* y = new int [5]; // dynamic array of 5 integer elements. Allocated at runtime.


What is the difference between a fixed size array and a variable size array?

The obvious answer is that one has a constant size while the other does not. More specifically, a fixed-size array is one where the size is known at compile time and does not change at runtime. By contrast, the size of a variable-sized array may or may not be known at compile time but may change at runtime. We often refer to a variable-size array as being a dynamic array, however some people (myself included) incorrectly refer to a fixed-size array as being a static array. The misunderstanding largely comes from the fact that we often refer to the heap (or free store) as being dynamic memory because all dynamic variables are allocated there (including variable-size arrays). But the term dynamic array does not refer to the memory, it refers to the dynamic -- as in changeable -- nature of the array itself. By contrast, a fixed-size array is only deemed static if it is statically allocated, in which case it will be allocated in the program's data segment along with all other static variables, global variables and constants. But a local fixed-size array is allocated on the program's stack and is therefore, by definition, non-static. Moreover, you can allocate a fixed-size array on the heap!


Why should a function that accepts an array as an argument and processes that array also accept an argument specifying the array?

Basically in c++ passing an array as an argument only provides a pointer to the first value and that function won't know how many values it has.If you read beyond the size you will just get garbage from memory.


Where can someone download Java for free?

Java is a free program which can be downloaded from the official site. The official site hosts an array of java related programs ranging from SDK's to just the runtime enviroment.


Why array bound checks are not implemented by C plus plus compilers?

Array bounds checks are implemented in C++ just as they are in C. Static arrays are always bounds-checked at compile time but dynamic arrays must be bounds-checked at runtime, placing the onus upon the programmer to ensure all bounds are within range. Often, dynamic array bounds checks are completely unnecessary and would simply add a processing overhead that would only result in inefficient code, thus it is not implemented by default at runtime. The programmer is therefore free to use bounds checks only when it is absolutely necessary.


What does buffer overflow mean?

A buffer overflow occurs when a program tries to store some amount of data in a location which cannot contain that amount of data. For example, trying to store an array of 10 integers in an array with room for 5 integers will cause a buffer overflow. Buffer overflows are common in C/C++, specifically in the strcpy function. This function copies the data from one string (char array) into another. It is often abused by clever hackers to either crash a program or to inject their own instructions onto the execution stack. The "extra" data copied over from the larger array doesn't magically disappear. The "overflow" refers to that data overwriting data on the stack. While this will often just crash the program (or computer), those clever hackers mentioned above may happen to know enough about the program structure to be able to insert their own low-level computer instructions to execute arbitrary code on the machine.