C++ weird array behaviour [duplicate] - c++

This question already has answers here:
How dangerous is it to access an array out of bounds?
(12 answers)
indexing past the end of C arrays [duplicate]
(2 answers)
Why doesn't my program crash when I write past the end of an array?
(9 answers)
Array overflow (why does this work?) [duplicate]
(5 answers)
The effects of writing past the end of an array [duplicate]
(5 answers)
Closed 2 years ago.
I was debugging my code for key index counting and found this problem.
I do not understand what is happening here. I've looked at the code for too long to see if I am missing something very obvious, but it does not seem like it.
int main()
{
const int r=7,len=10;
int arr[10]={1,4,6,2,0,4,3,6,5,2};
int count[r+1]={0};
for(int i=0;i<len;i++)
{
count[arr[i]+1]++;
}
cout<<arr[0]<<" ";
for(int i=0;i<r+1;i++)
{
count[i+1]+=count[i];
}
cout<<arr[0]<<" ";
return 0;
}
This is the kind of a mock up code which generates the same bug.
Output:-
1 11
I am not changing value of arr anywhere in my program and still it shows 11 instead of 1 in the output.
if I comment out count[arr[i]+1]++; or count[i+1]+=count[i]; or both it gives the correct output.
1 1
What is happening please explain. (comment if I am doing something silly).
Edit: This is only happening with arr[0].

Compiling it with g++ -Wall -Wextra you get this warning:
rando_so.cpp: In function 'int main()':
rando_so.cpp:15:19: warning: iteration 7 invokes undefined behavior [-Waggressive-loop-optimizations]
count[i+1]+=count[i];
~~~~~~~~~~^~~~~~~~~~
rando_so.cpp:13:18: note: within this loop
for(int i=0;i<r+1;i++)
This hints you to look a bit closer at that second loop. Your variable i goes up to the highest possible index of count - and then you add 1. That is undefined behaviour. In your case, it is likely that you happen to be writing into the first element of arr now, because of how it's laid out on the stack. But as far as I know, anything could happen as a result of this.

Related

looks like there is something wrong with the pre-increment operator in c++ [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Undefined behavior and sequence points
(5 answers)
Closed 19 days ago.
I was just messing with a piece of cpp code that I came across something I couldn't find an explanation for.
when I run this in c++:
int a = 5;
cout << (--a) * (--a);
9 is printed in the console. but when you get the variable (a) from the input, and then give it the same value (5), the result is different:
int a;
cin >> a;
cout << (--a) * (--a);
when you enter 5 as the input, 12 is printed in the console. Why is it so?

Why does my dynamic array work without being resized? [duplicate]

This question already has answers here:
No out of bounds error
(7 answers)
Closed 1 year ago.
I'm working on dynamic arrays for my c++ course, but I'm confused about the behavior of my dynamic arrays. For example, if I run this code:
int* myDynamicArr = new int[3];
for (int i = 0; i < 10; i++)
{
myDynamicArr[i] = i + 1;
cout << myDynamicArr[i] << endl;
}
I would expect it to not work since I only declared it as size 3. But when I run it, it prints out 0-9. Same thing if I do this:
char* myCharArr = new char[2];
strcpy(myCharArr, "ThisIsALongString");
cout << myCharArr;
It prints the full string even though it seems like it should fail. Can anyone explain what I'm doing wrong here? Thanks!
C++ does not perform bounds checking on arrays. So when you read or write past the bounds of an array you trigger undefined behavior.
With undefined behavior, your program may crash, it may output strange results, or it may (as in your case) appear to work properly.
Just because it could crash doesn't mean it will.

What does a c++ in function return without a return statement return? [duplicate]

This question already has answers here:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
What happens when a function that returns an object ends without a return statement
(3 answers)
Closed 4 years ago.
In a test, one of my classmates wrote the following function, to flip a number:
int tukor(int n)
{
int k=0;
while(n!=0)
{
k=k*10+n%10;
n=n/10;
}
n=k;
}
You will notice a complete lack of any return statements, but when cout<<tukor(1234); is run (namespaaace std is used), it outputs 4321. Now the entire class is confused as to how this is possible, even after the teacher added the lines n=0;n=12; at the end of the function. It has worked for all test cases so far.
Is this caused by undefined behavior or something similar?
EDIT: changing k before the n=k statement changes the return value, and if it is removed, the return value is 0.

How does C++ process this [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
I have here an equation i can't understand how c++ process this. Can someone explain this operation?
code:
#include <stdio.h>
main(){
int a[10] = {0,1,2,3,4,5,6,7,8,9};
int i = 0;
int num = a[i+++a[++i]]+a[++i+i++];
printf("\nnum1: %d i: %d,num,i);
}
why is the answer num = 9 while index i is just equal to 4;
Using ++ twice in the same expression on the same variable is explicitly undefined by all versions of both the C and C++ standards, and so i does not necessarily equal 4. It could be anything at the whim of the compiler writer.
Never do this. Never use ++ and -- twice in the same expression. There is no way to make any statement about what the resultant value will be, and no experience with what it does with one compiler will mean anything with respect to what another compiler does.

Why does this program output 4, instead of 3? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Output of multiple post and pre increments in one statement [duplicate]
(1 answer)
Closed 8 years ago.
int main()
{
int var = 1;
var += ++var;
cout<<var;
return 0;
}
In Java this will output 3, as expected, but in C++ (above) it outputs 4. Why?
Because this is undefined behaviour. You're modifying and accessing the same variable without an intervening sequence point, so the outcome is really up to the compiler. If you compile this with clang, you'll see:
unsequenced modification and access to 'var'
I actually get 4 as the answer, but it could equally be 3, 7, 123125123 or "Lobster".