Re:In C, how do you know if the dynamic allocation succeeded?
by AshamedCaptain on Friday November 05, 2021 @07:28PM
(#29125828)
Technically the allocation succeeded. That's why you have a pointer and not NULL. So the answer is the classic one: "you check if the pointer is NULL or not".
This entire thing is a wrong question to be asking, anyway.
Even if you are sure right after malloc that "you have the memory 100% available to you", who's not to say that, immediately one nanosecond afterwards, some other process comes in and, well, asks for more memory? The memory you were so sure you had for yourself right after malloc may no longer be there (either swapped out or even discarded if possible), and you will be killed for trying to access it again. Oops.
At no point you can say "I have this memory and it's mine and just mine!" unless you are root and mlock (or equivalents). And I hope you don't do that, anyway. Just let the OS do what it is designed to do.
Re:In C, how do you know if the dynamic allocation succeeded?
by one_off_comment on Friday November 05, 2021 @04:03PM
(#29124125)
There's gotta be some way to programmatically determine it, right? It may not be portable. It may require some system calls or something, but there's gotta be a way, right?
Re:In C, how do you know if the dynamic allocation succeeded?
by wmf on Friday November 05, 2021 @04:18PM
(#29124292)
I guess you could mlock() your memory and see if that succeeds or fails. When overcommit is enabled no memory is really safe.
Re:In C, how do you know if the dynamic allocation succeeded?
by matheusmoreira on Friday November 05, 2021 @10:33PM
(#29127016)
Maybe a system call that checks whether writing to a page would result in a fault? What if we had memory file descriptors for pages? We'd be able to use them with epoll and io_uring to asynchronously check whether it's safe to write to the pages they represent.
Re:In C, how do you know if the dynamic allocation succeeded?
by yjftsjthsd-h on Friday November 05, 2021 @04:22PM
(#29124335)
I'm not an expert enough to tell you; I just read the article and decided that it was too long so summarized for others. There's some discussion upthread about catching SIGSEGV and other methods, FWIW.
I kept reading hoping that there was going to be a solution, but not really; there are comments discussing disabling overcommit, but even that's a tradeoff (it does fix this failure mode, but you might not want to actually run a system like that).