Hi All,
Because I'm not quite sure what is all needed and to make this easier to
discuss, I put the entire lot into a standalone proggy.
Let me know if anything is missing or should be different, I commented the
code where I was not sure about certain things.
My output is listed below the code.
thanks,
G
Ps.: the string ": Cannot allocate memory" just pops up, I don't know where
from, maybe stderr?
[[
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <unistd.h>
void *xmalloc(size_t size)
{
void *ptr = malloc(size);
if (ptr == NULL) {
perror("xmalloc: out of memory.\n");
/* _exit(EXIT_FAILURE); // commented out for testing */
return NULL;
}
return ptr;
}
void xfree(void *ptr)
{
if (ptr == NULL)
return;
free(ptr);
ptr = NULL;
}
void *xcalloc(size_t nmemb, size_t size)
{
void *ptr = calloc(nmemb, size);
if (ptr == NULL) {
perror("xcalloc: out of memory.\n");
/* _exit(EXIT_FAILURE); // commented out for testing */
return NULL;
}
return ptr;
}
void *xrealloc(void *ptr, size_t size)
{
void *ret_ptr = realloc(ptr, size);
if (ret_ptr == NULL) {
perror("xrealloc: out of memory.\n");
/* _exit(EXIT_FAILURE); // commented out for testing */
return NULL;
}
return ret_ptr;
}
int main (int argc, char *argv[])
{
printf("==================================================\n\n");
printf("xmalloc tests:\n");
{
// what happens if we overallocate, check the NULL trap works
int count = 0;
while (1) {
void *p = xmalloc(1024*1024*1024);
if (p == NULL) {
printf("count = %d\n",count);
printf("OK: overallocating xmalloc returned NULL\n\n");
break;
}
else {
count++;
}
}
}
{ // what happens if size = 0
size_t size = 0;
void *p = xmalloc(size);
if (p == NULL) {
printf("Error: size = 0 -- xmalloc returned NULL ");
}
else
printf("OK: size = 0 returns %p, %zu\n", p, sizeof(p));
}
printf("==================================================\n\n");
printf("xcalloc tests:\n");
{
// what happens if we overallocate, check the NULL trap works
int count = 0;
while (1) {
void *p = xcalloc(1024*1024*1024, sizeof (int));
if (p == NULL) {
printf("count = %d\n",count);
printf("OK: overallocating xcalloc returned NULL\n\n");
break;
}
else {
count++;
}
}
}
{ // what happens if size = 0
// Manual says: If nmemb or size is 0, then calloc() returns
// either NULL, or a unique pointer value that can later be
// successfully passed to free(). Since we set xfree to
// protect against freeing NULL, we don't have to worry. (I
// think) But, the NULL ptr may be used to assign other stuff
// too, which is not so good.
size_t size = 0;
void *p = xcalloc(1024, size);
if (p == NULL) {
printf("Error: size = 0: xcalloc returned NULL\n");
}
else
printf("OK: size = 0 returns %p, %zu\n", p, sizeof(p));
}
// realloc test
printf("==================================================\n\n");
printf("xrealloc tests:\n");
{
void *p = xmalloc(1000);
// what happens if we overallocate, check the NULL trap works
int count = 0;
while (1) {
p = xrealloc(p, 1024*1024*1024);
if (p == NULL) {
printf("count = %d\n",count);
printf("OK: overallocating realloc returned NULL\n\n");
break;
}
else {
count++;
}
}
}
{ // what happens if size = 0
size_t size = 0;
void *p = xmalloc(1000);
p = xrealloc(p, size);
if (p == NULL) {
// should we force the returning of a pointer here so it
// can be freed later?
printf("Error: size = 0 test: xrealloc returned NULL\n");
}
else
printf("OK: size = 0 returns pointer %p.\n", p);
}
{ // what happens if we resize to a smaller allocation
size_t size = 500;
void *p = xmalloc(1000);
p = xrealloc(p, size);
if (p == NULL) {
printf("Error: size = 0 test: xrealloc returned NULL.\n");
}
else {
// actually, I'm not sure. I cannot think of a way to
// measure the size allocated to a pointer. sizeof(p)
// clearly is wrong here, since it only tells me how
// large the pointer is, not how much it points to.
printf("Result of resize: size required %zu -- "
"returns pointer %p of size %zu.\n",
size, p, sizeof(p));
}
}
printf("==================================================\n\n");
printf("xfree tests:\n");
{
void *p = xmalloc(133);
xfree(p);
p = NULL;
xfree(p);
printf("xfree accepts NULL pointer.\n");
xfree(p);
printf("xfree accepts second attempt to free pointer.\n");
}
printf("==================================================\n\n");
printf("Test complete.\n");
return 1;
}
]]
Output:
==================================================
xmalloc tests:
xmalloc: out of memory.
: Cannot allocate memory
count = 2
OK: overallocating xmalloc returned NULL
OK: size = 0 returns 0x37400468, 4
==================================================
xcalloc tests:
xcalloc: out of memory.
: Cannot allocate memory
count = 0
OK: overallocating xcalloc returned NULL
OK: size = 0 returns 0x37400478, 4
==================================================
xrealloc tests:
xrealloc: out of memory.
: Cannot allocate memory
count = 0
OK: overallocating realloc returned NULL
xrealloc: out of memory.
: Cannot allocate memory
Error: size = 0 test: xrealloc returned NULL
Result of resize: size required 500 -- returns pointer 0x37400878 of size 4.
==================================================
xfree tests:
xfree accepts NULL pointer.
xfree accepts second attempt to free pointer.
==================================================
Test complete.
--
Visit my Coding Diary: http://gabriela-gibson.blogspot.com/