iOS Learning Notes---C language Day

Source: Internet
Author: User

Dynamic memory allocation

First, storage area division

From low to high (memory address small----memory address Large): Code area---constant area---static area---heap area---stack area

Stack memory

//variables defined in the function body are stored in the stack area (including formal parameters).     intA =Ten;//A is also inside the stack area    floatb =1.5;//B is also inside the stack area .    Charc ='b'; DoubleD =3.14; printf ("%p%p%p%p\n",&a,&b,&c,&d); //Stack features advanced after the first defined variables first into the stack, after the definition of the variable into the stack. //when the function is called. The variables in the function will go into the stack in succession, and at the end of the function call, the variables are stacked//the allocation and recovery of memory in the stack area is automatically carried out by the system. 

Constant area

' A ' character constants 5 integer constant "iphone" string constant

Constant-occupied memory read-only state cannot be modified

Static Storage Area

static int a = 5;

1. Initialize only once 2. If the value is not initially given, the default is 0 3. Only the program exits (always exists)

The variable is stored in a static storage area before the type defined by the variable is added static

    // compile time has entered the memory, the program is running H has been there, will not be destroyed  , the program ended H will be destroyed, if not assigned to H, h default initial value is 0    Static int ten;

#import <Foundation/Foundation.h>int; // variables defined outside the function are called global variables, and global variables, like static variables, are stored in the global (also called static) zone. int Main (intconstChar * argv[]) {

Two, heap memory allocation function

Heap memory is typically allocated and freed by programmers

Malloc:void *malloc (unsigned int size);//void * represents any type of pointer

int * p = (int *) malloc (sizeof (int) * n);//(int *) strong-to-type sizeof (int) allocates memory size *n number of elements

Char *str = malloc (8);

    // The main function of malloc is to open a specified size of memory    space from the heap area // memory size in bytes     // The return value is the    first position of this space // we need to define a pointer to hold the return value    int *p = malloc (4);     // p is in the stack area. 10 heap area of malloc operation, p memory address is the heap area address    int;     int *q = &A;     // A in the stack area, Q is also in the stack area, q the address of a, that is, the address of the stack area

strcpy (str, "IPhone");

Free (str);//Mark Delete, do not clear contents

 struct   student{ char  name[20  ];         int   age;         int   num;             char   sex;    };    typedef  struct   student student; Student  *pstu = malloc (sizeof  (Student) * );    Pstu ->age = 20   //  

Student *p = malloc (sizeof (Student));//Allocate memory size with sizeof (Student)

void * CALLOC (unsigned n,unsigned size)//n memory size size calloc empty memory

Allocates n size space and zeros all bytes in that memory

void *realloc (void *p,unsigned newSize);//redistribution by new length

void *memset (void *s, int c, size_t n);//memset (p, 0, sizeof (int) * 5) is typically used to clear struct or array data

    //Enter the names of three trainees, dynamically allocate memory to save the college name, and finally output//defines an array of 3-character pointers, which by default point to null (0)    Char*names[3]={0}; //defines a temporary array for storing the individual words entered    Chartemp[ -] = {0};  for(inti =0; i<3; i++) {scanf ("%s", temp);//do not add & in front of temp, because the array name itself is the address//get the length of a single wordUnsignedLongLength =strlen (temp); //let the pointer point to the open heap spaceNames[i] = malloc (length+1); //copy the input into the heap spacestrcpy (names[i], temp); }     for(inti =0; i<3; i++) {printf ("%lu%s\n", strlen (Names[i]), names[i]); free (names[i]);
}

void *memcpy (void *dest,const void *source,size_t n)

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.