Question: Can a local variable's memory be accessed Air Max 2011 Womens Purple Black Grey outside its scope have a memory for some local variables, it can be accessed from outside the scope of what?? The following code: int * foo () {int a = 5; return \u0026 amp; a;} int main () {int * p = foo (); cout \u0026 lt; \u0026 lt; * p; * p = 8; cout \u0026 lt; \u0026 lt ; * p;} This code can be executed properly, and no run-time exception! Output is 58 how this is going? Does a local variable outside the function can also be accessed by it? Best answer (3200+ Chan) from Microsoft senior software engineer Eric Lippert: You rented a room in a hotel. You put a book into the drawer of the table 597806-400 Nike LeBron X EXT QS Denim-Pink Outlet first, and then went to bed. When you wake up the next morning, you pretend to forget to also keys. You stole the keys to the room! A week later, you go back to the hotel, but did not stay, you use stolen keys slipped into the last time you stay in the room, and view the drawer. Your book is still there. Is not it amazing! This is how it happened? Is a person drawer hotel room should not and can not be a no-show in this room see it? Well, obviously, this situation will certainly happen in the real world. When you do not stay in this room, there is no mystical power to get your books disappear, there is no magic Air Max 2011 Womens Purple Grey can prevent you from using a stolen key to enter the Nike Free Run 3 Women room. The hotel's management has no regulatory requirement to take your book. You did not tell them that if you drop a book, they can help you tear it. If you use a stolen key illegally entered the last time you room, and was not found hotel security system. You did not tell them if you later try to sneak into the room, they should stop you. But the fact is, you do requires you to sign an agreement to ensure that no sneaked back room. But you broke the agreement. In this case, anything can happen. If you are lucky, the book may still be there. Other people may also be in the book that drawer and your book were thrown into the fire in the hotel. It may also be time to go when you slip into just someone in your book to shreds. The hotel may be jointly and severally to that table you have removed the book, and put a wardrobe in there. This hotel might just want to be removed, and replaced with a football field, you glided time. You might die in a blasting. When you leave the hotel and stole the keys to the room, you do not know what will happen. You give up to live in a reliable and secure world, because you choose to break the rules of the system. C ++ is not a safe language. You can very easily broke the rules of the system. If you try to do something illegal and stupid things like that you have not you go back to stay in the room, and maybe you want to view goes on the table does not already exist. C ++ does not stop you. Safer than C ++ language by limiting your ability to solve this problem, such as through more stringent New Nike Free 5.0 V4 Grey Blue Running Shoes control room key. [Update]: My God. The answer received so much attention. (I do not know why, I just think this analogy is interesting, but who cares.) I think that after a more technical answer thinking about this update is necessary. The compiler's job is to manage the program generates code data have memory. There are many ways to generate memory management code, but over the years there are two basic technology is the need to know. The first is to have a long-standing area, this storage area where every byte, their life cycle is relatively long. Life cycle, which means that they can be accessed by the program period. Such memory can not be estimated in advance. The compiler generates a code called the heap manager that knows how to dynamically allocate memory, when the memory is no longer needed to release them out when needed. The second is to have a short-lived area, this storage area where each byte can be estimated in advance. And more special is that the life cycle of this region follows a nested pattern. In other words, has the longest life cycle variable in this area, it is allocated memory address was assigned to reuse it after a short life cycle of those variables. 597806-400 Nike LeBron X EXT QS Denim-Pink Outlet Local variable is the second case. When you call a function, its local variables will be generated. When this function calls another function, the new function of local variables are also generated. These variables are local variables before the first function is released. The start and end memory address local variables can be calculated in advance. For this reason, local variables are often assigned to the stack data structure, because a stack feature is the first element of the stack will be the last one out of the stack. It is as if the hotel can only be decided in accordance with the order of rent rooms. You Air Jordan Heel can not leave unless you room number before all people older than you are gone. So, let's think about the operation of Nike Jordan Melo B Mo the stack. In many operating systems, each thread has a stack, and the stack size is a variable sized. When you call a function, the relevant content is pushed onto the Air Max 2011 Womens Purple Grey stack. When you put a stack pointer that came out this function, as above questioner are doing the same. The pointer is pointing to the middle of all the valid millions of bytes of memory blocks. In our analogy, when you leave the hotel, you just leave the number of currently occupied the largest room. If no one after you check, you have illegally returned to the room. All your stuff definitely still room in this hotel. We use the stack as a temporary storage because they are inexpensive and easy Air Jordan Outlet to implement. C ++ does not provide for implementation must use the stack to store local variables, you can use the stack to store them, but no one to do this, because to do so would make the program very slow. C ++ there is no provision in the stack 2015 Nike Free 5.0 need to be cleared away after you leave the contents of the stack, so you can find your content before you return to the stack after illegally. Of course, if the compiler generates code, once you no longer use to put everything in the stack is cleared, it is perfectly legal. You do not need to explain why, because the cost of doing so is very high. C ++ does not require to be sure that when the stack variable hours, prior to the effective memory address is still valid. C ++ implementations also allow tell the operating system, 'we no longer need to stack two memory pages. Unless I say otherwise when there is anyone who wants to visit this before the effective stack memory page when throws an exception and ends the program.' ʱ?? New Nike Free 5.0 V4 Grey Blue Running Shoes Again, the general implementations do not, because to say and there is no need to make the program slower. On the contrary, most of the time, the general C ++ implementation allows you to make mistakes and then avoid it. Until one day, some really very scary error appeared then get the whole program crashes. This is a problem. C ++, there are so many rules but they can be broken so easily. I had this experience many times. Worse, the problem is often superficial, when you find the memory address conflict to check memory, but found them in a very long time is correct. So it's hard to know in the end is what went wrong. Nike Free 5.0 V2 Women Those memory-safe language by limiting your ability to solve this problem. In the standard C # where there is no way to get the memory address of a local variable, and then return it, or store it so after then. You can get Nike Air Max the memory address of a local variable, but the language is well Nike Dunk Heels designed, you can not also be able to use it after a local variable life cycle. In order to obtain the memory address of a local variable and return it, you have to put the compiler is set to a special unsafe mode, and write your program on 'unsafe' keyword. This can help remind you that you're doing something unsafe things might be breaking the rules. Further reading: C # returns a reference when done? http://blogs.msdn.com/b/ericlippert/archive/2011/06/23/ref-returns-and-ref-locals.aspx Why do we use a stack to manage memory? Type C # in value are stored in the stack has been? How virtual memory works? And more about C # memory management is how it works. Many articles are here to help C ++ programmers. http://blogs.msdn.com/b/ericlippert/archive/tags/memory+management/ Scopehotel rooms and C ++ local variables