Calling function with variable that is being initialized [duplicate] - c++

This question already has answers here:
Can initializing expression use the variable itself?
(3 answers)
What's the behavior of an uninitialized variable used as its own initializer?
(3 answers)
Self-assignment of variable in its definition
(3 answers)
Closed 6 months ago.
I am learning C++ using the books listed here. In particular, I read that using an uninitialized local variable of built in type is undefined behavior. Now, I came across the below given example where the user seems to be using uninitialized variables x and y. The user says:
#include <string>
template<typename T>
T num_convert(const char* s, const T) {
return static_cast<T>(std::stoll(s));
}
int main() {
//--------------------------v---------------> is this UB because x is uninitialized?
int x = num_convert("55", x);
//-----------------------------------v------> is this UB because y is uninitialized?
long y = num_convert("5555555555", y);
}
In the above example, I've used arrows to highlight the points where I have doubts. In particular, I want to know that are the above two highlighted statements UB because x and y are not initialized and the user is passing those to the function by value?

I want to know that are the above two highlighted statements UB because x and y are not initialized and the user is passing those to the function by value?
Yes, your understanding is correct. The variable x and y are uninitialized local variables and they're being used/passed by value when calling the functions. Thus, the program has undefined behavior as x and y have indeterminate values and you're using/copying those values to the parameter of the function.

Related

Is it wrong to declare two pointer variables with comma? [duplicate]

This question already has answers here:
How to initialize multiple variables in C++ to the same value?
(7 answers)
Closed 1 year ago.
#include<iostream>
using namespace std;
int main()
{
string y="hello";
string* x, *z= &y;
cout<<x<<endl;
cout<<*x;
}
In the above code, when I delete *z I get the correct output (address of hello and "hello"). But, when *z is present I get some weird output. Why can't both z and x store the address of y.
The shown program is wrong. You provide an initialiser only for z and not for x. Thus, you've default initialised x and it has indeterminate value. When you read the indeterminate value (and indirect through the pointer), the behaviour of the program is undefined.
Why can't both z and x store the address of y.
Two pointers can store the same address, but they won't store the same address unless you initialise or assign the pointers to have the same address.
Is it wrong to declare two pointer variables with comma?
It is possible to declare two pointer variables with a comma, but some people consider declaring more than one entity per declaration as bad style.
If you want to initialize both, have code like this:
string *x = &y, *z= &y;
Your code only initializes z.
And you talk about deleting z... don't do that. z points to y which was not allocated with new...

What happens under the hood in any C++ compiler with reference variables? [duplicate]

This question already has answers here:
What happens after C++ references are compiled?
(2 answers)
How references are internally stored in c++? [duplicate]
(3 answers)
Closed 1 year ago.
When we declare a reference variables int & x = something, is the & just a syntactical shorthand in the source code for referencing the value of the variable something? What I mean is, does the compiler insert, under the hood, the necessary calls for pointers in order to access the value of something, or does it use some more direct approach? E.g. do we have that int x & = something is equal to int* x = &something, and using the value at x like x = 10 results in the code *x = 10?

why const_cast is creating two values for one variable [duplicate]

This question already has answers here:
Two different values at the same memory address
(7 answers)
Closed 5 years ago.
Consider the following code:
I declare a new reference end assign it to value a via const_cast. Then I just increase the reference value print the addresses and values.
#include <iostream>
using namespace std;
int main()
{
const int a = 7;
int &b = const_cast<int&>(a);
++b;
cout<<"Addresses "<<&a<<" "<<&b<<endl;
cout<<"Values "<<a<<" "<<b<<endl;
}
//output
Addresses 0x7fff11f8e30c 0x7fff11f8e30c
Values 7 8
How can i have 2 different values in the same address??
Because modifying a variable declared to be const is undefined behavior, literally anything can happen.
Modifying a constant object gives undefined behaviour, so your program could (in principle) do anything.
One reason for leaving this behaviour undefined is to allow the optimisation of replacing a constant variable with its value (since you've stated that the value can never change). That's what is happening here: a is replaced with the value 7 at compile time, and so will keep that value whatever you try to do to it at run time.
Even though const_cast may remove constness from any pointer or reference, using the resulting pointer or reference to write to an object that was declared const invokes undefined behavior.
check out the example here for more illustration:
http://en.cppreference.com/w/cpp/language/const_cast
Any attempt to modify an object that is itself declared const by means of const_cast results in undefined behavior according to the ISO C++ Standard.
When the we refer "const object", it intends to say that the memory where the object is located may be write-protected. That is, a variable or expression of const type may denote an object stored in write-protected memory and any attempt to modify the object results in undefined behavior
EDIT: Refer this site for more info
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1994/N0571.asc

What's the difference between accessing struct members by pointer as opposed to by reference? [duplicate]

This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
When to use references vs. pointers
(17 answers)
Closed 8 years ago.
In "The C++ Programming Language (4th Edition)" Section 2.3.1, Stroustrup shows 3 different ways to access members of a struct:
void f(Vector v, Vector& rv, Vector* pv)
{
int i1 = v.sz; // access through name
int i2 = rv.sz; // access through reference
int i4 = pv->sz; // access through pointer
}
I understand that for the first one, v is passed-by-value, so a copy of the first argument is put on the function's stack and the value of its size sz is stored inside of i1.
In the second example, rv is a reference to the struct passed as the second argument. Because it is a reference, we can access the value referred to by rv without a * prefix.
I'm not too sure I understand what's going on with i4, and why someone would pick the third example over the second (or vice-versa).

Difference between int f(int *i) and int f(int &i) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What are the differences between pointer variable and reference variable in C++?
Pointer vs. Reference
To do a call-by-reference in C++, I think I could use either of those two:
int f(int *x);
int g(int &x);
They would be called like so:
int *w;
f(w);
g(*w);
int y;
f(&y);
g(y);
Is there a difference in the functions f and g? I should be able to work with x as an int* pointer and *x as an int inside both functions. So what is the difference?
Most of this is a matter of taste. There is one important difference though. A pointer (*) can have a null value whereas a reference cannot be null and always must refer to a valid object.
The reference can't be NULL, so you don't have to check that. Otherwise, it's probably just syntactic sugar (at least for simple use cases). Check the disassembly of your program to see.