address of return value

I

igor.lautar

Hi,

How can I get the address of return value in function?

ex.

int function(int argument)
{
int *arg_address;
int *rtn_address;

arg_address = &argument;
rtn_address = ??;

.
.
.
}

This would be helpfull to print return value automatically (when object
is destroyed).

Thx,
Igor
 
Y

ytfrdfiw

may be,you can do as following:

int ss(int i)
{
int *k = &i;
int *j = &i;
return (int)j; //this is a cast,becase j is a type of
int*
}
 
Y

ytfrdfiw

maybe,you can as following:
int ss(int i)
{
int *k = &i;
int *j = &i;
return (int)j; //this is a cast;

}
 
A

Alan Johnson

Hi,

How can I get the address of return value in function?

It is not necessarily the case that the return value even has an
address. For sufficiently small types your compiler may choose to place
the return value in a register.

Alan
 
B

Bernd Strieder

Hello,

How can I get the address of return value in function?


Plain and simple answer, you can't. The location of the return value is
completely in the hands of the compiler, whether register or stack or
whatever. There are no syntactical means for influencing the return
value location or taking its address.
This would be helpfull to print return value automatically (when
object is destroyed).

I don't understand this, maybe you could provide a more complete
example, what you want to achieve, especially what it has to do with
destruction of an object.


Bernd
 
G

Guest

Getting an address of return value in function, why?

I think it's illegal, just silly.

The variable has been beyond its scope, but you want to use it by its'
pointer.

If i were you , I would never do that.
 
M

Maxim Yegorushkin

Hi,

How can I get the address of return value in function?

Make the function take the address to store the return value into:

void f(int* result)
{
// here results is the address of the return value
*result = -1;
}
 
I

igor.lautar

Well, it would be usefull for tracing. Destructor of object would print
return value automatically (when object goes out of scope - at the
return from function).

You can always do:
int f(int a)
{
int ret = 0;
int *ret_addr = &ret;
.
.
.
return ret;
}

To simulate 'getting the address of return value', but then you have to
be carefull to always use this approach. Also, providing additional
argument has the same extra work.

Thx,
 
D

dost

igor.....
do..u want to print..the address...of
variable...or....exact...locataion......of...return...value..location
 
I

igor.lautar

I want to print return value automatically, without doing cout on every
return. This could be done if one has address of return variable or
address of temporary variable (above example), which is always set to
return value before actually returning. Then create an temporary
object, which prints this value at destruction.

Hope its clearer now. Should have give more info to start with... My
bad.

Regards,
Igor
 
I

igor.lautar

I want to print return value automatically, without doing cout on every
return. This could be done if one has address of return variable or
address of temporary variable (above example), which is always set to
return value before actually returning. Then create an temporary
object, which prints this value at destruction.

Hope its clearer now. Should have give more info to start with... My
bad.

Regards,
Igor
 
M

Michiel.Salters

I want to print return value automatically, without doing cout on every
return. This could be done if one has address of return variable or
address of temporary variable (above example), which is always set to
return value before actually returning.

How about a suitable #define RETURN?

As an added bonus, you can make it print only in debug mode.

HTH,
Michiel Salters
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top