File indexing completed on 2024-12-22 04:18:20
0001 #include <stdio.h> 0002 #include <stdlib.h> 0003 0004 static int MB = 1024*1024; 0005 0006 static int sum = 0; 0007 0008 void* tryAllocate(size_t size) 0009 { 0010 sum += size/MB; 0011 void* ptr = malloc(size); 0012 if (!ptr) { 0013 printf("FAILED allocating %i MB.\n", size/MB); 0014 return 0; 0015 } 0016 0017 printf("allocated %i MB.\n", size/MB); 0018 return ptr; 0019 } 0020 0021 int main(int argc, char *argv[]) 0022 { 0023 size_t size = 100; 0024 if (argc == 2) { 0025 size = atoi(argv[1]); 0026 } 0027 0028 void* ptr1 = tryAllocate(size * MB); 0029 void* ptr2 = tryAllocate(1500 * MB); 0030 size_t re = 10 * size; 0031 void* ptr3 = realloc(ptr1, re * MB); 0032 sum += re - size; 0033 0034 int ret = 0; 0035 if (!ptr3) { 0036 printf("FAILED reallocating %i MB.\n", re); 0037 ret = 1; 0038 } else { 0039 printf("reallocated %i MB.\n", re); 0040 } 0041 0042 if (ret == 0) 0043 printf("allocated %i MB in sum\n", sum); 0044 else 0045 printf("FAILED to allocate %i MB in sum\n", sum); 0046 0047 return 0; 0048 }