malloc.c
void *realloc(void *ptr, size_t size)
{
	union Header *block;
	size_t to_copy;
	void *buf;

	/* special case: if ptr is null, then allocate a new buffer
	 * using malloc */
	if (ptr == 0) {
		return malloc(size);
	}

	/* special case: if size is 0, then free the buffer */
	if (size == 0) {
		free(ptr);
		return 0;
	}

	/* find buffer's block header */
	block = ((union Header *)ptr) - 1;

	/* allocate a new buffer */
	buf = malloc(size);
	if (buf == 0) {
		return 0;
	}

	/* copy data from old buffer to new buffer */
	to_copy = block->h.size - sizeof(union Header); /* original buffer size */
	if (to_copy > size) {
		/* original size was larger than new size */
		to_copy = size;
	}
	memcpy(buf, ptr, to_copy);

	/* free the old buffer */
	free(ptr);

	/* return the new buffer */
	return buf;
}
/*
 * Reallocate given buffer so that it has given size.
 */
Reallocate given buffer so that it has given size.
Reallocate given buffer so that it has given size .
void *realloc(void *ptr, size_t size)
{
	union Header *block;
	size_t to_copy;
	void *buf;
	if (ptr == 0) {
		return malloc(size);
	}
	if (size == 0) {
		free(ptr);
		return 0;
	}
	block = ((union Header *)ptr) - 1;
	buf = malloc(size);
	if (buf == 0) {
		return 0;
	}
	to_copy = block->h.size - sizeof(union Header); 
	if (to_copy > size) {
		to_copy = size;
	}
	memcpy(buf, ptr, to_copy);
	free(ptr);
	return buf;
}
void * realloc ( void * ptr , size_t size ) { union Header * block ; size_t to_copy ; void * buf ; if ( ptr == 0 ) { return malloc ( size ) ; } if ( size == 0 ) { free ( ptr ) ; return 0 ; } block = ( ( union Header * ) ptr ) - 1 ; buf = malloc ( size ) ; if ( buf == 0 ) { return 0 ; } to_copy = block -> h . size - sizeof ( union Header ) ; if ( to_copy > size ) { to_copy = size ; } memcpy ( buf , ptr , to_copy ) ; free ( ptr ) ; return buf ; }
Reallocate given buffer so that it has given size.
Reallocate given buffer so that it has given size .
/* special case: if ptr is null, then allocate a new buffer
	 * using malloc */ /* special case: if size is 0, then free the buffer */ /* find buffer's block header */ /* allocate a new buffer */ /* copy data from old buffer to new buffer */ /* original buffer size */ /* original size was larger than new size */ /* free the old buffer */ /* return the new buffer */