(b) Memory && stack in the program

Source: Internet
Author: User

First, why does the program need to run memory? Basic concept?

Memory is the place where programs run, and the program needs to store some variables in memory.

Memory management is ultimately done by the operating system, the memory is essentially a hardware device, provided by the hardware system, memory management by the operating system, in order to manage memory convenience, the operating system provides many mechanisms to let our application use memory. These mechanisms are different from each other, each has its own characteristics, we program according to their actual situation to allocate memory (temporarily obtain the use of memory), using memory, free memory (to the operating system to return memory usage rights). The method of getting inside from C language is stack, heap, data area

Second, stack

In c, local variables are present in the stack, which is a limited amount of memory space. Stacks are automatically assigned to auto-retract, without the need for programmer intervention, convenient and simple.

Reuse: Stack memory is actually a block of fixed space, the program uses this space repeatedly. We should note that it is not possible to return the address of a local variable in a function because the memory is temporary and will be overwritten later.

  

#include <stdio.h>int *fun (void) {int a = 2;return &a;} void Fun1 (void) {int b = 5;int c = 6;} int main () {int *p;int *p1; P = Fun ();p rintf ("a =%d\n", *p); Fun1 (); FUN1 ();p rintf ("a =%d\n", *p); return 0;}

The output of this program is as follows:

We just keep in mind that the address of a function that cannot return a local variable will not make that mistake.

Dirty Memory: Memory space used repeatedly, the program will not be cleaned after each use, so the allocation of the original value, we should be aware of the definition of local variables must be initialized.

The stack will overflow: Because the stack size is fixed, if it exceeds the memory size, it will overflow.

  

#include <stdio.h>void stack_overflow (void) {    int a[10000000 ] = {0};    a[10000000-1;} int Main () {    stack_overflow ();     return 0 ;}
#include <stdio.h>void stack_overflow2 (void) {    int2;    Stack_overflow2 ();} int Main () {      stack_overflow2 ();       return 0 ;      }

Both of these codes cause a stack overflow.

(b) Memory && stack in the program

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.