Thread: What is wrong with this statement???

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    45

    What is wrong with this statement???

    char * p1;
    char * p2;
    char * tmp;
    p1=(char *) malloc(sizeof(char));
    p2=(char *) malloc(sizeof(char));
    *p1='A';
    *p2='B';
    tmp=p1;
    p2=tmp;

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    FAQ

    If the return of malloc is cast then the error which would be flagged is hidden, resulting in a difficult to find bug.
    What error? Failure to allocate? There is still a NULL pointer that is returned, right?
    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.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >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.*

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    ok, i got it thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong with my if statement
    By joker_tony in forum C Programming
    Replies: 6
    Last Post: 05-10-2008, 02:11 AM
  2. what is wrong with my if statement
    By joker_tony in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 12:26 AM
  3. switch case statement
    By stanlvw in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 05:06 AM
  4. What's wrong with my success statement??
    By cool_dude07 in forum C Programming
    Replies: 7
    Last Post: 07-21-2007, 11:22 PM
  5. Something wrong with this if statement?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-30-2002, 05:19 PM