为什么malloc分配不同请求的字节数? [英] Why does malloc allocate a different number of bytes than requested?

查看:95
本文介绍了为什么malloc分配不同请求的字节数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码

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

int main(){
void *a, *b;

a=malloc(16);
b=malloc(16);
printf("\n   block size (for a): %p-%p : %li",b,a,b-a);

a=malloc(1024);
b=malloc(1024);
printf("\n   block size (for a): %p-%p : %li",b,a,b-a); 
}

这不应该显示最后分配的块大小? (16或1024)。
它打印24& 1032,所以它有8个额外的字节...

This shouldn't display the the last allocated block size? (16 or 1024). It prints 24 & 1032, so it has 8 extra bytes...

我的问题是(在做这个测试用例之前)我在一个函数(1024字节)中执行malloc,并返回分配结果。当检查功能返回的块大小时,我得到516块...我不明白为什么。我想这可能是在分配的缓冲区进行一些处理之后发生的内存缓存的原因:)

My problem is (before making this testcase) that i do malloc in a function (1024 bytes), and return the allocated result. When checking the block size on the function return i get 516 blocks... and i dont get it why. I guess this might be the reason for the memory curruption that occurs after doing some processing on the allocated buffers:)

编辑:看到如何从C中的指针获取数组的大小?并且似乎问同样的事情,对于重新发布,对不起。

i've seen How can I get the size of an array from a pointer in C? and seems to ask the same thing, sorry for reposting.

我已经将我的示例重写为我更具体的代码:

I've redone my example to my more specific code:

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

short int * mallocStuff(long int number, short int base){
    short int *array;
    int size=1024;

    array=(short int*)calloc(1,size);
    //array=(short int*)malloc(size);

    return array;
}

int main(){
    short int **translatedArray;

    translatedArray=malloc(4*sizeof(short int));

    int i;
    for(i=0;i<4;i++){
        translatedArray[i]=mallocStuff(0,0);

        if(i>0)
            printf("\n   block size (for a): %p-%p : %i",translatedArray[i],translatedArray[i-1],translatedArray[i]-translatedArray[i-1]);
    }

    return 0;
}

输出是
块大小(对于a):0x804a420 -0x804a018:516
块大小(for a):0x804a828-0x804a420:516
块大小(对于a):0x804ac30-0x804a828:516

And the output is block size (for a): 0x804a420-0x804a018 : 516 block size (for a): 0x804a828-0x804a420 : 516 block size (for a): 0x804ac30-0x804a828 : 516

根据上面的帖子大于1024 ...我错了吗?

According to the above post that be bigger than 1024...am i wrong?

推荐答案

你有一个错误。而不是:

You have a bug. Instead of:

translatedArray=malloc(4*sizeof(short int));

您应该有

translatedArray=malloc(4*sizeof(short int*));

请注意代码中缺少的指针。我怀疑这是你观察到的行为源于的地方。

Note the missing pointer in your code. I suspect this is where your observed behavior stems from.

同时注意到 0x804a420 - 0x804a018 = 1032 ,而不是 516 。公式 translatedArray [i] - convertedArray [i - 1] 为您提供元素(短整型或更简单的简写)的数量两个地址之间,而不是字节的数量。

Also notice that 0x804a420 - 0x804a018 = 1032, not 516. The formula translatedArray[i] - translatedArray[i - 1] gives you the number of elements (short ints, or more simply, shorts) in between the two addresses, not the number of bytes.

这篇关于为什么malloc分配不同请求的字节数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆