If I break in line 3:
1 int foo()
2 {
3 return func();
4 }
is there a way to examine the return value of func()?
Thanks.
Use the finish command.
After line three the return value will be in EAX, so you can
print $eax
Hope this helps
If you step into "func()", and then say "finish", gdb will return
to foo and print the return value of func.
I answered a simular question here, info frame is a platform independent way todo this.
Related
I'm practicing a c++ code on pthreads and there is some code I'm struggling to understand, I'm hoping someone here can give me some help with code explanation. Any help is appreciated, thanks.
01.#include <pthread.h>
02.#include <iostream>
03.#define SIZE 10
04.using namespace std;
05.int array[SIZE];
06.void *countNegativeNumbers(void *arg){...} //Assuming this function works properly
07.void *calAverageNumber(void *arg){...} //Assuming this function works properly
08.void *printReverseOrder(void *arg){...} //Assuming this function works properly
09.int main(){
10. pthread_t id,t1,t2,t3; //add pthreads and declare it here
11. int creatT; //declare the int variable for creat thread later
12. for(int i=0; i<10;i++){
13. cin>>array[i];}
14. creatT=pthread_create(&t1, NULL, countNegativeNumbers, NULL);
15. if(creatT){
16. cout << "ERROR; return code from pthread_create() is " << creatT << endl;
17. return -1;}
18. sleep(5); // busy wait.
19. // same as line 14 to 18 creat threads for t2,t3 and do if statement and busy wait.
20. creatT = pthread_join(t1, &status);
21. if(creatT){
22. cout<<" ERROR; failed to join "<<creatT<<endl;}
23. // same as line 20 to 22 terminate t2 and t3 here.
24. pthread_exit(0);
25. }
Question:
For lines 11-12, it uses a for loop to collect users' key in values, do we have any other options to do it? I cant use cin>>array[i]; since i is not declare here right?
Could anyone give me a detailed explanation for code lines 15 to 17? Do I really need those codes here? I think this is some kind of check if the thread is here or not?
For lines 20 to 22, pthread_join(thread, pointer) since int main has no pointer why I could not use NULL here?
line 24 pthread_exit(0); what is the 0 stand for?
Thanks for any helps here!
1 - declare the arraywith definite size at line 5 , u also can use any method for getting input but this here are working fine
2 - if u look at manual of pthread create return value section , u will see that it return zero on success and error number otherwise, here is link for ref
https://man7.org/linux/man-pages/man3/pthread_create.3.html
3- has same return value as pthread_create also for ref
https://man7.org/linux/man-pages/man3/pthread_join.3.html
4- it terminate the thread and release all the resources it was locked by it
https://man7.org/linux/man-pages/man3/pthread_exit.3.html
i also recommend using manual in future it will help u have better understanding of the code
For lines 11-12, it uses a for loop to collect users' key in values,
do we have any other options to do it? I cant use cin>>array[i]; since
i is not declare here right?
I don't see anything wrong with that. i is defined in for(int i=0; i<10;i++){, so it is valid inside the loop.
Could anyone give me a detailed explanation for code lines 15 to 17?
Do I really need those codes here? I think this is some kind of check
if the thread is here or not?
Those lines are checking the return value of pthread_create(), to make sure the thread was actually created. See the pthread_create() manual.
For lines 20 to 22, pthread_join(thread, pointer) since int main has
no pointer why I could not use NULL here?
pthread_join() is used to join with the created thread and get its return value once it exits. The pointer argument could be NULL if you don't care about the return value.
line 24 pthread_exit(0); what is the 0 stand for?
The return value. In this case, it is terminating the main thread and returning 0. see pthread_exit().
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 5 years ago.
Improve this question
I made this function that returns the number of digits in an integer:
it works fine when I used ELSE to return the count
int getIndex(int number, int count) { // at first call count is 0
number /= 10;
if (number > 0){
getIndex(number,++count);
}
else{
return ++count;
}
}
but when I first tried to execute without ELSE statement I thought function will be called recursively till IF condition is not met and then only it will encounter the return statement
And function will exit there as integer is returned, but
actually
if the number contains more than one digit, doesn't matter how many time it increase with recursive call it outputs 2
Just curious where as to why I am getting my concept wrong
it works fine when I used ELSE
Actually, the behaviour of the shown program is undefined. If the if branch is entered, then no return statement will be reached, and the behaviour of the program will be undefined.
When you remove the else statement and instead return unconditionally, the behaviour is well defined: The function will always return count + 1 or count + 2 depending on the value of number (which isn't correct).
Consider this, where do you use the value of the recursive function call? Nowhere; you simply discard the value. Would it make sense to return that value to the caller? Yes, it would. If only you returned within the if branch, the behaviour would be correct.
return getIndex(number,++count);
Then it won't matter whether the recursion-terminating branch is within else or not, it will only be executed if the if branch is not executed:
You are missing a return before the recursive call.
By default, it returns 0 (for some compilers).
Edit:
But actually the code should have looked like this:
int getIndex(int number) {
if (number > 0) {
return getIndex(number/10) + 1;
} else {
return 0;
}
}
I need to check the return value of a function from a TRACE32 script.
Reading the documentation I see a possible solution to read Program Counter(IP) register and then after executing the instruction at address where PC points to take the value from there.
Is there any other function which returns directly the value returned by a function ?
Every function has usually a pseudo-variable called "return". You can see that in window sYmbol.Browse.Var \\*\*\<myfunc>\* (where myfunc is the name of your function)
Of any variable you can get its value with PRACTICE function Var.VALUE(<variable>).
So you get the return value of function myfunc() with
GO sYmbol.EXIT(myfunc) // go to return statement of myfunc
PRINT Var.VALUE(return) // get the return value
If you'd like to do a module test, another approach might be interesting for you: So imaging you just want to call the function int func3(int a, int b) with random arguments (e.g. 5 and 3) and get the return value. In this case, do the following:
Var.NEWLOCAL \x // create artificial variable on the PRACTICE stack
Var.Set \x=func3(5,3) // execute func3() with arguments 5 and 3 on your CPU
PRINT Var.VALUE(\x) // get the return value
Is this ever valid? Will the project compile? I do not have access to a compiler right now, but my friend had this in his code:
int returnTwice () {
return 1;
return 2;
}
Why and how is this inappropriate?
Thank you!
Thank you everyone that had something constructive to say.
It's partly valid.
The invalid part is that you try to declare a variable in an expression, which is not allowed. But there's nothing illegal by having multiple unconditional return statements, however only the first will be executed.
No, this isn't possible in C++ or any other programming language that I know of.
As another user who answered your question said, only the first return statement will be executed. Depending entirely on the compiler, it might give you an error or warning that you have two return functions in one defined scope, there is no syntax error here.
It is possible to return two or more values in C++ by placing each variable into a vector and returning it, as documented in this question.
A return returns the value assigned to it and exits the function.
In other programming languages such as Lua a return variable1, variable2; can be used.
It's not possible in C++. But, if you want a similar behaviour, you can use boost::tuple.
boost::tuple<double,double> figInfo(const Figure& fig)
{
double p = fig.getPerimeter();
double s = fig.getSurface();
return boost::make_tuple(p,s);
}
boost::tuple<std::string, unsigned short int, std::string> profile()
{
std::string first_name = "Christophe";
unsigned short int age = 29;
std::string address = "Unspecified";
return boost::make_tuple(first_name, age, address);
}
When a function returns, it stops executing. Everything after the first return executed will never get executed. Thus, you should get an "Error: unreachable code" for such a function.
I have the following code in c++:
int fff ( int a , int b )
{
if (a>b )
return 0;
else a+b ;
}
although I didn't write 'return' after else it does not make error ! < br/>
in main() when I wrote:
cout<<fff(1,2);
it printed 1 ?
How did that happened
can any one Explain that ?
This what is called undefined behavior. Anything can happen.
C++ does not require you to always return a value at the end of a function, because it's possible to write code that never gets there:
int fff ( int a , int b )
{
if (a>b )
return 0;
else return a+b;
// still no return at end of function
// syntactically, just as bad as original example
// semantically, nothing bad can happen
}
However, the compiler cannot determine if you never get to the end of the function, and the most it can do is give a warning. It's up to you to avoid falling off the end without a return.
And if you do, you might get a random value, or you might crash.
$6.6.3/2- "Flowing off the end of a
function is equivalent to a return
with no value; this results in
undefined behavior in a
value-returning function."
A compiler may or may not diagnose such a condition.
Here
else a + b;
is treated as an expression without any side effect.
the "random" return vaule is determined by the CPU register value after the call, since the register is 1 after the call, so the value is 1.
If you change you code, the function will return diffrent value.
A good compiler (e.g. gcc) will issue a warning if you make such a mistake, and have a command line switch to return a non-zero error status if any warnings were encountered. This is undefined behaviour: the result you're seeing is whatever value happened to be in the place that the compiler would normally expect a function returning int to use: for example, the accumulator register or some spot on the stack. Your code doesn't copy a+b into that location, so whatever was last put in there will be seen instead. Still, you're not even guaranteed to get a result - some compiler/architecture might do something that can crash the machine if the function didn't have a return statement: for example - pop() a value from the stack on the assumption that return has pushed one - future uses of the stack (including reading function-return addresses) could then get results from the memory address above or below the intended one.