C结构体内char[] 数组的初始化

一种C结构体内char[] 数组的初始化:

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

int main(void){	
   //数组不指定大小或者初始化
   struct __attribute__ ((__packed__)) char_struct{
       uint32_t allocator;
       char buf[];
   };
   const unsigned int SIZE_BUFF = 100;
   const char *init = "this is my first sds(simple dynamic string).";
   unsigned int init_size = (unsigned int)strlen(init);
   
   //100个字节的char数组 + 1个'\0'
   unsigned int head_size = (unsigned int)(sizeof(struct char_struct));
   printf("sizeof struct char_struct = %d\n", head_size);
   //堆上总计需要分配的大小
   unsigned int size = (head_size)+SIZE_BUFF+1;
   void *size_head = malloc(size);
   //初始化0
   memset(size_head, 0, size);
   
   //初始化allocator
   *((uint32_t*)size_head) = size;
   
   //指针跳过allocator
   char* buf_ptr = (char*)size_head + head_size;
   memcpy(buf_ptr, init, init_size);
   //最后一个赋值'\0'
   buf_ptr[init_size] = '\0';

   //重新转成struct char_struct指针
   struct char_struct* p_struct = ((struct char_struct*)(buf_ptr - head_size));
   printf("struct char_struct's allocator=%d,buf=%s\n",(uint32_t)(p_struct->allocator),p_struct->buf);
   printf("struct char_struct's allocator=%d,buf=%s\n",(uint32_t)(*((uint32_t*)(buf_ptr - head_size))),buf_ptr);

   free(buf_ptr-head_size);//free(p_struct);
   
   
   
   //为什么__attribute__((packed))?
   /**
   
   定义的数据一样,为什么长度会不一样?

	其实是编译器在「作祟」。

	为了提高系统性能,CPU处理内存时,会用「数据对齐(Data alignment)」这种方式。
	这种方式下,数据在内存中都以固定size保存。
	而为了进行对齐,有时候就需要在数据中插入(data structure padding)一些无意义的字节。
	比如编译器是以4个bytes为单位对齐的,当你声明一个UInt8的数据,后面就会补齐3个无意义的bytes。

	参考:

	Data alignment means putting the data at a memory offset equal to some multiple of the word size, which increases the system's performance due to the way the CPU handles memory.

	To align the data, it may be necessary to insert some meaningless bytes between the end of the last data structure and the start of the next, which is data structure padding.

	更详细可参考:What is the meaning of “attribute((packed, aligned(4))) ”

	__attribute__
	而要改变编译器的对齐方式,就要利用到__attribute__关键字,
	它是用于设置函数属性(Function Attribute)、变量属性(Variable Attribute)、类型属性(Type Attribute)。
	也可以修饰结构体(struct)或共用体(union)。

	写法为__attribute__ ((attribute-list)),
	后面的attribute-list大概有6个参数值可以设置:aligned, packed, transparent_union, unused, deprecated和 may_alias(我自己没有全部试过)。

	packed
	packed属性的主要目的是让编译器更紧凑地使用内存。

	所以再回头看__attribute__((packed)),
	它的作用就是告诉编译器:取消结构体在编译过程中的优化对齐,按尽可能小的size对齐——也就是按1字节为单位对齐。

	__attribute__((packed))和__attribute__((packed, aligned(1)))是等价的。
	(aligned(x)就是告诉编译器,以x个字节为单位进行对齐,x只能是1,或2的幂)。

	现在就可以解释刚刚打印结果的不一样的原因了:
	    第一个结构体,用__attribute__((packed))取消了在编译阶段的优化对齐,返回的是实际占用字节数。
		而第二个结构体,由于优化对齐的存在,UInt8的cmd,后面会补(padding)一个byte去对齐,最后加起来就是4个byte了,后面的数据也会错乱。

    原文作者:https://www.jianshu.com/p/9c307c3eb8a9
   **/
    typedef struct __attribute__((packed)) {
		uint8_t cmd;
		uint16_t index;
	} D2MCommand;

	typedef struct {
		uint8_t cmd;
		uint16_t index;
	} D2MCommandNoAttribute;
	printf("sizeof typedef struct __attribute__((packed)) = %d\n", (unsigned int)sizeof(D2MCommand));
	printf("sizeof typedef struct = %d\n", (unsigned int)sizeof(D2MCommandNoAttribute));
	
	D2MCommand* d2MCommand = (D2MCommand*)malloc(sizeof(D2MCommand));
	d2MCommand->cmd = 100;
	d2MCommand->index = 200;
	printf("sizeof typedef struct __attribute__((packed)) sizeof= %d,\t cmd= %d,\t index=%d\n",(unsigned int)sizeof(*d2MCommand), (unsigned int)d2MCommand->cmd,(unsigned int)d2MCommand->index);
	
	D2MCommandNoAttribute* d2MCommandNoAttribute = (D2MCommandNoAttribute*)malloc(sizeof(D2MCommandNoAttribute));;
	d2MCommandNoAttribute->cmd = 100;
	d2MCommandNoAttribute->index = 200;
	printf("sizeof typedef struct sizeof= %d,\t cmd= %d,\t index=%d\n", (unsigned int)sizeof(*d2MCommandNoAttribute),(unsigned int)d2MCommandNoAttribute->cmd,(unsigned int)d2MCommandNoAttribute->index);

   return EXIT_SUCCESS;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值