473,385 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Why such output?

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.

Jan 9 '06 #1
4 1241
"SinC" <ch*********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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!!!!!
Jan 9 '06 #2

"SinC" <ch*********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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
Jan 9 '06 #3
"Dick de Boer" <ddeboer - dsp.com> writes:
"SinC" <ch*********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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.


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 9 '06 #4

"SinC" <ch*********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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
Jan 10 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark Wilson CPU | last post by:
This must be easy, but I'm missing something... I want to execute a Perl script, and capture ALL its output into a PHP variable. Here are my 2 files: -------------------------------------...
3
by: edgekaos | last post by:
Is method 2 valid? Method 1: wstring input = L"STRING"; wstring output = input; transform(output.begin(), output.end(), output.begin(), towupper); Method 2: wstring input = L"STRING";...
4
by: Kevin Mansel via .NET 247 | last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that...
24
by: kalamantina | last post by:
#include "stdafx.h" #include <stdio.h> #define output( x ) printf( #x "\r\n" );fflush( stdout ) class CMyBase { public: CMyBase() { output( CMyBase() ); f(*this);
0
by: newbie | last post by:
i'm a newbie of c language. can anyone help me to implement the code so that I can get the ciphertext from the output. thanks. #ifndef _3DES_H #define _3DES_H #ifndef uint8 #define uint8 ...
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
3
by: MatsL | last post by:
Hi, This is seriously driving me crazy, could anyone explain to me why neither of these doesn't produce XHTML compliant output (it is being called in Render() btw): output.WriteLine("<img...
3
by: undshan | last post by:
I am writing a code that needs to open a file, create an output file, run through my function, prints the results to the output file, and closes them within a loop. Here is my code: #include...
5
by: amit.uttam | last post by:
Hey everyone, I've recently jumped big time into python and I'm working on a software program for testing automation. I had a question about proper logging of output. What I would like is: 1....
2
by: gabosom | last post by:
Hi! I've been breaking my head trying to get the output variables from my Stored Procedure. This is my SP code CREATE PROCEDURE GetKitchenOrderDetail( @idService int, --outPut Variables ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.