简体   繁体   English

C程序:当我使用malloc函数时,我发现总是有更多的字节而不是数组的大小

[英]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_size的值更改为6的结果

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,则可以: 将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. 但它不起作用。

将i_size的值更改为8的结果

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.

相关问题 我如何正确处理C中的malloc失败,尤其是当有多个malloc时? - How can I correctly handle malloc failure in C, especially when there is more than one malloc? 在 C 中,当使用 malloc 创建二维数组时,为什么在传递给函数时不需要指定二维数组大小? - In C why do I NOT need to specify 2D array size when passing into function when the 2D array is created with malloc? 为什么我不能使用malloc设置大于所需大小的数组? - Why can't I use malloc to set array size larger than what is needed? 我的malloc函数分配的次数超出了我的预期 - Is my malloc function allocating more than I intend to C.使用的内存多于malloc中分配的内存-我可以返回sth,结束程序而不是使其崩溃吗? - C. Using more memory than allocated in malloc - can I return sth, end program instead of letting it crash? C 程序显示的字符数超过数组大小 - C program displaying more characters than array size C:malloc似乎分配更多然后我请求(数组) - C : malloc seems to allocate more then i'm requesting (array) 当我知道char数组的最大大小时使用malloc - using malloc when I know the maximum size of the char array 我可以根据C标准分配超过65535个字节吗? - Can I allocate more than 65535 bytes according C standards? 什么时候应该在C中使用malloc,什么时候不使用? - When should I use malloc in C and when don't I?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM