Не могу добиться от malloc возврата NULL!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
int main()
{
clock_t tmstmp;
tmstmp = clock();
printf("clock = %ld\n", tmstmp);
size_t gb24 = 24LU*1024*1024*1024;
volatile unsigned char* pre_mem = malloc(gb24);
for (size_t i = 0; i < gb24; i += sizeof(clock_t)) {
*(pre_mem + i) = clock();
}
//uint_t offset = 0;
//explicit_bzero(pre_mem, gb24);
printf("starting the loop\n");
tmstmp = clock();
printf("clock = %ld\n", tmstmp);
fflush(stdin);
size_t counter = 0;
for(;counter < 12345678;++counter) {
//*(pre_mem + offset) = counter;
//offset += (sizeof(counter));
size_t mem_size = 64*1024;
volatile unsigned char* mem = malloc(mem_size);
for (size_t i = 0; i < mem_size; i+=sizeof(clock_t)) {
*(mem+i) = clock();
}
if (NULL == mem) {
printf("iter %lu: NULL\n", counter);
break;
} else {
for (size_t i = 0; i < mem_size; i+=sizeof(clock_t)) {
*(mem+i) = clock();
}
*(mem + counter%(4096)) = counter;
}
}
return counter % 123456789;
}
У меня на машине 16ГБ озу и 32 свопа.
/tmp ❯❯❯ journalctl -k --since "-5 minutes" | grep -i -E 'out of memory|oom-killer|memory cgroup|Killed process'
Nov 16 18:09:49 g16 kernel: nvim invoked oom-killer: gfp_mask=0x140cca(GFP_HIGHUSER_MOVABLE|__GFP_COMP), order=0, oom_score_adj=0
Nov 16 18:09:49 g16 kernel: Out of memory: Killed process 428379 (a.out) total-vm:29233540kB, anon-rss:9961472kB, file-rss:724kB, shmem-rss:0kB, UID:1000 pgtables:57260kB oom_score_adj:0
если не инициализировать всю память, а только «местами», то краш случается, когда виртуальной памяти отдано уже 80-110 гигов.
Помогите получить от malloc NULL!!
А то что это получается, не надо чекать что там вернул malloc?









