gpt4 book ai didi

c - 在 malloc 中进行类型转换

转载 作者:太空宇宙 更新时间:2023-11-04 01:01:53 25 4
gpt4 key购买 nike

<分区>

我知道这个:Do I cast the result of malloc?

我看了,还有其他的问题,还是没有解我的顾虑。我知道类型转换会隐式发生,但是无论我读了多少这些论点,错误 都是我不明白的东西。

主要论据是intpointer 可能有不同的大小。举个例子,设 int* 的大小为 8,int 的大小为 4。

int *x = (int*) malloc(sizeof(int));

首先,malloc() 分配的字节数是 4,尽管变量 x 将存储一个大小为 8 字节的指针。

我明确地将 malloc 的返回值强制转换为 (int *),它⟩— 顺便说一句⟩—⟩的大小为 8。这里怎么会丢失任何位?

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