Talking about why the C language program needs memory stack?

Source: Internet
Author: User

1 1. About memory (program execution requires memory support)2(1the memory itself is physically a hardware device, provided by the operating system3(2the management of the memory is ultimately managed by the operating System. In order to be able to manage the memory conveniently (the Hotel management room is not divided into many different types and treatment), the same operating system provides a variety of mechanisms to let our application use Memory. These mechanisms are different from each other, each with their own characteristics, our program according to their actual situation to choose a way to obtain memory (in the operating system to register the temporary use of this memory), the use of memory, free memory (to the operating system to return the use of this memory). That is, in the hotel to get the key or card, and then you can enter a room numbered xx, that is, you have temporary permission to use such as a night, the next day when you return the key or card, that is, the release of Memory. 4 2. There are three ways in which the C language can acquire Memory:5 Stack Data area6 3. About Stacks7 spec1:8 auto-allocate and Auto-recycle when running, programmers don't have to intervene manually9 Spec2: repeated useTen There is a space program in the stack that uses this space repeatedly. one spec3: Dirty Memory a Why is dirty, repeated use, even if it is not to clean up, the operating system will not help you to care, which has big brother to you to cook and wash the Dishes. So the allocation to the time to retain the original value, so ah, the variable definition is best to initialize, or you do not know what the situation - spec4: Temporary (function cannot return a pointer to a stack variable, because this space is temporary, notice that, many times we want the address of the variable, take it out to point to it with the pointer, and then manipulate it, but after this, once the other function is also in the stack, it is possible to replace the space that just now, This will not get the ideal Result) - Once the program has been executed, you have achieved your goal, and you can go and give it to other people to use this space. look, how Humane. the  -#include <stdio.h> -  -  + //The function cannot return the address of the local variable inside the function because the local variable is no longer available after the function has finished executing. - //This local variable is allocated on the stack, although it is not, but the stack memory is still accessible, but the access is actually the + //the memory address has nothing to do with that variable at the Time.  a int*func (void) at { -     intA =4;//A is a local variable, allocated on the stack is also called a stack variable, also known as a temporary variable -printf"&a =%p\n", &a); -     return&a; - } -  in voidFunc2 (void) - { to     intA = -; +     intb = -; -     intc = -; theprintf"in func2, &a =%p\n", &a); * } $ Panax Notoginseng voidStack_overflow (void) - { the     inta[10000000] = {0}; +a[10000000-1] = a; a } the  + voidStack_overflow2 (void) - { $     intA =2; $ Stack_overflow2 (); - } -  the  - intMainvoid)Wuyi { the     //Stack_overflow (); - Stack_overflow2 (); wu /* - int *p = NULL; about p = Func (); $ Func2 (); - Func2 (); - printf ("p =%p\n", p); -      a printf ("*p =%d.\n", *p); Prove the stack is dirty after it's done. + */     the     return 0; -}

Study notes, hehe, feel good. Do not spray ~ ~

Talking about why the C language program needs memory stack?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.