The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how would you solve this problem? soemac UNIX for Advanced & Expert Users 7 03-29-2008 05:29 PM
How to solve restarting problem akzin UNIX for Advanced & Expert Users 2 06-13-2007 05:28 AM
can't solve that problem [PLEASE HELP] AiRkO UNIX for Advanced & Expert Users 2 01-22-2004 10:31 AM
How can I solve this problem? acqy High Level Programming 4 12-30-2003 07:32 PM

Closed Thread
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-15-2005
Registered User
 

Join Date: Sep 2005
Posts: 3
Thumbs up Can any one solve this Problem...!!!

Try to solve this.....It's a nice program.....

#include<stdio.h>

void change()

{

/*Write something in this function so that the output of printf in main function should give 5 . Do not change the main function */ }



void main()

{

int i=5;

change();

i=10;

printf("%d",i);

}

Please reply to this if u found the solution......

Thanks,
-baba
Forum Sponsor
  #2  
Old 09-15-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
A solution involving portable C eludes me! I naturally rejected the idea of having change() locate its return address on the stack and alter it to step over the i=10 statement. The precise location of the return address and even whether to increment it or decrement it and certainly by how much can vary from system to system. There is really no guarantee that the return address will be stored on the stack or even that there is a stack. I admit that I don't know of any currently marketed unix systems without a stack or that move the PC backwards. But I also don't know of any standard prohibiting them. These days we are in transistion from 32 bit to 64 bit architectures. Most versions of unix currently support executables in either architecture. So the change() function would need to detect the length of the return address as well.

But the other problem that I see with this involves agressive optimization. There is no indication that i is accessible to change() or is otherwise aliased or volatile. So there is no reason that the compiler writer could not legally choose to emit code that operates like...
i=10;
change();
printf("%d",i);
Whether or not that would happen would probably depend on the optimization level selected and the compiler used, and whether or not the resulting executable is intended to interact with a symbolic debugger.

So if you have a portable solution, I would love to see it!
  #3  
Old 09-15-2005
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
how about this godawful hack?
Code:
#include<stdio.h>

void change()

{
    printf("5\n");
    fflush(stdout);
    fclose(stdout);
}

int main()
{

    int i=5;    
    change();    
    i=10;    
    printf("%d",i);
    return 0;
}
And yea, I know I'm not changing the output value of i, for which no obvious even remotely portable solution occurs to me.

Last edited by jim mcnamara; 09-15-2005 at 08:01 AM.
  #4  
Old 09-15-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
Quote:
Originally Posted by jim mcnamara
how about this godawful hack?

Certainly portable!
  #5  
Old 09-21-2005
Registered User
 

Join Date: Sep 2005
Posts: 3
Red face Hi

Thanks Jim....
  #6  
Old 09-21-2005
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
this is not an acceptable solution,

since it was different
thought of sharing this...

Quote:
Originally Posted by Baba B. Saheb
#include<stdio.h>
void change()
{
printf("5\n");
exit(0);

}

void main()
{
int i=5;
change();
i=10;
printf("%d",i);
}
  #7  
Old 09-27-2005
Registered User
 

Join Date: Jan 2002
Location: India
Posts: 111
#include<stdio.h>

void change()

{
#define printf(a,b) printf(a,b/2)
}



void main()

{

int i=5;

change();

i=10;

printf("%d",i);

}
__________________
Regards,
Satya Prakash Prasad
Google The UNIX and Linux Forums
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:20 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0