为什么的malloc分配不同的字节数比请求? [英] Why does malloc allocate a different number of bytes than requested?
问题描述
我有这块code的
#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块......,我不明白为什么。我想这可能是这样做的分配的缓冲区一些处理后,会出现内存curruption的原因:)
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:)
编辑:我见过<一个href=\"http://stackoverflow.com/questions/232691/array-size-from-pointer-in-c\">http://stackoverflow.com/questions/232691/array-size-from-pointer-in-c似乎要问同样的事情,对不起,重新发布。
i've seen http://stackoverflow.com/questions/232691/array-size-from-pointer-in-c and seems to ask the same thing, sorry for reposting.
我重做我的例子我更具体的code:
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;
}
和输出是
块大小(对于一):0x804a420-0x804a018:516
块大小(对于一):0x804a828-0x804a420:516
块大小(一):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*));
请注意在code缺少的指针。我怀疑这是你观察到的行为源于
Note the missing pointer in your code. I suspect this is where your observed behavior stems from.
另请注意, 0x804a420 - 0x804a018 = 1032
,而不是 516
。其计算公式 translatedArray [I] - translatedArray [我 - 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屋!