I just tested malloc
with a huge memory request and it didn't return NULL
. I already heard this but the BSD (I'm on a mac) man-page says:
RETURN VALUES
If successful,calloc()
,malloc()
,realloc()
,reallocf()
, andvalloc()
functions return a pointer to allocated memory. If there is an error, they return aNULL
pointer and set errno toENOMEM
.
How can I check correctly and reliable, that the returned pointer points to a valid block of requested size?
EDIT: I just saw this post. Same for BSD I guess?
EDIT 2: The code:
typedef struct {
uint8_t red;
uint8_t green;
uint8_t blue;
} RGB_TYPE;
int main() {
RGB_TYPE *field = malloc(50000 * 50000 * sizeof(RGB_TYPE));
if (!field)
return -1;
... write to field (SEG_FAULT) ...
return 0;
}