Why such output?

S

SinC

Here is the program code:

typedef void (*T)(void);

void id(void)
{
int z;
(*(((T *)(&z))+ 2))();
cout<<"y";
}


void Magic(void)
{
cout<<"magic\n";
id();
cout<<"x\n";
}


int main(void)
{
Magic();
cout<<"a\n";
return 0;
}

The output of this program is (g++ compiler) :
magic
x
x
a

Can somebody tell me, what is going on here in this whole code?
Thanks.
 
P

pemo

SinC said:
Here is the program code:

typedef void (*T)(void);

void id(void)
{
int z;
(*(((T *)(&z)) + 2))();
cout<<"y";
}


void Magic(void)
{
cout<<"magic\n";
id();
cout<<"x\n";
}


int main(void)
{
Magic();
cout<<"a\n";
return 0;
}

The output of this program is (g++ compiler) :
magic
x
x
a

Can somebody tell me, what is going on here in this whole code?


Firstly, this is C++, and c.l.c is a group dedicated to C. So, you should
ask your question on, say, comp.lang.c++.

Secondly, the code is, um, rather suspect!

The bit I assume you're interested in is?
int z;
(*(((T *)(&z))+ 2))();

This appears to be taking the address of an unitialised int (address of
frame variable!), casting that to be a function pointer, adding 2 to it,
calling the function pointed to!!!!

I doubt whether this is portable!!!!!
 
D

Dick de Boer

SinC said:
Here is the program code:

Can somebody tell me, what is going on here in this whole code?
Thanks.
z in function ID is allocated on stack. The return adres pushed when calling
id() is also on stack. It seems that &z + 2 adrresses the reurun adres of
the calling function. Here, character x is written. After that the result
depends strongly on implementation of the stackframe.

I hope for you this isn't a serious program....
DickB
 
K

Keith Thompson

Dick de Boer said:
z in function ID is allocated on stack. The return adres pushed when calling
id() is also on stack. It seems that &z + 2 adrresses the reurun adres of
the calling function. Here, character x is written. After that the result
depends strongly on implementation of the stackframe.

It depends strongly on the implementation long before that. There's
no guarantee that there's such a thing as a "stack" or a "stackframe",
or that the return address (if any) is stored somewhere in it.
 
M

Mike Wahler

SinC said:
Here is the program code:

typedef void (*T)(void);

void id(void)
{
int z;
(*(((T *)(&z))+ 2))();
cout<<"y";
}


void Magic(void)
{
cout<<"magic\n";
id();
cout<<"x\n";
}


int main(void)
{
Magic();
cout<<"a\n";
return 0;
}

The output of this program is (g++ compiler) :
magic
x
x
a

Can somebody tell me, what is going on here in this whole code?

What output did you expect and why?

-Mike
 

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

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,477
Latest member
ColumbusMa

Latest Threads

Top