C语言库函数---malloc

备注:C语言malloc申请的内存,不能用sizeof来计算,但是数组可以

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct some_info{
    char name[20];
    char sex[20];
    char like[20];
}some_info;

typedef struct test
{
    char *name;
    char *age;
    char *sex;
}test;
void get_info(struct test *);
void clean_up(struct test *);

void main()
{
    int i = 0;
    char *name = NULL;
    char (*names)[20] = NULL;   
    struct some_info *my_info = NULL;    
    struct test ty_info[10] = {NULL};

    while(1){
	/*一维数组使用malloc*/
//    name = (char *)malloc(2000*sizeof(char));
	/*二维数组使用malloc*/
//    names = (char (*)[20])malloc(2000*20*sizeof(char)); 
	/*一维结构使用malloc*/  
//    my_info = (struct some_info *)malloc(2000*sizeof(struct some_info)); //记得判断内存是否申请成功
//    sscanf("wangdan man study","%s%s%s",my_info->name,my_info->sex,my_info->like);
	/*二维结构使用malloc*/
//    get_info(&ty_info[0]);
//	clean_up(&ty_info[0]);
      
 //   free(names);  //记得设置names为null
 //   free(name);
 //   free(my_info);
//	free(ty_info);
    }
}
//二维结构用来申请内存
void get_info(struct test *ptd)
{
    ptd->name = (char *)malloc(20*sizeof(char));
    ptd->age = (char *)malloc(20*sizeof(char));
    ptd->sex = (char *)malloc(20*sizeof(char));
}
//二维结构用来清理内存
void clean_up(struct test *ptd)
{
    free(ptd->name);
    free(ptd->age);
    free(ptd->sex);
}

下面是一个二维结构数组的例子:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct ceph_osd_info
{
    char *dev;
    char *capacity;
    char *local;
    char *rev;
    char *node_id;
    char *status;
    char *vendor;
    char *sn;
}ceph_osd_info;

void get_infos(struct ceph_osd_info *ptr)
{
    ptr->dev=(char *)malloc(128*sizeof(char));
    ptr->capacity=(char *)malloc(128*sizeof(char));
    ptr->local=(char *)malloc(128*sizeof(char));
    ptr->rev=(char *)malloc(128*sizeof(char));
    
    ptr->node_id=(char *)malloc(128*sizeof(char));
    ptr->status=(char *)malloc(128*sizeof(char));
    ptr->vendor=(char *)malloc(128*sizeof(char));
    ptr->sn=(char *)malloc(128*sizeof(char));
}

void clean_up(struct ceph_osd_info *ptr)
{
    free(ptr->dev);
    free(ptr->capacity);
    free(ptr->local);
    free(ptr->rev);
    
    free(ptr->node_id);
    free(ptr->status);
    free(ptr->vendor);
    free(ptr->sn);
}

int main()
{
    FILE *pp = NULL;
    int info_num = 0;
    int i = 0;
    char cmd[512] = {0};
    char buff[512] = {0};
    
    struct ceph_osd_info osd_infos[12] = {NULL};
    
    sprintf(cmd,"bcli disk list|grep ^s");
    if((pp = popen(cmd,"r"))){
        while(fgets(buff,512,pp)){
            get_infos(&osd_infos[info_num]);
            sscanf(buff,"%s %s %s %s %s %s %s %s",\
                osd_infos[info_num].dev,osd_infos[info_num].capacity,osd_infos[info_num].local,\
                    osd_infos[info_num].rev,osd_infos[info_num].node_id,osd_infos[info_num].status,osd_infos[info_num].vendor,osd_infos[info_num].sn);
            info_num++;
        }
    }
    
    for(i=0; i<info_num; i++){
        printf("dev:%s\n",osd_infos[i].dev);
        printf("capacity:%s\n",osd_infos[i].capacity);
        printf("local:%s\n",osd_infos[i].local);
        printf("rev:%s\n",osd_infos[i].rev);
        printf("node_id:%s\n",osd_infos[i].node_id);
        printf("status:%s\n",osd_infos[i].status);
        printf("vendor:%s\n",osd_infos[i].vendor);
        printf("sn:%s\n\n",osd_infos[i].sn);
        clean_up(&osd_infos[i]);
    }
}
发布了62 篇原创文章 · 获赞 6 · 访问量 6895

猜你喜欢

转载自blog.csdn.net/qq_23929673/article/details/95607522