5 ms·
I have some questions: Have you experienced malloc failing a lot? How do you test to ensure error handling code paths work as expected? Have you read 'notes'
by aivarsk 11y ago
I have some questions:
Have you experienced malloc failing a lot? How do you test to ensure error handling code paths work as expected?
Have you read 'notes' section of Linux malloc manpage? It says:
By default, Linux follows an optimistic memory allocation strategy. This means that when malloc() returns non-NULL there is no guarantee that the memory really is available. In case it turns out that the system is out of memory, one or more processes will be killed by the OOM killer.
- danieltillett 11y agoI have never knowingly experienced malloc failing in any of my code which is why I asked if it is something to worry about in practice. Once you run out of memory things start dying or the system locks up hard. Edit. It is impossible for malloc to fail (at least on linux) [1]. Seems rather pointless checking for something that can’t happen. 1. https://scvalex.net/posts/6/ https://scvalex.net/posts/6/
- dalke 11y agoThat link shows that malloc failed, just not due to exceeding available RAM. At the end it says "malloc finally failed because of address space exhaustion."
- danieltillett 11y agoIf the memory was actually being used it would have crashed long before that. The test was kind of artificial.
- dalke 11y agoAbsolutely. I was pointing out that "It is impossible for malloc to fail" is, according to the linked page, an incorrect summary.
- danieltillett 11y agoIt is still pointless checking if malloc fails on linux :) Actually the real issue here is that you should be aware that on some systems malloc might fail and not segfault.
- sovande 11y agoOn virtual memory systems, it is a safe bet that malloc will "never" fail to return a value. It is just good form to check the return value of system calls. If you program for embedded systems (e.g. Arduino) and bring over the bad habit of never checking malloc, you are going to be in for a surprise. I'm not sure, but I believe on iOS, malloc could fail on the device.
- dalke 11y agoI had malloc fail recently, on a machine with low rlimits. Here's an example on FreeBSD 8.2: #include <stdlib.h> #include <stdio.h> #include <sys/resource.h> const int SIZE = 64*1024*1024; int main() { char *buffer = NULL; struct rlimit r; /* Limit all memory to 1024 bytes */ r.rlim_cur = 1024; r.rlim_max = 1024; if (setrlimit(RLIMIT_AS, &r) == -1 ) { perror("setrlimit(RLIMIT_AS) error\n"); exit(EXIT_FAILURE); } if ( (buffer = malloc( sizeof(long) * SIZE) ) == NULL) { perror("malloc() failed\n"); exit(EXIT_FAILURE); } free(buffer); printf("Malloc worked\n"); return 0; } Here's the output: %cc fail.c %./a.out malloc() failed : Cannot allocate memory If malloc failure testing is important to you, then you'll need some way to replace your use of the system malloc/free with special versions of your own. (Eg, through function pointers, or LD_PRELOAD.) Your own versions can be crafted to insert faults in just the right spots. It's tedious work.