C Advanced 1 (heap, stack, static area, code area)

Source: Internet
Author: User

First, four areas of memory 1. Stack

Advanced Post-out

stack size fixed, default 1M, can be compiled when set, out of overflow

When the variable leaves the scope, the data on the stack is automatically released

Stack is continuous, upward growth

#include <stdio.h>#include<stdlib.h>voidgo ();voidMain () {void*P1 = malloc (Ten);//on the P1,P2 stack    void*P2 = malloc ( -); //00a0f914, 00a0f908printf"%p,%p", &AMP;P1, &AMP;P2);//The address is on the stack and grows upward//00a63cd8, 00a67a50printf"\n%p,%p", p1, p2);//heap area, growth downwardGetChar ();}

The stacking order of function parameters is right-to-left

    int a=4;     // the order of the parameter stacks is from right to left    printf ("%d,%d", A, ++a); // 5,5

2. Heap

Heap on memory needs manual release manually, by program control, malloc application, free release

The heap is linked, grows downward, and continues to be accessed faster than the link

3. How do I choose a stack or a heap?

Whether the data occupies a clear memory size

Manual or Automatic Management

4. Static Zone

Store global variables/constants, static variables/constants

Global variables

Global variables and function declarations can have multiple

Can be used across files

static variables

With the program, the compile time is initialized, will only initialize once

Can only be used in the current C file

5. Code Area

program code directives, constant strings, readable only

Second, the implementation of the stack with arrays
#include <stdio.h>#include<stdlib.h>//determine if the stack is emptyintIsEmpty ();//set stack to emptyvoidsetempty ();//into the stackintPushintnum);//out of the stackintpop ();#defineN 5structstack{intTop//Top of Stack    intData[n];//Storing Data};//initialization of the stackstructStack Mystack = {-1, {0 } };voidMain () {intnum[5] = {1,2,3,4,5};  for(inti =0; I <5; i++) {push (num[i]); }     while(!IsEmpty ()) {printf ("%d", Pop ()); } getchar ();}//determine if the stack is emptyintIsEmpty () {if(Mystack.top! =-1)    {        return 0;//0 means not empty    }    return 1;//1 means empty}//set stack to emptyvoidSetempty () {if(IsEmpty ()) {Mystack.top= -1; }}//into the stackintPushintnum) {    if(Mystack.top = = N-1)    {        return-1; }    Else{mystack.top+=1; Mystack.data[mystack.top]=num; return 1; }}//out of the stackintpop () {intnum=-1; if(!IsEmpty ()) {num=Mystack.data[mystack.top]; Mystack.top-=1; }    returnnum;}
Recursive judging whether the array is decreasing the way one
intIsdec (intNum[],intLen) {    if(len==1)    {        return 1; }    Else    {         if(num[len-1]<num[len-2])        {            returnIsdec (num, Len-1); }        Else        {            return-1; }            }}

  

Way Two
N must be 0
int isdec (int num[],int len,int n) { if (n==len-2 { return1]; } Else { return1]) && isdec (num,len,n+1);} }

C Advanced 1 (heap, stack, static area, code area)

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.