Problem with C++ arrays.
-
im my textbook , i was told that you daclare an array as follows:
type var[size];
where size is constant and cannot be changed later. but if i run this :
int a[2];
a[3]=10;
cout<
it does not give any error and prints:
10how is this possible.. because the size of the array was 2!! ??
please help me understand. -
im my textbook , i was told that you daclare an array as follows:
type var[size];
where size is constant and cannot be changed later. but if i run this :
int a[2];
a[3]=10;
cout<
it does not give any error and prints:
10how is this possible.. because the size of the array was 2!! ??
please help me understand.Maybe take a look at this[^] thread, as they talk about out-of-bounds array indexing, it's undefined behaviour.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
-
im my textbook , i was told that you daclare an array as follows:
type var[size];
where size is constant and cannot be changed later. but if i run this :
int a[2];
a[3]=10;
cout<
it does not give any error and prints:
10how is this possible.. because the size of the array was 2!! ??
please help me understand.C/C++ does not enforce arrays bounds checking. Your code invokes "Undefined behavior", which means what it sounds like - the C/C++ standards don't say what will/can happen. The compiler may do anything, including make demons fly out your nose! (see Undefined behavior - Wikipedia, the free encyclopedia[^]). This is often the root of subtle, and often hard to find bugs.
-
C/C++ does not enforce arrays bounds checking. Your code invokes "Undefined behavior", which means what it sounds like - the C/C++ standards don't say what will/can happen. The compiler may do anything, including make demons fly out your nose! (see Undefined behavior - Wikipedia, the free encyclopedia[^]). This is often the root of subtle, and often hard to find bugs.
okay thanks
-
okay thanks
-
im my textbook , i was told that you daclare an array as follows:
type var[size];
where size is constant and cannot be changed later. but if i run this :
int a[2];
a[3]=10;
cout<
it does not give any error and prints:
10how is this possible.. because the size of the array was 2!! ??
please help me understand.It is possible because C and C++ do not check array boundaries, this behavior make them fast at cost of security. In such a small program, you see no consequences, but bigger program, you will see another variable suddenly changing of value when it is not supposed to.
int a[2];
int b;
b= 0;
a[3]=10;
cout<
In this example,b
may end up with 10 depending on compiler detailsPatrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
-
It is possible because C and C++ do not check array boundaries, this behavior make them fast at cost of security. In such a small program, you see no consequences, but bigger program, you will see another variable suddenly changing of value when it is not supposed to.
int a[2];
int b;
b= 0;
a[3]=10;
cout<
In this example,b
may end up with 10 depending on compiler detailsPatrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
Actually the behaviour has nothing to do with speed, its a legacy of dereferenced pointers in C p[i] is EXACTLY the same as writing *(p+i), most novice programmers will recognize the first form but not the second. The two codes are literally identical and will almost always produce the same code. The second however makes it clear that applying bound checking as a generic behaviour would break all dereferenced pointers. Some C compilers recognize the first form as a special case and will do bound checking and give a warning, but even they will not detect a bound error if the second form is used because p+i is just a pointer with an offset. That may well be, and often is intended behaviour. So array bounds has always been and will always remain the responsibility of the C programmer. A pointer can exist to a memory that is non existent, invalid or has been previously released anyhow, so the array bound situation is no more dangerous than any of those all of which are the C programmers responsibility.
In vino veritas
-
im my textbook , i was told that you daclare an array as follows:
type var[size];
where size is constant and cannot be changed later. but if i run this :
int a[2];
a[3]=10;
cout<
it does not give any error and prints:
10how is this possible.. because the size of the array was 2!! ??
please help me understand.I hate C++ :sigh: it 's so difficult
-
I hate C++ :sigh: it 's so difficult
And an easy language would be?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
-
And an easy language would be?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
C#! ;P
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
-
C#! ;P
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
Of course! nothing bad happens when you go out of range on an array in C#! :laugh:
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
-
Of course! nothing bad happens when you go out of range on an array in C#! :laugh:
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
Not sure if it's an agreement or not... Let's say: no memory corruption occurs!
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!