提问人:binary_assemble 提问时间:9/14/2023 更新时间:9/26/2023 访问量:13
为什么 sizeof char* 分配了 malloc 返回 8?
Why sizeof of char* allocated with malloc return 8?
问:
我正在用 g++ 编译。
我有这个代码:
char* charP = (char*) malloc(1);
我将其理解为分配 1 个字节。
当我做一个它的大小时,它显示 8。printf
charP
printf("size of charP is %lu \n", sizeof(charP));
为什么是 8 而不是 1?
答:
1赞
Tom
9/26/2023
#1
sizeof(charP) 返回指针的字节化,而不是此指针引用的缓冲区的字节化。由于您使用的是 64 位计算机,因此 charP 的大小为 8 个字节。
获取缓冲区长度的最好方法是记住它。(你知道你分配了 1 个字节)
评论