Categories:
.NET (961)
C (387)
C++ (185)
CSS (84)
DBA (8)
General (31)
HTML (48)
Java (641)
JavaScript (220)
JSP (109)
JUnit (31)
MySQL (297)
Networking (10)
Oracle (562)
Perl (48)
Perl (9)
PHP (259)
PL/SQL (140)
RSS (51)
Software QA (28)
SQL Server (5)
Struts (20)
Unix (2)
Windows (3)
XHTML (199)
XML (59)
Other Resources:
malloc Function
Why does malloc(0) return valid memory address? What's the use?
✍: FYIcenter
malloc(0) does not return a non-NULL under every implementation. An implementation is free to behave in a manner it finds suitable, if the allocation size requested is zero. The implmentation may choose any of the following actions:
* A null pointer is returned.
* The behavior is same as if a space of non-zero size was requested. In this case, the usage of return value yields to undefined-behavior.
Notice, however, that if the implementation returns a non-NULL value for a request of a zero-length space, a pointer to object of ZERO length is returned! Think, how an object of zero size should be represented?
For implementations that return non-NULL values, a typical usage is as follows:
void func ( void ) { int *p; /* p is a one-dimensional array, whose size will vary during the the lifetime of the program */ size_t c; p = malloc(0); /* initial allocation */ if (!p) { perror ("FAILURE"); return; } /* ... */ while (1) { c = (size_t) ...; /* Calculate allocation size */ p = realloc ( p, c * sizeof *p ); /* use p, or break from the loop */ /* ... */ } return; }
Notice that this program is not portable, since an implementation is free to return NULL for a malloc(0) request, as the C Standard does not support zero-sized objects.
2007-02-26, 6016👍, 0💬
Popular Posts:
What is AL.EXE and RESGEN.EXE? In the previous question you have seen how we can use resource files ...
What is Windows DNA architecture? Note :- If you have worked with classic ASP this question can come...
What is the difference between "printf(...)" and "sprintf(...)"? sprintf(...) writes data to the cha...
What is difference between Association, Aggregation and Inheritance relationships? In object oriente...
What is the difference between Authentication and authorization? This can be a tricky question. Thes...