3 ms·
What does malloc() return if it fails? volatile char *x = 0; And what happens if you write to that address? *x = 69; The answers to these questions,
by geocar 13y ago
What does malloc() return if it fails?
volatile char *x = 0;
And what happens if you write to that address?
*x = 69;
The answers to these questions, whilst not defined by the C language, are portable enough.
- emillon 13y agoThe difference is in the error message you get. Adding "if (x == NULL) abort()" will display a backtrace near the error. To understand the cause of the segfault, you will have to follow the data flow in your favorite debugger.
- clarry 13y agoWhat about the more common situation where the thing to be allocated is a (potentially large) array? These aren't always accessed sequentially. And the offset might be (indirectly) controlled by untrusted data from the wild.