tags:

views:

165

answers:

5

Hi,

I want to access variable v1 & v2 in Func() while being in main()

int main(void)
{
   Func();
   int k = ? //How to access variable 'v1' which is in Func()
   int j = ? //How to access variable 'v2' which is in Func()
}

void Func()
{
    int v1  = 10;
    int v2  = 20;
}

I have heard that we can access from Stack. But how to do.

Thank you.

+8  A: 

You can't legally do that. Automatic variables disappear once execution leaves the scope they're declared in.

I'm sure there are tricks, like inspecting the stack and going "backwards" in time, but all such tricks are platform-dependent, and might break if you, for instance, cause the stack to be overwritten in main().

unwind
+3  A: 

Some ways you can do this are:

  1. Declare the variables in main() and pass them by pointer or reference into Func()
  2. Return the variable, or vector< int >, or a struct that you made, etc. of the variables to main()
  3. Dynamically allocate the variables in Func(), and return a pointer to them. You would then have to remember to delete the allocated memory later as well.

But there is no access to the stack of Func() from main() that is standard.

DeadHead
+3  A: 

Why do you want to do that? Do you want those values as return values? I would introduce a struct for that, according to the meaning of the values the struct would get a suitable name

struct DivideResult {
  int div;
  int rem;
};

DivideResult Func() {
  DivideResult r = { 10, 20 };
  return r;
}

int main() {
  DivideResult r = Func();
}

Otherwise, such variables are for managing local state in the function while it is activated. They don't have any meaning or life anymore after the function terminated.

Johannes Schaub - litb
+2  A: 

You can't do that portably. When Func()'s stack frame disappears, there's no reliable way to access it. It's free to be trampled. However, in x86-64, there is something known as the red zone, which is a 128B area below the stack pointer that is safe from trampling, and theoretically you might still be able to access it, but this is not portable, easy, nor correct. Simply put, don't do it.

Here's how I would do it:

int main(void)
{
   int k, j;
   Func(&k, &j);
}

void Func(int *a, int *b)
{
    *a  = 10;
    *b  = 20;
}
Mike Mu
+1  A: 

You're in C/C++ land. There are little you cannot do.

If this your own code, you shouldn't even try to do that. Like others suggested: pass a output parameter by reference (or by pointer in C) or return the values in a struct.

However, since you asked the question, I assume you are attempting to look into something you only have binary access to. If it is just an one time thing, using a debugger will be easier.

Anyway, to answer your original question, try the following code. You have to compile it in for x86 CPU, with optimization and any stack debug flag turned off.

void f() {
    int i = 12345;
    int j = 54321;
}

int main()
{
    int* pa = 0;
    int buf[16] = {0};

    f();

    // get the stack pointer
    __asm {
     mov dword ptr [pa],ESP
    }

    // copy the stack, try not to do anything that "use" the stack
    // before here    
    for (int i = 0; i < 16; ++i, --pa) {
     buf[i] = *pa;
    }

    // print out the stack, assuming what you want to see
    // are aligned at sizeof(int)
    for (int i = 0; i < 16; ++i) {
     std::cout << i << ":" << buf[i] << std::endl;
    }

    return 0;
}
Shing Yip
In C/C++ land, there's a lot you can't do, including this. What you mean is, "you are in native code land, and you can screw around with implementation details, so there's little you can't do" ;)
jalf
Shing,awesome like it. you the only one who understand my need ;)
mahesh