char * p1;
char * p2;
char * tmp;
p1=(char *) malloc(sizeof(char));
p2=(char *) malloc(sizeof(char));
*p1='A';
*p2='B';
tmp=p1;
p2=tmp;
char * p1;
char * p2;
char * tmp;
p1=(char *) malloc(sizeof(char));
p2=(char *) malloc(sizeof(char));
*p1='A';
*p2='B';
tmp=p1;
p2=tmp;
Using [code] [/code] tags makes posted code easier to read.
Code:char * p1; char * p2; char * tmp; p1=(char *) malloc(sizeof(char)); p2=(char *) malloc(sizeof(char)); *p1='A'; /* Possible use of null pointer 'p1' in argument to operator 'unary *' */ *p2='B'; /* Possible use of null pointer 'p2' in argument to operator 'unary *' */ tmp=p1; p2=tmp; /* Creation of memory leak in assignment to 'p2' */
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
read the FAQ to see why casting the return value of malloc is generally considered a bad idea.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
What error? Failure to allocate? There is still a NULL pointer that is returned, right?FAQ
If the return of malloc is cast then the error which would be flagged is hidden, resulting in a difficult to find bug.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
>There is still a NULL pointer that is returned, right?
In the context from which it was taken -- when stdlib.h is not #included (for a pre-C99 compiler) -- no: malloc would be implicitly declared as returning an int -- which is not a pointer. I think Joona does a good job with an explanation, but the rest of the thread is here.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
ok, i got it thanks.