zoukankan      html  css  js  c++  java
  • 【转】动态申请内存空间

     
    1、动态申请一维内存
    
    (1)、用malloc函数
    
    #include <stdio.h>
    #include <string.h>
    #include <malloc.h>
    
    int main()
    {
        int n=5;
        int  *temp;
        temp=(int*)malloc(sizeof(int)*n);
        for(int i=0;i<n;i++)
        {
                temp[i]=i;
        }
        for(i=0;i<n;i++)
        {
            printf("%d ",temp[i]);
        }    
        free(temp);
        return 0;
    }
    
    
    new
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int n=5;
        int  *temp;
        temp=new int[n];
        for(int i=0;i<n;i++)
        {
                temp[i]=i;
        }
        for(i=0;i<n;i++)
        {
            printf("%d ",temp[i]);
        }    
        delete [] temp;
        return 0;
    }
    2、动态申请二维内存
    
    (1)、用malloc函数
    #include <stdio.h>
    #include <string.h>
    #include <malloc.h>
    
    int main()
    {
        int n=5; //n表示行
        int  **temp;
        int m=2; //m为列
        temp=(int**)malloc(sizeof(int *)*n);
        for(int k=0;k<n;k++)
            temp[k]=(int *)malloc(sizeof(int)*m);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
                temp[i][j]=i*m+j;
        }
        for(i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
                printf("%d ",temp[i][j]);
            printf("
    ");
        }    
        free(temp);
        return 0;
    }
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
        int n=5; //n表示行
        int  **temp;
        int m=2; //m为列
        temp=new int*[n];
        for(int k=0;k<n;k++)
        {
            temp[k]=new int[m];
        }
    
            for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
                temp[i][j]=i*m+j;
        }
        for(i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
                printf("%d ",temp[i][j]);
            printf("
    ");
        }    
        delete [] temp;
    
        return 0;
    }








    1
    .标准3部曲:malloc + free +指针置空 2.编程实例 /* date:20100824 description:malloc使用规范探讨 in参数:申请的堆内存字节数,注意int,short,float需要自己乘上相应字节数。 out返回值:void * */ main() { char *str=NULL; str=(char *)malloc(10); //注意malloc返回值是void *,申请时需要强制转换成需要的类型 memset(str,0,10); //如果不清空,申请的区域值是随机的,养成好习惯 strcpy(str,"happylife"); //使用strcpy特别注意拷贝的字符串长度<=10-1,即要预留字符串结束标志'' puts(str); free(str); printf("str[address]:%s[%x] ",str,str); //这里的str内容为空,但是指针地址还在 str=NULL; //注意指针free之后该指针仍然存在,最好将它指向为空。 printf("str[address]:%s[%x] ",str,str); //这里的str内容为空,地址也为空 }
     
    3. malloc()工作机制
    
      malloc函数的实质体现在,它有一个将可用的内存块连接为一个长长的列表的所谓空闲链表。调用malloc函数时,它沿连接表寻找一个大到足以满足用户请求所需要的内存块。然后,将该内存块一分为二(一块的大小与用户请求的大小相等,另一块的大小就是剩下的字节)。接下来,将分配给用户的那块内存传给用户,并将剩下的那块(如果有的话)返回到连接表上。调用free函数时,它将用户释放的内存块连接到空闲链上。到最后,空闲链会被切成很多的小内存片段,如果这时用户申请一个大的内存片段,那么空闲链上可能没有可以满足用户要求的片段了。于是,malloc函数请求延时,并开始在空闲链上翻箱倒柜地检查各内存片段,对它们进行整理,将相邻的小空闲块合并成较大的内存块。
  • 相关阅读:
    如何修改配置文件:CentOS下SSH端口修改
    linux ssh_config和sshd_config配置文件学习
    linux文件权限命令chmod学习
    硬盘接口类型介绍
    Linux中权限(r、w、x)对于目录与文件的意义
    谈谈对虚拟DOM的理解
    对于深入响应式原理的深刻理解
    环套树 or 基环树 找环
    POI 2014 little bird
    洛谷P2876 [USACO07JAN]解决问题Problem Solving
  • 原文地址:https://www.cnblogs.com/Lee-geeker/p/3232382.html
Copyright © 2011-2022 走看看