out of array index? | Sololearn: Learn to code for FREE!

+1

out of array index?

https://code.sololearn.com/cq4xACSzkEId/?ref=app whay this happened?(whay arr[2] is anonymous Integer?) and other question is: is there runtime error in c++?

4/16/2021 8:21:23 PM

Mehran

19 Answers

New Answer

+2

The value that you are getting when accessing an out of bounds array element is completely random. It could be that it returns the contents of a different variable or just random data in memory or on the stack. It will vary depending on where the specific compiler allocates the memory for the array. Here is an example of accessing an out of bounds variable in the code playground. Notice that it often prints a different value each time it runs. https://code.sololearn.com/ca2A21A5a8a0 You cannot predict what will be returned.

+2

In c++ imagine array saved in memory one by one and there isnt any alert if you get out of array index it just continue reading the memory so you need be careful with this

+2

Mostly just in memory alocated for this process, so you cant crash your system or apps

+2

It can be whatever like another piece of your code or also just garbage, probably not big meaning

+2

C and C++ are stack based languages. Functions, including main, use a stack frame to store the return address and local variables used in the function. Since an array is a continuous block of memory it's elements are accessed by using an offset from the starting address. If you attempt to access an out of bounds array address the compiler is still able to compute that address but is not aware that it is beyond the end of the array. Reading such an address simply reads the value stored at that address on the stack. Writing to such an address can be catastrophic. The compiler also uses the stack to save the values of the cpu registers as it is executing the function. It may also create temporary variables that are not explicitly part of your C/C++ code to hold intermediate values. Writing to the stack will corrupt these values with dire results. This, combined with the fact that data is sometimes padded to fall on 64-bit boundaries, means that it is difficult to predict the exact data on the stack beyond the end of the array. To learn more about stack frames see: https://en.wikipedia.org/wiki/Call_stack

+2

Martin Taylor thanks it was useful for me.

+1

David Dolejší Can pointers access to any place of memory?

+1

David Dolejší wich apps stored that Integer on this index?

+1

David Dolejší i I realized that one index after array length is always zero. (test code) why?

+1

How many times did u try it? I m not expert so dunno, u can try save another value and I m sure it will work

+1

Mehran Raeesi, one index after the array length is not guaranteed to be zero. I think you are getting confused with null terminated C string character arrays. In a null terminated string the string is terminated by a null character. For example, the string "Hello" contains 5 characters but a 6th NULL character exists that terminates the string. include <iostream> #include <cstring> using namespace std; int main() { char txt[] = "Hello"; cout << "The string length is " << strlen(txt) << endl; cout << "The array size is " << sizeof(txt) << endl; return 0; } Output: The string length is 5 The array size is 6

+1

Martin Taylor are you see my code? i know character array end by null. my code: int arr[]={2,1}; cout<<arr[2]<<endl;//Same as *(a+2). result is anonymous Integer and arr[3] is 0 or 1 depends on compiler that i check. why?

+1

Martin Taylor answer another my question about c++ (see my activity)

+1

Mehran Raeesi, I access SoloLearn using a web browser not the app. I can't see your activity. Your questions do not show up in your profile.

+1

Martin Taylor it is my another question : using c++ language? i love c/c++ because it is very self-description(concepts describe themselves with computer architecture) and powerful. But it no longer has much use in the world, in Windows c#(Microsoft offers c#) is used on Android java, and on the web the client uses JavaScripit and etc, If I learn it, I will forget it because I have no use. What your offer? when I mention you, are you get notification?

+1

Mehran Raeesi, try changing your code to... int arr[]={1, 2}; int a = 567; cout << "array " << arr[2] << endl; cout << "int " << a << endl; and see what happens.

+1

ok it is beautiful. second variable use memory cell after array because that declared after it.

+1

Martin Taylor change code with it int arr[] ={1,2}; int a=3 int b=4; cout <<arr[2]<<endl ; cout <<arr[3]<<endl ; //result is 4, 3 why? are data transfer in memory?

+1

these tests depend on compiler.