Regain the dynamic memory allocation of the C language

Source: Internet
Author: User

Disadvantages of dynamic memory allocation legacy arrays: 1 array length must be predetermined and can only be a long integer cannot be a variable 2 traditional form defined array the memory program of the array cannot manually release 3 array once defined, The storage space that the system allocates for the array will persist until the function runs at the end of the 4 array length cannot be expanded or scaled up dynamically in the course of the function's operation. Why you need to dynamically allocate memory for dynamic memory allocation memory examples-- Dynamic array construction static memory allocation and dynamic memory allocation problems using memory across functions #include<stdio.h>void f (void) {int a[5]={0,1,2,3,4}//cannot be released manually to release the system after the function has run out}int Main () {f ();} #incliude <stdio.h> #include <malloc.h>int main () {int i=5;/* static allocation *//* allocate eight bytes P store first address number */int *p= (int *) Malloic (4);/* the second int * cast to int *//* represents four bytes but only the first address represents */free (p),//free (p) indicates that the memory that the P points to is released//p itself is not manually released by the programmer}/* The only thing that malloc can do is to return to the first address. The memory occupied by */p itself is statically allocated  p is the dynamic of the memory referred to
The malloc function learns dynamic memory allocation #include<stdio.h>void f (int *q) {*q=200; if this is write free (p), then the meaningless}int main () {int *p= (int*) in the main function later malloc (sizeof (int.)) *p=10;printf ("%d\n", *p), F (P);/*free (P); * * This is also wrong because memory printf has been freed ("%d\n", *p); } Zhan Storage structure heap allocation theater payees  


Regain the dynamic memory allocation of the C language

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.