是固定的int字节尺寸,或者它在C/C++中相应地占据?

发布于 2025-01-25 21:33:35 字数 248 浏览 2 评论 0原文

我已经看到了一些使用int而不是其他类型的程序/code>

让我举一个示例,当您将9分配给int时,我知道9仅需1个字节即可存储,所以其他3个字节免费使用还是被占用?

我要说的是,int始终在内存中需要4个字,或者相应地采用字节,而4个bytes是最大尺寸,

我希望您理解我在说什么。

I have seen some program that use int instead of other type like int16_t or uint8_t even though there is no need to use int

let me give an example, when you assign 9 to an int, i know that 9 takes only 1 byte to store, so is other 3 bytes free to use or are they occupied?

all i'm saying is, does int always takes 4-bytes in memory or it takes byte accordingly and 4-bytes is the max-size

i hope you understand what im saying.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

回忆凄美了谁 2025-02-01 21:33:35

所有类型的大小都是恒定的。您在整数中存储的值对类型的大小没有影响。如果您存储一个由单个字节表示的较小的正值小于最大值,则更重要的字节(如果有)将包含零值。

int的大小不一定是4个字节。整数类型的字节大小已定义。

The size of all types is constant. The value that you store in an integer has no effect on the size of the type. If you store a positive value smaller than maximum value representable by a single byte, then the more significant bytes (if any) will contain a zero value.

The size of int is not necessarily 4 bytes. The byte size of integer types is implementation defined.

维持三分热 2025-02-01 21:33:35

我已经看到了一些使用int而不是其他类型的程序,例如int16_t或uint8_t,即使不需要使用int

有时也称为“ sloppy键入”。 int的缺点是它的大小是实现定义的,因此它不可移植。从理论上讲,它甚至可以使用异国情调的签名格式(至少直到C23标准)。

当您将9分配给INT时,我知道9只需1个字节

不正确,并且任何地方都没有免费字节。给定一些代码int x = 9;,那么整数常数9是类型int,并占用与一个相同的空间,除非编译器决定将其优化为较小的类型。 9存储在只读内存中,通常与.text段中的可执行代码一起存储。

变量x完全采用sizef(int) bytes(32位系统上的4个字节),无论存储什么值。除了在可能完全删除变量时,编译器无法对大小进行任何明智的优化。

I have seen some program that use int instead of other type like int16_t or uint8_t even though there is no need to use int

This is sometimes called "sloppy typing". int has the drawback that its size is implementation-defined, so it isn't portable. It can in theory even use an exotic signedness format (at least until the C23 standard).

when you assign 9 to an int, i know that 9 takes only 1 byte to store

That is not correct and there are no free bytes anywhere. Given some code int x = 9; then the integer constant 9 is of type int and takes up as much space as one, unless the compiler decided to optimize it into a smaller type. The 9 is stored in read-only memory, typically together with the executable code in the .text segment.

The variable x takes exactly sizeof(int) bytes (4 bytes on 32 bit systems) no matter the value stored. The compiler cannot do any sensible optimization regarding the size, other than when it is possible to remove the variable completely.

潇烟暮雨 2025-02-01 21:33:35

类型的大小在编译时固定。没有“动态调整大小”。如果您告诉编译器使用int,它将使用保证具有至少16位宽度的整数类型。但是,它可能(并且大多数时候)取决于您使用的平台和编译器。您可以使用sizeof(int)在平台上查询字节宽度。

关于不同整数类型的宽度有一个整洁的概述>。

int16_tuint8_t不是核心语言类型,而是便利性库定义的类型,如果需要确切的位宽,可以使用这些类型(例如,对于位算术)

The size of types is fixed at compile time. There is no "dynamic resizing". If you tell the compiler to use int it will use an integer type that is guaranteed to have at least 16bit width. However, it may be (and is most of the time) more depending on the platform and compiler you are using. You can query the byte width on your platform by using sizeof(int).

There is a neat overview about the width of the different integer types at cppreference.

The int16_t or uint8_t are not core language types but convenience library defined types, that can be used if an exact bitwidth is required (e.g. for bitwise arithmetic)

心意如水 2025-02-01 21:33:35

int没有“免费字节”。 int至少16位宽,确切的大小取决于目标平台(请参见 https://en.cppreference.com/w/cpp/language/types )。 sizeof(int)是编译时间常数。无论其具有什么价值,它总是占据相同数量的字节。

固定宽度整数类型( https://en.cppreference.com/w/w/w/cpp/类型/整数)对于假定一定大小整数的代码很有用,因为假设int的一定大小通常是一个错误。 int16_t正好是16位宽,uint8_t正好是8位宽,独立于目标平台。

An int has no "free bytes". An int is at least 16 bits wide and the exact size depends on the target platform (see https://en.cppreference.com/w/cpp/language/types). sizeof(int) is a compile time constant though. It always occupies the same number of bytes, no matter what value it holds.

The fixed width integer types (https://en.cppreference.com/w/cpp/types/integer) are useful for code that assumes a certain size of integers, because assuming certain size of int is usually a bug. int16_t is exactly 16 bits wide and uint8_t is exactly 8 bits wide, independent of the target platform.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文