gpt4 book ai didi

c - 为什么 sizeof(size_t) 不是 1?

转载 作者:行者123 更新时间:2023-12-02 08:38:19 28 4
gpt4 key购买 nike

我只是想知道:既然sizeof()的返回类型是size_t,为什么sizeof(size_t)给我4?也就是说,当我 malloc(someSize) 时,我请求的是 someSize 字节还是 someSize*4 字节?

我已经做了两天的 ASM 作业,现在我变得很困惑。感谢您的帮助!

最佳答案

根据 1999 ISO C 标准 (C99),size_t 是一个至少 16 位的无符号整数类型(参见 7.17 和 7.18.3 节)。

The size_t is an unsigned data type defined by several C/C++ standards, e.g. the C99 ISO/IEC 9899 standard, that is defined in stddef.h.1 It can be further imported by inclusion of stdlib.h as this file internally sub includes stddef.h.

This type is used to represent the size of an object. Library functions that take or return sizes expect them to be of type or have the return type of size_t. Further, the most frequently used compiler-based operator sizeof should evaluate to a constant value that is compatible with size_t.

这篇文章对此进行了讨论:What is size_t in C?

当您执行 malloc(sizeof(int)) 时,它会动态分配四个字节的内存。不要混淆 size_tsizeof(size_t)

关于c - 为什么 sizeof(size_t) 不是 1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19069041/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com