Re: Allocating too much memory kills my App rather than returning NULL
Re: Allocating too much memory kills my App rather than returning NULL
- Subject: Re: Allocating too much memory kills my App rather than returning NULL
- From: Igor Mozolevsky <email@hidden>
- Date: Sat, 05 Nov 2011 03:27:03 +0000
On 5 November 2011 02:38, Dave Zarzycki <email@hidden> wrote:
> You're tripping across a fundamental aspect of how virtual memory works.
[snip]
Also, watch what you're *really* allocating:
% cat ./malloc_test.c
#include <stdio.h>
#include <stdlib.h>
#include <malloc/malloc.h>
int main(int c, char **v) {
const size_t size[] = {2, 33, 1025, 4097, 1024*1024+1, 0};
for(int i = 0; size[i] != 0; i++) {
unsigned char *ptr = calloc(1, size[i]);
if(!ptr) {
abort();
}
fprintf(stderr,
"Allocated `%zu' bytes at %p\n"
"\treally allocated: %zu\n"
"\tsuggested good size: %zu\n",
size[i],
ptr,
malloc_size(ptr),
malloc_good_size(size[i]));
free(ptr);
}
return 0;
}
% cc --std=c99 -o malloc_test malloc_test.c
% ./malloc_test
Allocated `2' bytes at 0x100100080
really allocated: 16
suggested good size: 16
Allocated `33' bytes at 0x100100090
really allocated: 48
suggested good size: 48
Allocated `1025' bytes at 0x100800000
really allocated: 1536
suggested good size: 1536
Allocated `4097' bytes at 0x100800600
really allocated: 4608
suggested good size: 4608
Allocated `1048577' bytes at 0x100200000
really allocated: 1052672
suggested good size: 1052672
Cheers,
--
Igor :-)
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden