gpt4 book ai didi

c - 为什么 malloc() 在使用指向 `double` 的指针时没有分配正确大小的内存?

转载 作者:行者123 更新时间:2023-11-30 20:50:52 26 4
gpt4 key购买 nike

这是我想做的事情的本质:

double *E; //Array of doubles
int N; //The eventual size of the array, typically >1

// some code where variable N gets assigned

//inside of some function
E = malloc(sizeof(double)*N);

printf("size of E = %lu\n",sizeof(E)/sizeof(E[0])); //checking size of array E

无论 N 的实际值如何,此代码的输出都是“E 的大小 = 1”。为什么 malloc() 函数没有在内存中分配正确的大小?

我知道这看起来很初级,但我不明白为什么这行不通。

任何见解将不胜感激。

最佳答案

您实际上是将指针的大小除以 double 的大小。两者都需要相同数量的字节(通常为 8 个)来存储。因此你得到 1。

关于c - 为什么 malloc() 在使用指向 `double` 的指针时没有分配正确大小的内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37912231/

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