[英]C program: when I use malloc function, I find that there are always more bytes than size of the array
Maybe the new code can explain what I mean. 也许新代码可以解释我的意思。
#include <stdio.h>
#include <stdlib.h>
int main(void){
int i_size = 2, j_size = 5, k_size = 5, l_size = 6, m_size = 6;
int i = 0, j = 0, k = 0, l = 0, m = 0;
printf("ptr1\n");
int** ptr1 = (int**) malloc(sizeof(int*) * i_size);
for(i = 0; i < i_size; i++){
printf("%d\n", ptr1 + i);
}
printf("ptr2\n");
int** ptr2 = (int**) malloc(sizeof(int*) * j_size);
for(j = 0; j < j_size; j++){
printf("%d\n", ptr2 + j);
}
printf("ptr3\n");
int** ptr3 = (int**) malloc(sizeof(int*) * k_size);
for(k = 0; k < k_size; k++){
printf("%d\n", ptr3 + k);
}
printf("ptr4\n");
int** ptr4 = (int**) malloc(sizeof(int*) * l_size);
for(l = 0; l < l_size; l++){
printf("%d\n", ptr4 + l);
}
printf("ptr5\n");
int** ptr5 = (int**) malloc(sizeof(int*) * m_size);
for(m = 0; m < m_size; m++){
printf("%d\n", ptr5 + m);
}
free(ptr1);
free(ptr2);
free(ptr3);
free(ptr4);
free(ptr5);
return 0;
}
The result is as below: 结果如下:
It is funny that it seems for malloc
function, the minimun size of array is 32 bytes. 有趣的是,对于
malloc
函数来说,数组的最小大小是32字节。 And the OS seems to preserve 8 bytes space because when I found that for l_size
, it has 64 bytes. 操作系统似乎保留了8个字节的空间,因为当我发现
l_size
,它有64个字节。 It means that if you want to malloc a 48-byte array, the OS will allocate more than 48-byte space. 这意味着如果你想要一个48字节的数组malloc,操作系统将分配超过48字节的空间。
update above 更新以上
===== =====
My code is shown as below: 我的代码如下所示:
#include <stdio.h>
#include <stdlib.h>
int main(void){
int i_size = 5, j_size = 6, i = 0, j = 0;
printf("Addresses of two dimensional pointer\n");
int** ptr = (int**) malloc(sizeof(int*) * i_size);
printf("%d:\t%d\n", &ptr, ptr);
for(i = 0; i < i_size; i++){
*(ptr + i) = (int*) malloc(sizeof(int) * j_size);
}
for(i = 0; i < i_size; i++){
for(j = 0; j < j_size; j++){
*(*(ptr+i) + j) = i * 2 + j;
}
}
for(i = 0; i < i_size; i++){
printf("%d:\t%d\n", ptr + i, *(ptr + i));
}
printf("==\n");
for(i = 0; i < i_size; i++){
for(j = 0; j < j_size; j++){
printf("%d:\t%d\t%d\n",*(ptr + i) + j, *(*(ptr + i) + j), ptr[i][j]);
}
printf("==\n");
}
for(i = 0; i < i_size; i++){
free(*(ptr + i));
}
free(ptr);
return 0;
}
And the result as the folloing picture shows: 结果如下图所示:
The code is running on the windows 10, 64-bit and compiled with TDM-GCC4.9.2 of Dev C++. 代码在Windows 10,64位上运行,并使用Dev C ++的TDM-GCC4.9.2进行编译。
I am confused by the red block. 我被红色块弄糊涂了。 Why is always 8 more bytes than the size of array.
为什么总是比数组大小多8个字节。 If I change the value of
i_size
to 6, it seems that the OS will give 64 bytes to ptr
but not 48. 如果我将
i_size
的值更改为6,则OS似乎将给ptr
64字节而不是48。
I expect that the value 12021664
should be 12021648
. 我希望值
12021664
应该是12021648
。 If I change the value of i_size
to 7, it is ok: 如果我将
i_size
的值更改为7,则可以:
But when the value of i_size
is 8, I expect the value 10710960
should be 10710944
. 但是当
i_size
值为8时,我预计值10710960
应为10710944
。 But it doesn't work. 但它不起作用。
ptr
points to an array of int *
. ptr
指向int *
的数组。 On your system, it appears that pointers are 8 bytes in size, so each member of the array is 8 bytes. 在您的系统上,指针的大小似乎是8个字节,因此数组的每个成员都是8个字节。
When you then print the address of each member as ptr + i
, you see that they each differ by 8. 然后,当您将每个成员的地址打印为
ptr + i
,您会看到它们各自相差8。
Also, you should be using %p
to print pointers instead of %d
. 此外,您应该使用
%p
来打印指针而不是%d
。 Using the wrong format specifier invokes undefined behavior . 使用错误的格式说明符调用未定义的行为 。
Regarding the specific memory addresses returned by malloc
, those are an implementation detail of the library. 关于
malloc
返回的特定内存地址,这些是库的实现细节。 There is no requirement that successive allocations should be adjacent in memory. 不要求连续分配在内存中应该相邻。 In fact, it makes sense that they are not adjacent because there is likely some metadata being stored in those in-between addresses which is being used internally by
malloc
. 实际上,它们不相邻是有意义的,因为可能有一些元数据存储在
malloc
内部使用的那些中间地址中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.