The thorny case of kmalloc(0)
Posted Jun 7, 2007 18:27 UTC (Thu) by
pj (subscriber, #4506)
In reply to:
The thorny case of kmalloc(0) by dvrabel
Parent article:
The thorny case of kmalloc(0)
Exactly... it should be valid to allocate 0 bytes of memory as long as you don't try and dereference the pointer and use it for anything. Consider code like:
items[] itemblock = kmalloc( itemcount * sizeof(items) );
ASSERT(itemblock != NULL);
for(int i = 0; i++ ; i < itemcount) {
...do something with itemblock[i]...
}
itemcount is allowed to be 0 in the above code, with no problems. a kmalloc(0) occurs, but the for loop never gets its body run, so itemblock is never dereferenced. At the same time, the ASSERT only happens when there's a problem allocating memory.
It's a good solution. And I like Linus' comment :)
(
Log in to post comments)