The question might seem little trivial,i'm trying to write a program in C,which just eats away memory as much as it could before OOM gets invoked and kills it.Although,i initially used malloc() with memset(),i decided to try realloc() this time.I'm doing this purely for learning purpose since i'm new to C.
I intend for an allocation of 1MB with every call to realloc() and memset().When i run this program to allocate 20MB:
1) I dont understand why in the output some of the address are the same (2-4) (5-10) & (11-20) ?Shouldn't each one of them be different.
2) Is my program really consuming 20MB of memory? I did run it through Valgrind and it says "22,020,096 bytes in 1 blocks are definitely lost in loss record 1 of 1"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <error.h>
#include <unistd.h>
int main (int argc,char **argv) {
char *ptr;
int max = 0;
int max_write;
int sleep_time;
size_t size = 1048576;
if (argc > 1) {
sleep_time = (argv[2] ? atoi(argv[2]) : 2 );
max_write = (argv[1] ? atoi(argv[1]) : 1000);
printf (" + To Write: %d,%d\n",max_write,sleep_time);
}
ptr = (char *) calloc (1,size);
if (ptr == NULL) {
perror("calloc Error:");
}
printf(" + Allocation: %p\n",ptr);
do {
max++;
size += (1048576);
ptr = (char *) realloc (ptr,size);
if (ptr == NULL) {
perror("realloc Error:");
}
memset(ptr,0,size);
printf(" + Pointer: %p / Memory: %d\n",ptr,max);
} while (max != max_write);
//
return(0);
}
OUTPUT:
./eatmemory 20
+ Allocation: 0x7f2bb6b12010
+ Pointer: 0x7f2bb6451010 / Memory: 1
+ Pointer: 0x7f2bb6150010 / Memory: 2
+ Pointer: 0x7f2bb6150010 / Memory: 3
+ Pointer: 0x7f2bb6150010 / Memory: 4
+ Pointer: 0x7f2bb5b4f010 / Memory: 5
+ Pointer: 0x7f2bb5b4f010 / Memory: 6
+ Pointer: 0x7f2bb5b4f010 / Memory: 7
+ Pointer: 0x7f2bb5b4f010 / Memory: 8
+ Pointer: 0x7f2bb5b4f010 / Memory: 9
+ Pointer: 0x7f2bb5b4f010 / Memory: 10
+ Pointer: 0x7f2bb4f4e010 / Memory: 11
+ Pointer: 0x7f2bb4f4e010 / Memory: 12
+ Pointer: 0x7f2bb4f4e010 / Memory: 13
+ Pointer: 0x7f2bb4f4e010 / Memory: 14
+ Pointer: 0x7f2bb4f4e010 / Memory: 15
+ Pointer: 0x7f2bb4f4e010 / Memory: 16
+ Pointer: 0x7f2bb4f4e010 / Memory: 17
+ Pointer: 0x7f2bb4f4e010 / Memory: 18
+ Pointer: 0x7f2bb4f4e010 / Memory: 19
+ Pointer: 0x7f2bb4f4e010 / Memory: 20
1) I dont understand why in the output some of the address are the same (2-4) (5-10) & (11-20) ?Shouldn't each one of them be different.
As others already stated, realloc doesn't necessarily have to move the existing memory chunk, but may just extend it.
2) Is my program really consuming 20MB of memory? I did run it through Valgrind and it says "22,020,096 bytes in 1 blocks are definitely lost in loss record 1 of 1"
This is somewhat implementation specific, but you would usually end up claiming more memory than you asked for. For one, free needs some meta-data on how to merge the memory with neighboring free chunks. This information often resides in a couple of bytes right before the address that alloc/realloc will return. Also, the memory may not be organized in a way that would allow for allocations of arbitrary sizes, so malloc will just return the one that fits best.