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).
Related
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 2 years ago.
Improve this question
first post here so be gentle. I'm trying execute a recursive binary search. I have tried different variations of code to try to make the function work but i still doesnt.
Here's my code:
bool contained(int x, const int* pBegin, const int* pEnd){
if(x==*pBegin) { //
return pBegin;
}
int size = pEnd - pBegin; //size of the array
int mid= size/2; //the middle of the array
int const* pMid = pBegin + mid; //The address of the element in the middle of the array
if(x>*pMid) //Condition that looks if x is to the left of the array
return contained(x,pMid,pEnd);
else if(x<*pMid) //Condition that looks if x is to the right of the array
return contained(x,pBegin,pMid-1);
}
int main()
{
cout << "Binary search, test: " << endl;
int arr[] = {1,2,3,4,5,7,8,9};
int size = 9;
for(int i=0; i<size; i+=1)
arr[i] = i;
bool find = containedInSortedarray(6, &arr[0], &arr[size]);
cout << "Found " << find << endl;
}
For example here, when i execute my bool-contained function with a premade array and let it search for the value 6, it should eventually come to the conclusion that the element does not exist, yet my output says it does. Have i missed something in my code?
Thanks in advance!
There are a couple of issues with your code:
You never return false.
You return pBegin which is not a bool, (sadly in your case) it is implicitly convertible to one, but not in the way you want it too. Turn on your compiler warnings all the way up - -Wextra -Wall -pedantic -Werror should be the bare minimum, especially if you are a beginner.
Be precise about the interval your function searches in. func(x,a,b) - does it include b? Based on contained(x,pBegin,pMid-1); it seems it does, but in the case of containedInSortedarray(6, &arr[0], &arr[size]); hopefully not.
Dereferencing &arr[size] is UB.
What is the purpose of the for loop in main?
Having the size detached from the array is a disaster waiting to happen. At least use sizeof(arr)/sizeof(arr[0]). Better yet, use std::array or a std::vector.
I would suggest searching [a,b) because it can be easily be divided into [a,mid), [mid,end). That way your mid computation is already correct. You can then call it like containedInSortedarray(x,arr,arr+arr_size)
The base condition should catch the arrays of size 0 and 1 - both can be trivially tested for presence of x.
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 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
Is it possible to declare a variable without assigning value in C++?
int a; int a=15; Which one will be Correct?
If i assign value to a variable 3 times or more which one will count at the end in C++??
int a=15;
int a=10;
int a=5;
Which value will be execute for a at the end?
int a; // declared but not assigned
a = 1; // assigning a value
a = 2; // assigning a different value
a = 3; // assigning another value
std::cout << a << "\n"; // will print 3 since only the last assignment matters
int x; declares a variable x without assigning a value.
If you assign to a variable three times then which ever assignment executed last will be the variables final value. Very important idea, C++ statements execute in a specific order, and which order they execute in is essential to understanding what a program does.
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 4 years ago.
Improve this question
The following code results in an infinite loop. The value of a does not seem to be going lower than 1. Please help.
int main()
{
init(4);
return 0;
}
void init(int a)
{
int j;
cout<<"After declaring: "<<j;
j = a;
cout<<"After initializing: "<<j;
a--;
while(a>0)
{
init(a);
}
}
First, you are accessing an uninitialized variable. This introduces undefined behaviour into your program; I'd remove this section.
Second, your while-loop runs as long as a > 0, but in the body of the loop, the value of a is never changed. Note that when calling init, parameter a is passed by value, i.e. the value of a is copied; Each instance of init will get it's own a, and it will never change the value of the caller's a.
Recursive functions usually are used instead of a loop. Nevertheless, you need an "anchor" to tell the recursion where to stop:
void init(int a)
{
int j = a;
cout<<"After initializing: " << j;
if (a>0) {
init (a-1);
}
}
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
So, I've read this:
Will the initialization list always be processed before the constructor code?
and given the following constructor:
public:
A (int x=5):x(x+1)
{
cout << "In A::A x= " << x << endl;
}
and the sample of code in the main:
A a1(10);
I don't understand the result:
"In A::A x = 10"
when according to my logic it should be:
"In A::A x = 11"
But instead, x = 11 only after the constructor body invoked. Why is that?
The "problem" with this code is that both the parameter to the constructor and the member variable are named x. That is, x might not refer to the x you expect.
In this case, x refers to the parameter of the constructor - and that has a value of 10. The reason is that when C++ encounters a scope where there are two variables with the same identifier, then the most local scope wins. Here: the x from the parameter value. If you want to use the memver variable, change the code to use this.x instead of just x:
A (int x=5):x(x+1)
{
cout << "In A::A x= " << this->x << endl;
}
Now the value you see should be 11, not 10. this is a pointer to the current object, so this->x is the value of the member variable x of the current object.
Of course, it would be still better to use different names. That way you can avoid such confusion.
x in the body of your constructor means the argument x.
The first x in x(x+1) is the member field of your class A. That is the source of the confusion.
You should give another name to your member field. Some conventions start them with a _ or a m_.
class A {
private:
int m_x;
public:
A (int x=5):m_x(x+1)
{
cout << "In A::A x= " << m_x << endl;
}
};
In your case with A a(10);, your constructor will write 11.