1 Star 0 Fork 0

xiaoyi971520/MEMManagement

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.c 1.20 KB
一键复制 编辑 原始数据 按行查看 历史
xiaoyi971520 提交于 2020-01-05 23:15 . c 内存管理模块
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "mem.h"
typedef struct _buff_st_1
{
int data_len;
char data[0];
}buff_st_1;
typedef struct _buff_st_2
{
int data_len;
char *data;
}buff_st_2;
typedef struct _buff_st_3
{
int data_len;
char data[];
}buff_st_3;
int main()
{
int n1 = sizeof(buff_st_1);
int n2 = sizeof(buff_st_2);
int n3 = sizeof(buff_st_3);
buff_st_1 *buff1 = (buff_st_1 *)malloc(sizeof(buff_st_1)+100);
buff_st_2 *buff2 = (buff_st_2 *)malloc(sizeof(buff_st_2));
buff_st_3 *buff3 = (buff_st_3 *)malloc(sizeof(buff_st_3)+50);
int n = show_memory();
strncpy(buff1->data,"hello",100);
printf("buff1 address:%p,buff1.data_len address:%p,buff1.data address:%p,value:%s\n",
buff1, &(buff1->data_len), buff1->data,buff1->data);
buff2->data = (char*)malloc(10);
printf("buff2 address:%p,buff2.data_len address:%p,buff2.data address:%p\n",
buff2, &(buff2->data_len), buff2->data);
strncpy(buff3->data,"world",100);
printf("buff3 address:%p,buff3.data_len address:%p,buff3.data address:%p,value:%s\n",
buff3, &(buff3->data_len), buff3->data,buff3->data);
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xiaoyi971520/MEMManagement.git
git@gitee.com:xiaoyi971520/MEMManagement.git
xiaoyi971520
MEMManagement
MEMManagement
master

搜索帮助