explicit cast to malloc()

Discussion in 'C Programming' started by mohi, Jul 25, 2008.

  1. mohi

    mohi Guest

    hello everyone ,
    as i have noted a lot many times it has been mentioned that we shudn't
    cast pointers returned by malloc()
    i mean
    its been said that use
    node * nod=malloc(sizeof(node));

    instead of

    node * nod=(node *) malloc( sizeof(node));

    but when i use the upper way my gcc compiler shows error like

    invalid conversion from void* to node*

    whats the matter ?

    pls help
    thank you
    mohan
     
    mohi, Jul 25, 2008
    #1
    1. Advertising

  2. mohi <> writes:
    > as i have noted a lot many times it has been mentioned that we shudn't
    > cast pointers returned by malloc()
    > i mean
    > its been said that use
    > node * nod=malloc(sizeof(node));
    >
    > instead of
    >
    > node * nod=(node *) malloc( sizeof(node));
    >
    > but when i use the upper way my gcc compiler shows error like
    >
    > invalid conversion from void* to node*
    >
    > whats the matter ?


    Does it show an error *like* "invalid conversion from void* to node*",
    or does it show that exact error?

    I might expect to see an error message about converting from int to
    node* if you forgot the mandatory "#include <stdio.h>".

    If that doesn't help, read question 7.6 of the comp.lang.c FAQ,
    <http://www.c-faq.com/>, and in the future please check the FAQ before
    posting. If that doesn't help, read the rest of section 7, and
    perhaps the entire FAQ. And if *that* doesn't help, feel free to post
    again -- but *please* copy-and-paste your actual complete program and
    the exact error message.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jul 25, 2008
    #2
    1. Advertising

  3. On Jul 25, 4:10 pm, Keith Thompson <> wrote:
    > mohi <> writes:
    > > as i have noted a lot many times it has been mentioned that we shudn't
    > > cast pointers returned by malloc()
    > > i mean
    > > its been said that use
    > > node * nod=malloc(sizeof(node));

    >
    > > instead of

    >
    > > node * nod=(node *) malloc( sizeof(node));

    >
    > > but when i use the upper way my gcc compiler shows error like

    >
    > > invalid conversion from void* to node*

    >
    > > whats the  matter ?

    >
    > Does it show an error *like* "invalid conversion from void* to node*",
    > or does it show that exact error?
    >
    > I might expect to see an error message about converting from int to
    > node* if you forgot the mandatory "#include <stdio.h>".
    >


    You mean <stdlib.h>...

    -David
     
    David Resnick, Jul 25, 2008
    #3
  4. mohi

    CBFalconer Guest

    mohi wrote:
    >
    > as i have noted a lot many times it has been mentioned that we
    > shudn't cast pointers returned by malloc(). i mean its been
    > said that use: node * nod=malloc(sizeof(node));
    > instead of: node * nod=(node *) malloc( sizeof(node));
    >
    > but when i use the upper way my gcc compiler shows error like
    > invalid conversion from void* to node*
    >
    > whats the matter ?


    You are using a C++ compiler, not a C compiler. Probably because
    you have misnamed the source file, which should be of type .c
    (lower case).

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Jul 25, 2008
    #4
  5. mohi

    mohi Guest

    On Jul 26, 1:14 am, David Resnick <> wrote:
    > On Jul 25, 4:10 pm, Keith Thompson <> wrote:
    >
    >
    >
    > > mohi <> writes:
    > > > as i have noted a lot many times it has been mentioned that we shudn't
    > > > cast pointers returned by malloc()
    > > > i mean
    > > > its been said that use
    > > > node * nod=malloc(sizeof(node));

    >
    > > > instead of

    >
    > > > node * nod=(node *) malloc( sizeof(node));

    >
    > > > but when i use the upper way my gcc compiler shows error like

    >
    > > > invalid conversion from void* to node*

    >
    > > > whats the matter ?

    >
    > > Does it show an error *like* "invalid conversion from void* to node*",
    > > or does it show that exact error?

    >
    > > I might expect to see an error message about converting from int to
    > > node* if you forgot the mandatory "#include <stdio.h>".

    >
    > You mean <stdlib.h>...
    >
    > -David


    sorry guys im really sorry
    i did compile it with g++


    it becomes so confusing when u have to use both c and c++
    is there a better way of dealing with this confusion
    :)
    mohan
     
    mohi, Jul 25, 2008
    #5
  6. David Resnick <> writes:
    > On Jul 25, 4:10 pm, Keith Thompson <> wrote:

    [...]
    >> I might expect to see an error message about converting from int to
    >> node* if you forgot the mandatory "#include <stdio.h>".
    >>

    >
    > You mean <stdlib.h>...


    Yes, thanks.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jul 25, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J.T. Conklin
    Replies:
    1
    Views:
    455
    David Hilsee
    Aug 11, 2004
  2. Andy
    Replies:
    5
    Views:
    522
    Shezan Baig
    Jan 30, 2005
  3. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,102
    Dan Pop
    Feb 10, 2004
  4. EvilRix
    Replies:
    8
    Views:
    668
    Martin Dickopp
    Feb 14, 2004
  5. Replies:
    1
    Views:
    602
    Salt_Peter
    Dec 25, 2006
Loading...

Share This Page