Home » Interview Questions » Interviews » Story Details
Printable Version

Interview Question: Output of C-language code

by Kamal Rawat on Jan 09, 2012

What will be the output of this given C code. (returning reference of local variables from function)
Comments: 0    Views: 769

MCN Professionals | Interview Question of the day

MCN Professionals is starting Industrial Training for MCA-2012 Batch. Have a look at our Industrial Training Program and Course Details.
-----------------------------------------------------------------------------------------------------------------------------------------

Today's Question:

What will be the output of the below code ?
int* myFunc()
{
     int x = 2;
     return &x;
}
main()
{
     int* p = myFunc();
     printf("%d", *p);
}
Note that the answer if not 2 :).
Solution:
Before jumping to the result, lets understand how functions are called and how local variables are allocated memory.

When a function is called, an activation record of the function is created on the Stack. Local variables (non-static only) in a function are allocated memory on this Stack (Activation record has much more information than just local variables). When control is returned from the function, this Activation record is deleted.

The first Activation record is created for main function. when main calls myFunc(), the picture of activation record will look something like this.
Activation Record.jpg
Note that Activation record has much more information (like return address etc.) but we have kept only local variables in Activation record for simplicity reason. Let the address of x be 100 and its value be 2. When myFunc will return to main, It will return the address of x (i.e 100) but once the control reaches main function, the AR (Activation Record) for myFunc will be deallocated and its memory will be returned to compiler.
Activation Record_2.jpg
Now if we want to access the value at location 100, we cannot say that it will always be 2. Note that the value may come 2 while you run the program, but this is only because the compiler has not reused this memory in any way (hence this memory still contains 2).. but that is not the standard case.

Hence, the value is not defined, and p is a dangling pointer (pointer which is pointing to a memory not allocated to our program).

So, the thumb rule is, Never return address or reference of local variable from a function.

---------------------------------------------------------------------------

Interview Questions Archive:

         To see all the questions in the category click here...


Post a Comment
*
DevExpress PowerBuilder Web Development Windows Development Languages Software Engineering Databases
iPhone Architecture Secutiry UML & Modeling Operating Systems Networking Testing
Graphics Design Project Management Hardware Open Source Games Development Business Intelligence Visual Studio LightSwitch 2011
MonoDevelop Visual Studio 2010 ASP.NET HTML, DHTML XML PHP JavaScript
Silverlight Web Services WCF Windows Forms WPF Windows Services Dynamic Link Libraries
ActiveX COM, DCOM, ATL C# VB.NET C++ F# Java
Pascal SQL Server Oracle DB2 MS-Access Windows Servers Windows
Linux Unix SAP LINQ .NET Framework ADO.NET Reporting
Crystal Reports SQL Server Reporting Services Igenda Reports Active Reports Adobe Fireworks Arrays & Collections Hosting
Future Trends Android Windows Phone Smart Devices Business M&A Investment & Funding
Web Browsers Internet Explorer Firefox Safari Common Entrepreneurs Students
Consulting Wiki Gadgets MobileMe iCloud iOS Social Media
Facebook Twitter LinkedIn Google+ Microsoft Kinect XBox
Wii Playstation DirectX i OS OS X CIO, CTO, CEO Windows 8
Web Design Expression Blend 4 Photoshop CS5 Creative Suite 5.5 Expression Web 4 Expression Studio 4 Creative Suite® 5.5 Design
Creative Suite 5.5 Web Creative Suite 5.5 Production Startups Funding M&A Laptops Smart Phones
Desktops Cameras & Camcorders Netbooks Tablets Virtualization Microsoft Surface WordPress
Software Products Cloud Computing Current Affairs Technology TV TV
Earnings XAML E-Commerce MonoTouch Mono for Android Deals Electronics
Mobile Phone Laptop Tablet Book Computer Press Releases Reviews
Products Books Companies Windows Azure SQL Azure Interviews Mac
Web Browsers Symbian Windows Forms WPF Windows Services HTML 5 Office 365
SharePoint 2010 Exchange Server Adobe Visual Studio 2012 iPad Flex / Flash Games
Windows 9
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName