开发者

Find address of variables in main? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

Recently while surfing some C++ blogs, I came across a smal开发者_C百科l C teaser program in one of them.

#include<stdio.h>

int find_addr()
{
/*fill your code here*/
}

int main()
{
int i,j;
clrscr();
find_addr();
return 0;
}

The question is to find the address of variables i & j without touching the main function. I haven't been able to figure it out yet. Feels awful that I couldn't even solve this minor question :((.

EDIT:

The above program had lot of non-standard statements like the use of inclusion of conio.h anbd other non standard headers and its functions , getch() and other statements, I edited it in a hurry and forgot to omit void from void main(), apologies for that.

EDIT2: I have given my one vote to close this thread since I perceive through the responses posted here that there are non-standard issues related to the question.


I think I found where you read the puzzles. Most of the programs use typeless main(), or worse, void main(). They assume a lot of system- and/or compiler-specific things. The programs on the page are not very good quality, and make for a bad tutorial. Please stay away from it.

For example, this is the first program:

what is the output? Definitely the output is not what you think! so think more..

main()
{
    int i = 300;
    char *ptr = &i;
    *++ptr = 2;
    printf("%d",i);
    getch();
}

Third program:

what is the output of the following code if array name starts with 65486?

void main()
{
    int num[] = {10,11,12,13};
    printf("%u %u",num,&num);
}

I could go on, but there is no need really. As I said, stay away from this page!


I think it could look like the following, but it is not conformant way and you shouldn't use it in practice.

int find_addr()
{
  int t;
  int* i_addr = &t - <some platform&compiler&whatever specific constant>;
  int* j_addr = &t - <some platform&compiler&whatever specific constant>;
}

The idea is that i and j are placed on the stack and you could find address of stack by using address of one more variable on the stack.

You should note that sometimes it is impossible to find addresses of i and j because compiler will not allocate memory for them because of optimization. This once again confirms the fact that you should not try to write such code.


On windows this can be done using the _AddressOfReturnAddress intrinsic. This function gives you the address on the stack which contains the return address. the address of i,j would be a constant negative offset from that address.

Notice that any such "solution" is highly non portable and would probably fail in many cases. one such case is if the compiler decides for some reason to make i and j registers. The compiler may decide to do this since you never explicitly take the address of i or j so as far as it is concerned, its safe. In this case i,j don't actually have addresses so you're going to get the address of something else on the stack and writing on it is very likely to crash your program.


I found this works in VC compiler..

the function name takes 2 int32 in the stack, then comes the definition of a.

int find_addr()
{

int a;
printf("&a = %u &i = %u & j = %u\n", &a, &a+4, &a+3);

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜