06-4 sizeof相关

本文介绍了C语言中数组的大小获取方法,包括使用sizeof计算数组长度,并探讨了字符串处理的安全性问题,如使用strncpy代替strcpy以避免缓冲区溢出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、如果在作用域内,变量以数组形式声明,则可以使用sizeof求数组大小:

int num[]={5,89,33,25,99};

int len = sizeof(num)/sizeof(int);

2、sizeof是编译器在编译的时候计算的,无法动态计算。因此对于int *或者将数组传递给函数,那么就无法使用sizeof获取大小了。即使函数声明中写着int[]也不行(为了避免误解,不要在参数中声明数组类型)。

因此一般给函数传递数组/字符串的时候都要求额外传递“长度”参数,因为函数内部也不知道“有多长”。memcpy(void * restrict, const void * restrict, size_t)

3、不是可以通过strlen获取字符串长度吗?安全性问题,strlen根据'\0'判断字符串结束,恶意攻击者会构造一个不包含'\0'的字符串,然后让数据写入数组之外的程序内存空间,从而进行破坏。strcpy→strncpy;strcat→strncat等。

sizeof2hanshu.c

#include <stdio.h>
/*数组长度有关及限制,加上函数*/

//void printEach(int *nums)    //本方式无法获得
//void printEach(int nums[])   //即使定义为数组的样子也无法通过sizeof()获得长度
void printEach(int *nums,int len)  //调用时传入一个数值为数组的字节总数才能直接运行并使用
{
	//本函数内的sizeof(nums)指的是计算指针的字节数,因为函数内的nums是一个指针
	printf("%d,%d\n",sizeof(nums),sizeof(int));
	
	int i;
	for(i=0;i<len;i++)
	{
		printf("%d|",nums[i]);
	}
	

}

int main(int argc, char *argv[])
{
	int nums[] = {11,22,33,44,55,66};
	

	printEach(nums,sizeof(nums)/sizeof(int));

	//sizeof(nums)  计算数组的字节数
	//size(int)     计算int类型的字节数
	//如果nums是当前声明范围之内的数组的名字,则sizeof(nums)就是数组的字节数(编译时完成计算)
		
	/*
	int len = sizeof(nums)/sizeof(int);
	printf("%d,%d,%d\n",len,sizeof(nums),sizeof(int));

	for(i=0;i<sizeof(nums)/sizeof(int);i++)
	{
		printf("%d|",nums[i]);
	}
	*/



	return 0;
}

ptrChar1.c

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

int main(int argc, char *argv[])
{
	char str1[] = "hello world";
	char str2[30];
	//strncpy(str2,str1,sizeof(str1)/sizeof(char));  //把str1拷贝到str2中,第三个参数是指数组长度,实际操作中需要在包含头文件string.h
	strcpy(str2,str1);                               //该方法不安全,同时联接函数strcat也不安全,因此应该调用strncpy,strncat,防止缓冲区漏洞
	printf("%d\n",strlen(str1));
	printf("%s",str2);
	
	return 0;
}


int MQTT_ReceivelPublish(char *dup, char *qos, char *retain, char *redata) { int remaining_len = 0; // 剩余长度 int topic_len = 0; // 主题长度 int cmd_len = 0; // 消息长度 int packet_id_len = 0; // 报文标志符长度 int index = 1; // MQTT固定头从第二个字节开始解析剩余长度 int multiplier = 1; int temp = 0; char tempbuff[512] = {0}; /*----------------------------------------------- 步骤 1: 提取报文标志位 -----------------------------------------------*/ *dup = (redata[0] & 0x04) >> 3; // 提取重发标志 *qos = (redata[0] & 0x06) >> 1; // 提取 QoS 等级 *retain = (redata[0] & 0x01); // 提取保留消息标志 /*----------------------------------------------- 步骤 2: 解析剩余长度(变长编码) -----------------------------------------------*/ do { temp = redata[index++]; remaining_len += (temp & 0x7f) * multiplier; // 0x7f = 127 multiplier *= 0x80; // 0x80 = 128 // 限制最大允许4字节编码 if (multiplier > 0x80 * 0x80 * 0x80 * 0x80) { printf("[ERROR] 剩余长度溢出!\r\n"); return -1; } } while ((temp & 0x80) != 0); /*----------------------------------------------- 步骤 3: 解析主题长度和内容 -----------------------------------------------*/ topic_len = redata[index] << 8 | redata[index + 1]; memcpy(tempbuff, &redata[index + 2], topic_len); mqtt_debug(tempbuff, topic_len, "\r\n* topic报文主题 *\r\n"); /*----------------------------------------------- 步骤 4: 处理报文标识符(仅QoS>0时存在) -----------------------------------------------*/ if ((redata[0] & 0x06) >> 1) { packet_id_len = 2; // QoS>0时标识符占2字节 } /*----------------------------------------------- 步骤 5: 计算有效载荷长度 -----------------------------------------------*/ cmd_len = remaining_len - (2 + topic_len) - packet_id_len; printf("\r\ncmd_len 命令长度(消息长度):%d \r\n", cmd_len); /*----------------------------------------------- 步骤 6: 提取有效载荷并处理 -----------------------------------------------*/ if (cmd_len <= 0 || cmd_len > sizeof(tempbuff)) { printf("[ERROR] 有效载荷大小无效\r\n"); return -2; } memcpy(tempbuff, &redata[index + 2 + topic_len + packet_id_len], cmd_len); MQTT_GetData(tempbuff); // 核心数据处理逻辑 mqtt_debug(tempbuff, cmd_len, "\r\n* Publish信息解码 *\r\n"); WIFI_Clean(); return 0; } 完善这个代码,支持QOS2等级
03-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值