简体   繁体   English

C:malloc似乎分配更多然后我请求(数组)

[英]C : malloc seems to allocate more then i'm requesting (array)

Hi I have this question: Why when I'm allocating memory with malloc for a float array, it allocate more space that i'm requesting? 嗨,我有一个问题:为什么当我使用malloc为浮点数组分配内存时,它会分配我所请求的更多空间? For example in this code i'm trying to allocate a ten "cells" float array, but if I try to access to other cells it returns me no segfault error: 例如,在这段代码中,我试图分配十个“单元”浮点数组,但是如果我尝试访问其他单元,则不会返回段错误:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

float *fk_array;

int main(int argc, char** argv){


    fk_array = (float *) malloc(10 * sizeof(float));
    int i;
    fk_array[9] = 2.23;
    fk_array[15] = 0.3;
    for(i = 0; i<20 ; i++)
        printf("%f\n",fk_array[i]);
    free(fk_array);

    return 1;
}

It returns me : 它返回我:

0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
2.230000
0.000000
0.000000
0.000000
0.000000
0.000000
0.300000
0.000000
0.000000
0.000000
0.000000

Where I'm in wrong?? 我错在哪里?

It's not unlikely that, depending on allocation algorithm, that more is allocated than you request. 根据分配算法,分配的数量比您请求的数量多的可能性不大。 However, accessing that memory is undefined behavior . 但是,访问该内存是未定义的行为

When calling malloc , additional space may used to store metadata about the allocated block, such as its size and information about other allocated blocks. 调用malloc ,可以使用附加空间来存储有关已分配块的元数据,例如其大小和有关其他已分配块的信息。 Sometimes the implementation prefers allocated blocks to be at specific intervals (eg, multiples of 4 or 8 bytes). 有时,实现方式更喜欢分配的块处于特定间隔(例如4或8字节的倍数)。 However, you shouldn't depend on the amount of space being consistent each time you call malloc ; 但是,您不应该依赖于每次调用malloc时保持一致的空间量; use only what you have specifically requested or you may be overwriting other important information. 仅使用您特别要求的内容,否则您可能会覆盖其他重要信息。

See the answers to the following questions for more information: 有关更多信息,请参见以下问题的答案:

I would not be surprised if allocating heap memory does not fall into specific processor rules that addresses must be aligned on a long, quad, or other boundary. 如果分配堆内存不属于必须在长,四或其他边界上对齐地址的特定处理器规则,我不会感到惊讶。

So, if you requested 100 bytes of heap space, the OS may return more than you requested to satisfy alignment requirements of the processor. 因此,如果您请求100字节的堆空间,则OS可能会返回比您满足处理器对齐要求的请求更多的内存。 However, accessing more than the 100 bytes is considered undefined and can and probably will result in a lot of strange behavior you do not want. 但是,访问超过100个字节被认为是未定义的,并且可能并且很可能会导致许多您不想要的奇怪行为。

You can only get a segmentation fault if a read or write falls in the wrong page . 如果读取或写入位于错误的页面中,则只会出现分段错误。 A page is typically 4KiB. 一个页面通常为4KiB。

The malloc() system call may reserve a bit more space than requested, and it reserves a few words of memory before the block for book-keeping, but that your program does not get killed when accessing fk_array[15] does not mean that it did not access outside the block reserved for it. malloc()系统调用可能比请求保留更多的空间,并且在该块之前保留了一些单词用于簿记,但是访问fk_array[15]时您的程序不会被杀死,并不意味着它没有在为其保留的块之外访问。 It probably did, the OS just didn't notice it. 它可能做到了,操作系统只是没有注意到它。 If you had allocated another array after allocating fk_array , you would have had a chance to notice that it was changing. 如果在分配fk_array之后分配了另一个数组,您将有机会注意到它正在更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM