0

Consider the following code:

void foo(){
   .....
}
int main()
{
    int arr[3][3] ;
    char string[10];
    foo();
    return 0;
}

How can the function foo access the locals of my main function without passing the parameters to the function as function arguments ? Does the function foo have enough privileges to access and modify the variables in main ? please reply thank you

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Lokesh Dogga
  • 65
  • 1
  • 7
  • 5
    Possible duplicate of [Can a local variable's memory be accessed outside its scope?](http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) – Stargateur Jan 02 '17 at 03:24
  • 3
    That's what parameters are for. – stark Jan 02 '17 at 03:24

1 Answers1

1

As per the C language specification, a function's local variables are inaccessible to other functions. There is no legal, supported way to do what you're asking. That said, in most (all?) implementations of the C language the main function's variables will be stored on the stack, which is easy to locate and can be read and written by anyone (it has to be because everyone needs to store local information in it) so it is technically possible (though it is a remarkably bad idea).

void foo(){
    int b; // puts a 4 byte word on the stack atop the return address
    (&b)[2]; // interpret b as the first entry in an array of integers (called the stack)
    // and offset past b and the return address to get to a
    // for completeness
    (&b)[0]; // gets b
    (&b)[1]; // gets the return address
}
int main()
{
    int a; // puts a 4 byte word on the stack
    foo(); // puts a (sometimes 4 byte) return address on the stack atop a
    return 0;
}

This code, might, on some systems (like 32 bit x86 systems) access the variables inside of the main function, but it is very easily broken (e.g. if pointers on this system are 8 bytes, if there's padding on the stack, if stack canaries are in use, if there are multiple variables in each function and the compiler has its own ideas about what order they should be in, etc., this code won't work as expected). So don't use it, use parameters because there's no reason not to do so, and they work.

Irisshpunk
  • 758
  • 5
  • 8
  • Really extreme as an answer. Moreover, in a function having parameters, those parameters are actually local variables of the function, but probably they reside in registers - not the stack. So this answer is really bad, even if _sometimes_ it can be "formally correct". – linuxfan says Reinstate Monica Jan 02 '17 at 08:17
  • Do you mean the code sample is really bad? I agree, but I felt that there was an opportunity here to shed some light on how certain things work "under the hood" (rather than just clearing up something about the C language itself) and felt that it was worth the risk that the asker might jump right past the "don't ever do this warning" and make use of this nonsense code. If you have suggestions on how to make it more clear that I don't endorse this behavior, I'm all ears. – Irisshpunk Jan 02 '17 at 20:27
  • No, it is not the code, and correctly you also said "don't use it". It is the whole idea that, by my taste, is not suited for this answer. I did, years ago, exactly what you describe here, and it worked. But I was exactly aware of the compiler implementation, I also used assembler, it was a very specific case (an implementation of multitasking). The context was very precise; instead you replied to someone asking "has foo() enough privileges..."... which makes me think he is beginner enough to not be molested by difficult internals. Just that. – linuxfan says Reinstate Monica Jan 03 '17 at 07:43