Local Scope Resolution [duplicate] - c++

This question already has answers here:
Why are global variables always initialized to '0', but not local variables? [duplicate]
(4 answers)
Why are global and static variables initialized to their default values?
(5 answers)
Closed 1 year ago.
I decided to change the scope of a function operator from global to local. After changing from the commented code, I found that my code no longer runs and exits with error:
C4700 uninitialized local variable 'n' used.\
This seems to be a quite obvious contradiction to the actual method of local resolution. Does anyone have an explanation for this?
int Combs::factorial(int a)
{
//value = 1;
int n;
for (int i = a; i >0; i--)
{
n *= i;
}
cout << n;
return n;
}

When the variable is declared at global/file scope, the compiler initializes it for you, when it's local to a function, it doesn't, so you need to do it yourself.
n is indeed used unitialized, when it was a global variable it was not.

Related

what will happen to if value of x defined as "int x;" and value of y defined as "int y = 1;" and use min(x,y) in C++? [duplicate]

This question already has answers here:
What happens to uninitialized variables? C++ [duplicate]
(3 answers)
Uninitialized variable behaviour in C++
(4 answers)
Closed 2 years ago.
int main(){
int x;
int y = 1;
int minNumber = min(x,y);
cout << minNumber;
return 0;
}
I know x and y are different initialization. But I do not know what excatly happen when I call min() function. Thanks for your time.
It is undefined behavior because x is uninitialized non static local variable.
x is not initialized and hence it would have some value as per what's there in memory location which is assigned for x, by the system. min function's return value will be unpredictable, in this case.

local variable initiated to zero explicitly and not have an unknown value in c++ language [duplicate]

This question already has answers here:
Variable initialization in C++
(11 answers)
Closed 3 years ago.
Testing some snippets, I found a very strange behaviour. I have 2 local variables that it's not initialized. And one that it's used later in the program is always initialized to zero, the other has a garbage value (what I expected to get as a normal behavior). As if the compiler checks that I will use it in a while loop and it initialized to zero implicitly. I don't understand this behavior.
int i,j; // variable declaration and initialization
cout << i <<' '<< j << endl;
while ( i < 10 ) // condition
{ cout << i << '\n';
i++; // variable update
}
If I include the above snippet in a main program, it seems it works well always. The value of i is always zero before it enters the while loop and the while loop works well always. But the variable j has any unknown variable (garbage)
In fact,I copied this snippet directly from the book: Jumping into C++ by Alex Allain, page # 74. (I added variable j for comparison only)
Line 1 of the program is a declaration, not an initialization. Using uninitialized variables leads to undefined behavior. It’s pure luck that the loop variable i is initialized to 0 if it indeed is 0.

How does C++ proceed for += with a variable without value? [duplicate]

This question already has answers here:
What happens when I print an uninitialized variable in C++? [duplicate]
(4 answers)
Closed 6 years ago.
i am currently learning C++ and i had a question about some "weird" things i noticed while testing what i have learnt.
So i use this :
int marc[9];
int sum = 0;
for (int x =0; x < 9; x++) {
marc[x] = x*4;
sum += marc[x];
}
cout << sum << endl;
And i get a normal result which is 144
However, if i changed
int sum = 0;
to
int sum;
I got a crazy result like 19557555002
So i was wondering what is happening in the second case?
Thanks for your answer :)
You are operating on uninitialized memory in the second case. Non-static local variables are not initialized to zero by the runtime like in other languages, if you need sum to have a defined value, you must initialize it yourself. The 19557555002 will be the integer interpretation of any bytes that were present at the memory address allocated for sum.
Further reading: What happens to a declared, uninitialized variable in C? Does it have a value?
Its called an undefined behavior and it happens when you don't initialize your variables.
int sum;
above code can only declare a variable but it doesn't initialize it by default so the variable contains a garbage value.
this creates an uninitialized int int sum;
it can have "garbage" values, and this is exactly what happened to you
how this happens: let's say you use an int x in address y, and sets it to 19557555002. now, lets say you "leave" that address (go out of scope, program terminates, OS takes that memory...) and someone else takes it because he wants to put there a new int. if he just declares his int, without initializing it, his int can be stationed (if the OS so desires...) in address y, that previously used to hold your int, which means in address y, he will find 19557555002. That is what could happen to you if you don't initialize variables.
Memory for local variables is typically allocated on the stack. This means that without some initialization, they will hold some data that was residing there previously.
As others said it is undefined behavior, but practically, on most implementations, it results in effects like this:
void foo()
{
int a = 5;
}
void bar()
{
int b;
std::cout << b;
}
void someCaller()
{
foo();
bar();
}
On most implementations, this will usually result in the printing of 5 on the stdout.
Note that some compilers like MSVC initialize all variables in Debug configuration, but usually any kind of optimization flags will avoid initializing memory, if not explicitly requested.

How this piece of code accessing the local variable outside the C++ function? [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 6 years ago.
int * addition(int arr[])
{
int sum=0;
for(int i=0;i<4;i++)
sum+=arr[i];
return ∑
}
int main()
{
int arr[4]{1,3,4,5}, * ptr=addition(arr);
cout<<*ptr<<endl;
return 0;
}
As the variable sum is local to the function addition so the variable should be destroyed as soon as the program control moves out of the function but it is still giving the output 13. Why?
Compiler: g++ 4.8.2 on Ubuntu 14.04 LTS
The variable sum is on the stack. When the function addition() returns it doesn't zero the released stack memory, so the address returned still contains the sum.
Does it still work if you enable optimisation with -O3 ?

I want to know how it will act when the function "add" return a reference to local variable , but it run correctly, no crash [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 8 years ago.
please look at this code firstly . I run it on g++ complier.
I want to know how it will act when the function "add" return a reference to local variable ,
but it run correctly, no crash . Why ?
int & add (int a,int b) {
int c = a + b ;
return c ;
}
int main()
{
cout<<add(1,2)<<endl;
int a = add(1,2);
cout<<a<<endl;
}
Because it is undefined behavior. You can't rely on it working and as your code grows it won't.
Do not return a reference to a local variable.
It may or may not work.
But will sometime down the line it will bite you.