Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
The community reviewed whether to reopen this question last month and left it closed:
Original close reason(s) were not resolved
Improve this question
After reading this answer I decided to try it. To my surprise, the following code is working, and the reference is correctly reseated. Why's that?
#include <iostream>
using namespace std;
int main()
{
int a = 0;
int b = 1;
int& ref{a};
ref = b;
cout << ref << endl;
return 0;
}
The reference ref itself was not reset. It still references the variable a. This statement
ref = b;
changed the value of the referenced variable a. To be sure insert this statement
std::cout << "a = " << a << '\n';
You can consider references as an aliases for variables they refer.
A reference is just an alias to the variable it refers to. Once a reference has been bound, it cannot be rebound. Anything you do to a reference afterwards actually happens instead to the variable it refers to. If you read from a reference, it reads from the variable instead. If you assign a value to a reference, it assigns the value to the variable instead. If you take the address of a reference, it takes the address of the variable instead.
So, these statements:
ref = b;
cout << ref << endl;
are really just doing this instead:
a = b;
cout << a << endl;
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I don’t know what is wrong with the following code.
It tells address of local variable returned.
#include <iostream>
using namespace std;
int *myfunc(int *ptrB);
int main() {
int *a, b;
cout << "give b :" << endl;
cin >> b;
a = myfunc(&b);
cout << "a is :" << *a << endl;
return 0;
}
int *myfunc(int *ptrB) {
int a;
a= (*ptrB) * (*ptrB);
*ptrB = a;
return &a;
}
"I don't know what is wrong." - What is wrong is that you are returning the address of a local variable that ceases to exist once the function returns, so that address points to a dead object and trying to use it is UB.
When you do anything with a pointer that is returned by a function, like dereference it, which is a typical thing to do with a pointer, you need to know that the object that your pointer points to exists. However, the pointer you return points to an object that exists in scope of the returning function; after the function has returned, the function scope no longer exists and the object goes down with it. It is like holding an address of a house that has been demolished and hoping you can spend the night there.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
int *x = new int;
*x = 5;
cout << x << endl;
delete[] x;
I am trying to dynamically allocate resources to a variable 'x'. I then assign the value '5' to it. When I try to print the value of 'x', I get some garbage value called 0x8321 and so on. I am sure that is not the value I assigned.
Any idea how I can access the value of x?
You must use std::cout << *x << std::endl.
The value that you are getting is the address of the memory (region) to where the pointer is pointing to.
EDIT : And use delete x; instead of delete[] x;.
You are currently printing the address of the pointer. If you want the value you should do
cout << *x << endl
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have this class definition:
class event {
public:
wstring type;
int pos;
int sen;
event(const string &t) : type(util::string2wstring(t)) {}
~event() {};
};
and the global variable:
list<event> events;
This variable was initialize in one function and after that I want to modify differents values of this events like:
for(auto ei : events) {
ei.pos = (*w).get_position();
ei.sen = sen;
cout << "pos: " << ei.pos << " in sentence " << ei.sen << endl;
++w;
++sen;
}
event ei = events.front();
cout << "pos2: " << ei.pos << " in sentence2 " << ei.sen << endl;
Then the first cout print the correct values of the new pos and sen but the second (outside the for scope) print the oldest values.
If I print the values in other function the oldest values are printed.
I think that the problem is no reference access to the global variable but is it true?
And the most important, how can I fix it?
Thanks for your time,
Regards.
The problem is that ei in your range-for loop is a value and not a reference. That means it is a copy of the element in the container, and modifying a copy of course does not modify the original.
To use references you need to specify it:
for(auto& ei : events) { ... }
// ^
// Note ampersand here
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
int f_point(int kek,int *lol) {
*lol *= *lol;
return kek;
}
int main {
int x;
std::cin >> x;
int *adress_of_x = &x;
int a,b = f_point(x,&x); //how does it work?
std::cout << a << LINE_JUMP;
std::cout << b << LINE_JUMP;
}
For example, if I give 2 to program then I will get 0 and 2. Why?
b = f_point(x,&x) in this statement value of first parameter is 2.
Your function is not changing the value of first parameter and returns the same value.
Your are passing first parameter by value so it has no relation with the updated value of x. Variable a is uninitialized, so it is taking a garbage value.
int a,b = f_point(x,&x); //how does it work?
The variable declaration leaves a uninitialized and initializes b from the result of f_point(x,&x);.
Since it's an uninitialized variable, accessing the value of a in the
std::cout << a << LINE_JUMP;
statement leads to undefined behavior of your program. Having an output of 0 is just one of any possibilities (including your fridge explodes unexpectedly or little demons flying out of your nostrils).
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'd like to know why local variables do not get recreated if the function is recalled?
#include <iostream>
using namespace std;
void func(void)
{
int a = 0;
cout << &a << endl;
}
int main(void)
{
func();
func();
func();
func();
system("pause");
return 0;
}
Why is the variable a mapped every single time to the same memory address?
The address of a local variable is not defined by the standard. It may be the same from one call to the next, or it may be different.
In this particular case, with this code and on your machine and compiler, the address is most likely the same because each time func is called it is called from main, so the stack is laid out the same each time. If you were to call some other function from main and then call func, the address would most likely be different.
This behavior cannot be depended on, however.
The variable is recreated every time, but the same address is reused because… well, because why not? Every time, it is an available memory location with nothing else in it. Why not use it? If you required a different location each time, you'd eventually "run out" of memory for no reason. It's convenient for your system to re-use the same location for the same variable in the same function. If your program were more complex, you'd be likely to see less predictable behaviour, though.
You've demonstrated that stack variables may be at the same address when you call a function when the stack is in the same state.
Why not try calling the function when the stack is in a different state? For example, make it recursive:
#include <iostream>
using namespace std;
void func(int n)
{
int a = 0;
cout << "n=" << n << ": " << &a << endl;
if (0 < n)
func(n-1);
}
int main()
{
func(5);
return 0;
}
Example output:
n=5: 0x7fff519aa848
n=4: 0x7fff519aa808
n=3: 0x7fff519aa7c8
n=2: 0x7fff519aa788
n=1: 0x7fff519aa748
n=0: 0x7fff519aa708
As you can see, each call to func() has the stack in a different state, therefore the address of a can be different each time.