-3

I got some code- I want to see how the particular variables are placed onto stack.

#include <iostream>
using namespace std;

int fun(char b, long c, char d){
short p,q,r;
int y;
/***return &b,&c,&d,&p,&q,&r,&y;***/ - this one was for purpose of what should be returned, it is not valid code}

int main(){
fun('a',123,'b');
return 0;
}

Expected result would be addresses of particular variables, that's why I used operand &. But still, I don't know where to place it in my code correctly, i.e. MAIN function.

REMARK: function does literally NOTHING, it is just an exercise for computer architecture course purpose.

Marta
  • 37
  • 10
  • 2
    Why not print them? – NathanOliver Jun 11 '19 at 15:18
  • 2
    note that you pass the parameters by value, so once you mangaged to print addresses in `fun` it will be the adresses of the local variables, not of those in `main` – 463035818_is_not_an_ai Jun 11 '19 at 15:18
  • You can't return more than one thing at a time. – HolyBlackCat Jun 11 '19 at 15:21
  • I know how the RETURN works @HolyBlackCat, it it commented for the purpose. – Marta Jun 11 '19 at 15:22
  • `return &b,&c,&d,&p,&q,&r,&y;` evaluates the addresses of 6 things and then discards them, with only the 7th impacting the result. I don't think there is an implicit conversion from `int*` to `int`, so `fun` is also ill-formed – Caleth Jun 11 '19 at 15:22
  • Ok, my question is then-> how to call the FUN in MAIN function? To have addresses – Marta Jun 11 '19 at 15:24
  • You could aggregate aggregate all of the stuff you need to return into a structure and then return the structure. – user4581301 Jun 11 '19 at 15:29
  • @Marta It sounds like you are asking how to return many different objects from a single function. Is this correct? – François Andrieux Jun 11 '19 at 15:29
  • your question is still unclear. You seem to know how to get the addresses, this is done by using the `&` operator. but what now? Do you want to keep main as is and `fun` is supposed to print the address of passed parameters to screen? Or do you maybe want `fun` to get the addresses and return them to `main` so you can print them in `main`? – 463035818_is_not_an_ai Jun 11 '19 at 15:33

2 Answers2

3

If you want to capture the addresses within fun to see where they are on the stack (or whereever they are in memory), AND you want to return all those addresses to main, you can use this:

#include <iostream>
#include <map>
using namespace std;

map<const char*, void*> fun(char b, long c, char d) {
  short p, q, r;
  int y;
  return {
    { "b", &b },
    { "c", &c },
    { "d", &d },
    { "p", &p },
    { "q", &q },
    { "r", &r },
    { "y", &y },
    { "b", &b }
  };
}

int main() {
  auto results = fun ('a', 123, 'b');
  for (auto p: results) {
    printf("%s is at %p\n", p.first, p.second);
  }
}

For me it displayed

b is at 0x7ffdec704a24
c is at 0x7ffdec704a18
d is at 0x7ffdec704a20
p is at 0x7ffdec704a36
q is at 0x7ffdec704a38
r is at 0x7ffdec704a3a
y is at 0x7ffdec704a3c

Remember that as others have pointed out, you cannot use these addresses in main! It would be much better, IMHO, to just do the printf invocations within fun itself. But no worries! Hopefully this helps.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
0
#include <iostream>
using namespace std;

int fun(char b, long c, char d){
short p,q,r;
int y;
cout<<(void*)&b<<endl<<&c<<endl<<(void*)&d<<endl;
cout<<&p<<endl<<&q<<endl<<&r<<endl<<&y<<endl;}


int main()
{
fun('a',123,'b');
return 0;}

Ok, I got it now. This is the solution.

Marta
  • 37
  • 10