Following is my reply to a question posted in a newsgroup,
in which, a person said my advice was wrong without
saying where and why. I turn to clc.
[BEGINS]
Hi friends,
I cam across these questions and seem baffled by these
Can anyone tell me answers to this?
This code may have bugs!. If any suggest remedy
to this else give output!
#1.c
#include<stdio.h>
You should include <stdlib.h> for malloc.
main()
{
void *pointer;
void *vector;
void *address;
void *location;
The prototype of malloc is:
void *malloc ( size_t );
where,
size_t is an unsigned integer typedefe in <stdlib.h>.
pointer=malloc(-1);
Read: http://www.geocities.com/vijoeyz/faq/c/unsigned2.txt
to know about how a negative integer is assigned to an unsigned
int.
vector=malloc(0);
Each compiler is free to define the behaviour of malloc () when
the size is 0. Usually, the compiler documents how it does.
Two possibilies are:
When the requested size is zero
* return a NULL pointer.
* Assume some non-zero size
In either case, using the return value malloc() can cause undefined
behaviour.
address=malloc(1);
This is OK.
location=NULL;
free(pointer);
free(vector);
free(address);
If the corresponding mallocs were successful, then this is OK.
free(location);
This is also legal, but no action occurs for free ( NULL ).
[ENDS]