LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   A little help on return prototype (https://www.linuxquestions.org/questions/programming-9/a-little-help-on-return-prototype-894409/)

alikamu 07-29-2011 12:28 AM

A little help on return prototype
 
Code:


#include<stdio.h>
int sum (int *,int *);
int main()
{
int *a,*b;
c=sum(a,b);
printf("%d\n",c);
return 0;
}
int sum (int *a, int *b)
{
//i added the value and return the sum
}

Now can you explain, that when i return the value to main, how does it work, since the memory to the pointer is given in heap and it will be destoryed as soon as the function call is over, so how is the value return and saved in c in main.

thanks

Ramurd 07-29-2011 12:33 AM

because the function was declared; Therefore, when the function was called, it's return value would be taken.
If you'd return by reference (if that's actually possible) there's a chance that you get a segv; I've no idea how compilers would go about that... never done returning by reference on a variable that would be destroyed from the heap.

Peverel 07-29-2011 02:35 PM

Be careful about words here: the heap is where malloc and its friends operate, and is global: you mean the stack. The answer is simple: the stack is cut back, losing the stack frame for the function, including parameters, and the return value is added to the top of the stack, in the stack frame for main. Since sum() is declared as int, the return value is dereferenced, if necessary, and returned as int.
You do not give the code for sum, so it is not clear if dereferencing is necessary.

edit: You do not say how a and b are given values, or how the sum is constructed. If malloc is used, then the values really are on the heap and are global. For example, if you create space for an int inside sum() using malloc, then that still exists, even though (probably) inaccessible, since the pointer to it is lost when sum() returns.

dwhitney67 07-29-2011 07:22 PM

Quote:

Originally Posted by Peverel (Post 4428746)
Be careful ... If malloc is used, then the values really are on the heap and are global.

Be careful yourself of using the term "global". An "object" allocated on the heap is not global; a handle (or pointer) to said object is available, but only if 1) it is defined (ie assigned the result of the function allocating the memory), and 2) it is available to each and every function where it is needed.

In layman's terms:

Code:

void function_one()
{
  int* var = malloc(sizeof(int));

  function_two();
}

void function_two()
{
  printf("I haven't a clue about the variable 'var' declared in function_one()!\n");
}


ta0kira 07-29-2011 11:50 PM

Quote:

Originally Posted by Ramurd (Post 4428194)
If you'd return by reference (if that's actually possible) there's a chance that you get a segv; I've no idea how compilers would go about that... never done returning by reference on a variable that would be destroyed from the heap.

In C++ you can return heap objects by reference without issue; you just have to take the address when you delete. Returning a stack object by reference will cause problems, though. The problem is that the compiler makes space for a pointer return (even for a reference) on the stack and the function creates the stack object above it; therefore, the stack unwinds past it when the function returns so the pointer goes bad. When returning by value, the return persists until ;, however.
Code:

#include <string>
#include <iostream>

std::string value() { return "something"; }

int main()
{
  const std::string *stored = NULL;
  ( stored = &value() ) && std::cout << "try 1: " << *stored << std::endl; //try 1: something
  std::cout                          << "try 2: " << *stored << std::endl; //try 2:
}

I believe this last part applies to the question OP seems to be asking. For more clarity, the return statement copies its argument to the stack space reserved for the return value.
Kevin Barry

MTK358 07-30-2011 07:23 AM

Quote:

Originally Posted by alikamu (Post 4428190)
Code:


#include<stdio.h>
int sum (int *,int *);
int main()
{
int *a,*b;
c=sum(a,b);
printf("%d\n",c);
return 0;
}
int sum (int *a, int *b)
{
//i added the value and return the sum
}

Now can you explain, that when i return the value to main, how does it work, since the memory to the pointer is given in heap and it will be destoryed as soon as the function call is over

What to the values passed as arguments have to do with the return value?

Quote:

Originally Posted by alikamu (Post 4428190)
so how is the value return and saved in c in main.

It doesn't return a pointer the the value in the stack, it creates a new copy of it that's returned to main.


All times are GMT -5. The time now is 01:17 AM.