sizeof与strlen.md

本文深入解析C/C++中sizeof运算符与strlen函数的区别,通过代码示例阐述它们在处理数据类型大小与字符串长度上的不同作用。sizeof用于计算类型或变量所占内存大小,而strlen用于测量以''结尾的字符串的实际长度。

**sizeof与strlen的区别: **

下面以代码的形式来解释
#include <iostream>
using namespace std;
int main()
{
	cout << sizeof(int) <<"\t"<<sizeof(double)<< endl;
	
	int x = 3;
	cout << sizeof(x) << endl;

	//cout << strlen(4) << endl;	//error: strlen只能以\0结尾的参数作为参数
	cout << strlen("abc123d") << endl;	// 系统会自动加个'\0'作为结束符

	int arr[] = { 1,2,3 };
	cout << sizeof(arr) << endl;	// 数组作为sizeof参数时,不退化
	//cout << strlen(arr) << endl;	// error:数组作为strlen的参数时,退化为指针,指针是一种普通的变量,从访问上不异与其他变量。
	//它保存的数值是整型类型,和整型数据不同的是,它指向的是一段内存地址。所以不是以‘\0’结束的字符串,所以报错

	/*

	两者区别:
	sizeof是一个操作数,而strlen是库函数
	sizeof的参数可以是数据的类型也可以是变量,而strlen的参数只能是以'\0'为结尾的字符串
	编译器在编译时期就计算出了sizeof的结果,而strlen函数必须在运行时才能计算出来
	sizeof计算的是类型所占内存的大小,而strlen计算的是字符串实际的长度
	数组作sizeof的参数不退化,而传递给strlen作参数就退化为指针了

	*/

	return 0;
}
int create_upgrade_file(const char* output_path, const char* part_names[], const char* part_files[], int part_count) { // 变量声明 FILE* in_fp = NULL; FILE* out_fp = NULL; uint8_t* data = NULL; MTD_UP__HEAD head; size_t total_size = 0; size_t padding_size = 0; size_t block_size = 2048; char *tmpaddr = NULL; const char* ext = NULL; char curtype[16] = {0}; int filesize = 0; int tmpsize = 0; int curmethod = 0; unsigned char md5_digest[16] = {'\0'}; // 函数主体 out_fp = fopen(output_path, "wb"); if (!out_fp) return -1; for (int i = 0; i < part_count; i++) { // printf("i:%d\n",i); in_fp = fopen(part_files[i], "rb"); // type 判断 ext = strrchr(part_files[i], '.'); curmethod = INT64_MIN; if (ext != NULL) { ext++; // 跳过点符号 // 后缀名匹配判断 if (strcmp(ext, "elf") == 0 || strcmp(ext, "mbn") == 0 || strcmp(ext, "bin") == 0) { snprintf(curtype, sizeof(curtype), "MTD"); curmethod = 1; } else if (strcmp(ext, "ubi") == 0) { snprintf(curtype, sizeof(curtype), "UBI"); curmethod = 0; } else { // 未知后缀处理 snprintf(curtype, sizeof(curtype), "UNKNOW"); curmethod = -1; } } else { // 无后缀名处理 snprintf(curtype, sizeof(curtype), "NO_EXT"); curmethod = -2; } if (!in_fp) { fclose(out_fp); return -1; } fseek(in_fp, 0, SEEK_END); int filesize = ftell(in_fp); rewind(in_fp); padding_size = (block_size - (filesize % block_size)) % block_size; total_size = filesize + padding_size; data = malloc(total_size); if (!data) { fclose(in_fp); fclose(out_fp); return -1; } if (fread(data, 1, filesize, in_fp) != filesize) { free(data); fclose(in_fp); fclose(out_fp); return -1; } fclose(in_fp); in_fp = NULL; memset(data + filesize, 0xFF, padding_size); memset(&head, 0, sizeof(head)); strncpy(head.name, part_names[i], sizeof(head.name) - 1); head.size = total_size; strncpy(head.type, curtype, sizeof(head.type) - 1); head.UPmethod = curmethod; // memcpy(head.md5, md5, sizeof(head.md5)); printf("name:%s;\n size:%u;\n type:%s;\n UPmethod:%d;\n",head.name,head.size,head.type,head.UPmethod); tmpaddr = (char *)malloc(16 + total_size); memcpy(tmpaddr, md5Key, 16); memcpy(tmpaddr + 16, data, total_size); tmpsize = 16 + total_size; md5_make_digest(md5_digest, (unsigned char *)(tmpaddr), tmpsize); printf("MD5:\n"); for (int i = 0; i < 16; i++) { if(i == 15){printf("%0x- \n", md5_digest[i]);} // 输出十六进制 else{printf("%0x- ", md5_digest[i]);} } memcpy(head.md5, md5_digest, 16); if (fwrite(&head, sizeof(head), 1, out_fp) != 1) { free(data); fclose(out_fp); return -1; } if (fwrite(data, 1, total_size, out_fp) != total_size) { free(data); fclose(out_fp); return -1; } free(data); data = NULL; free(tmpaddr); tmpaddr = NULL; } fclose(out_fp); return 0; } 我说的是上述函数
11-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值