sábado, 21 de novembro de 2009

Example of xmalloc ~ xmalloc.c

#include <stdio.h>
#include <stdlib.h>

void *
xmalloc (size_t size)
{
 void *malloc();
 register void *value = malloc (size);
 if(value == 0)
  error("Virtual memory exhausted");
 return (value);
}


char *
savestring (const char *ptr, size_t len)
{
 register char *value = (char *) xmalloc (len + 1);
 value[len] = '\0';
 return (char *) memcpy (value, ptr, len);
}


int
main (void)
{
 char *str;
 
 str = savestring ("Hello World", 12);
 printf("%s\n", str); 
 return(0);
}
xmalloc check if malloc successfully allocate the memory that you need

Nenhum comentário:

Postar um comentário