473,386 Members | 1,883 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,386 software developers and data experts.

Why are we not allowed to return the address of the local variable from a function ?

Expand|Select|Wrap|Line Numbers
  1. int *g(void) 
  2. int x = 10; 
  3. return (&x); 
  4.  
what's the problem in returning the address of the local variable ?so even if its address of dead variable then still that memory location exists so it shuld give garbage value,


I am unable to get this one that how another function which uses the same local variable gets the same stack frame where the previous function foo resided ?

why is the op 57 here ?

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h> 
  2. #include <stdlib.h> 
  3. int * foo(){ 
  4. int a = 5; 
  5. return &a; 
  6. void boo(){ 
  7. int a = 7;
  8.  
  9. void boo1(){ 
  10. int a = 57;
  11.  
  12. int main(){ 
  13. int * p = foo(); 
  14. boo(); 
  15. boo1(); 
  16. printf("%dn",*p); 
  17. }
Aug 22 '15 #1
1 1581
weaknessforcats
9,208 Expert Mod 8TB
This is not true:

I am unable to get this one that how another function which uses the same local variable gets the same stack frame where the previous function foo resided ?

Each function has its own stack frame. The stack frame exists until the function completes. So in your example, when function g returns, the stack frame for foo no longer exists. That means the local variable a no longer exists and if you return the address of a you provide your calling function with an address that maybe is invalid.

Unfortunately, the stack frame for a called function might actually exist for a while after the function completes. The calling function might be able to use the local variable briefly. This leads to a run time crash of heap corruption that is very difficult to debug.

In the case of a calling function passing the address of a local variable to a called function, that is OK. That's because the stack frame for the calling function will exist the entire time the called function is executing. However, this is done by using called function arguments.
Aug 22 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Nafai | last post by:
Hello I would like you guys to give me the best solution for this problem: class bigInteger { private: vector<char> digits; public: ... bigInteger& operator+=(const bigInteger& x)
2
by: Chris Stankevitz | last post by:
Hi, I have some weird behavior in a very large (500,000 line) program that I boiled down to a few lines: TCSensorMode::ProcessMeasurement() { InternalComplicatedFunction(1.0, 2.0); int a =...
4
by: octavio | last post by:
Hello members of the comp.lang.c newsgroup. Please I need you help on the following one. Compiling the simple code I'm getting this error message. Why ? Please what's the correct type of the fb...
25
by: Sourav | last post by:
Suppose I have a code like this, #include <stdio.h> int *p; void foo(int); int main(void){ foo(3); printf("%p %d\n",p,*p);
5
by: FireHead | last post by:
Hello All, Its hard to explain but here it goes: char &free_malloc(char* object_copy){ Here ------------------------------------------------------------------ object_copy is parametre were...
14
by: Spitfire | last post by:
Hi All, I've this weird question about pointers. I would like to know how to return the address of a local variable, safely!! Isn't that a unrecommended procedure? Doesn't it have possibilities...
3
by: sam_cit | last post by:
Hi Everyone, I have the following function, char* sample1() { char *p = "india"; return(p); }
13
by: hari | last post by:
Hi all, Is it legal to return a local variable from function. Regards Hari
7
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; }...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.