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

时间:2020-12-17 21:18:11

This question already has an answer here:

这个问题在这里已有答案:

Here is the essence of what I am trying to do:

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

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

The output of this code is "size of E = 1", regardless of the actual value of N. Why does the malloc() function not allocate the correct size in memory?

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

I know this seems very rudimentary, but I cannot understand why this would not work.

我知道这似乎很简陋,但我不明白为什么这不起作用。

Any insight would be greatly appreciated.

任何见解将不胜感激。

4 个解决方案

#1


2  

You are essentially dividing the size of the pointer by the size of a double. Both take the same amount of bytes (8 typically) to store. therefore you get 1.

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

#2


1  

E's type is pointer to double. E's value is an address. When you get the sizeof(E), you are getting the size of the variable that is the pointer rather than the size of what is being pointed to by the variable. In order to get the size of what malloc allocated, you need to dereference the pointer in sizeof so your last line becomes :

E的类型是指向double的指针。 E的值是一个地址。当你得到sizeof(E)时,你得到的是指针变量的大小,而不是变量指向的大小。为了获得malloc分配的大小,你需要在sizeof中取消引用指针,这样你的最后一行变为:

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

EDIT

There is no difference between *E and E[0] as pointed out by one of the comments. In C, there is no way of you knowing where you array ends because the array itself as a datatype does not store its length. This is partly why the vector datatype in C++ was necessary.

正如其中一条评论所指出的,* E和E [0]之间没有区别。在C中,您无法知道数组的结束位置,因为数组本身作为数据类型不会存储其长度。这就是为什么C ++中的向量数据类型是必要的原因。

Also, since you already stored the length of the array in N, you might as well just print N. When you pass the array to any functions, pass the array along with the length. With strings, you can get away with iterating over the character array till you get '\0', the null terminating character. For integer and floating point datatype arrays, there is no such convention.

此外,由于您已经将数组的长度存储在N中,因此您也可以只打印N.当您将数组传递给任何函数时,将数组与长度一起传递。使用字符串,您可以通过迭代字符数组直到获得'\ 0'(空终止字符)。对于整数和浮点数据类型数组,没有这样的约定。

#3


0  

malloc is allocating a right size array, else your test is wrong.

malloc正在分配一个正确大小的数组,否则你的测试是错误的。

sizeof(E) is the size of the pointer = 8

sizeof(E)是指针的大小= 8

sizeof(*E) is the size of the first double = 8

sizeof(* E)是第一个double = 8的大小

you can know the size of your array multiplying sizeof(*E) * N

你可以知道你的数组的大小乘以sizeof(* E)* N.

#4


0  

To put another spin on this explanation, if sizeof(E) worked as you expected (giving the size of the allocated memory, rather than the size of the pointer) then you could use that to get the size of dynamically allocated arrays. You can't do that in C.

为了解释这个问题,如果sizeof(E)按预期工作(给出分配内存的大小,而不是指针的大小),那么你可以用它来获得动态分配数组的大小。你不能用C做到这一点。

#1


2  

You are essentially dividing the size of the pointer by the size of a double. Both take the same amount of bytes (8 typically) to store. therefore you get 1.

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

#2


1  

E's type is pointer to double. E's value is an address. When you get the sizeof(E), you are getting the size of the variable that is the pointer rather than the size of what is being pointed to by the variable. In order to get the size of what malloc allocated, you need to dereference the pointer in sizeof so your last line becomes :

E的类型是指向double的指针。 E的值是一个地址。当你得到sizeof(E)时,你得到的是指针变量的大小,而不是变量指向的大小。为了获得malloc分配的大小,你需要在sizeof中取消引用指针,这样你的最后一行变为:

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

EDIT

There is no difference between *E and E[0] as pointed out by one of the comments. In C, there is no way of you knowing where you array ends because the array itself as a datatype does not store its length. This is partly why the vector datatype in C++ was necessary.

正如其中一条评论所指出的,* E和E [0]之间没有区别。在C中,您无法知道数组的结束位置,因为数组本身作为数据类型不会存储其长度。这就是为什么C ++中的向量数据类型是必要的原因。

Also, since you already stored the length of the array in N, you might as well just print N. When you pass the array to any functions, pass the array along with the length. With strings, you can get away with iterating over the character array till you get '\0', the null terminating character. For integer and floating point datatype arrays, there is no such convention.

此外,由于您已经将数组的长度存储在N中,因此您也可以只打印N.当您将数组传递给任何函数时,将数组与长度一起传递。使用字符串,您可以通过迭代字符数组直到获得'\ 0'(空终止字符)。对于整数和浮点数据类型数组,没有这样的约定。

#3


0  

malloc is allocating a right size array, else your test is wrong.

malloc正在分配一个正确大小的数组,否则你的测试是错误的。

sizeof(E) is the size of the pointer = 8

sizeof(E)是指针的大小= 8

sizeof(*E) is the size of the first double = 8

sizeof(* E)是第一个double = 8的大小

you can know the size of your array multiplying sizeof(*E) * N

你可以知道你的数组的大小乘以sizeof(* E)* N.

#4


0  

To put another spin on this explanation, if sizeof(E) worked as you expected (giving the size of the allocated memory, rather than the size of the pointer) then you could use that to get the size of dynamically allocated arrays. You can't do that in C.

为了解释这个问题,如果sizeof(E)按预期工作(给出分配内存的大小,而不是指针的大小),那么你可以用它来获得动态分配数组的大小。你不能用C做到这一点。