explicit cast to malloc()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mohi

    explicit cast to malloc()

    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(size of(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
  • Eric Sosman

    #2
    Re: explicit cast to malloc()

    mohi wrote:
    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(size of(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're compiling C++, not C. The comp.lang.c++ newsgroup
    is just down the hall to your left; I hear they're planning to
    have a party as soon as they find the template.

    --
    Eric.Sosman@sun .com

    Comment

    • William Pursell

      #3
      Re: explicit cast to malloc()

      On 25 Jul, 20:46, mohi <mohangupt...@g mail.comwrote:
      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(size of(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 ?
      Most likely you are invoking gcc as a c++ compiler.


      Comment

      • Keith Thompson

        #4
        Re: explicit cast to malloc()

        mohi <mohangupta13@g mail.comwrites:
        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(size of(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_Keit h) kst-u@mib.org <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"

        Comment

        • David Resnick

          #5
          Re: explicit cast to malloc()

          On Jul 25, 4:10 pm, Keith Thompson <ks...@mib.orgw rote:
          mohi <mohangupt...@g mail.comwrites:
          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(size of(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

          Comment

          • mohi

            #6
            Re: explicit cast to malloc()

            On Jul 26, 1:14 am, David Resnick <lndresn...@gma il.comwrote:
            On Jul 25, 4:10 pm, Keith Thompson <ks...@mib.orgw rote:
            >
            >
            >
            mohi <mohangupt...@g mail.comwrites:
            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(size of(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

            Comment

            • Eric Sosman

              #7
              Re: explicit cast to malloc()

              mohi wrote:
              [...]
              it becomes so confusing when u have to use both c and c++
              is there a better way of dealing with this confusion
              Sure:

              (setq use-C nil
              use-C++ nil
              use-lisp exclusively
              tongue-in-cheek t)

              --
              Eric.Sosman@sun .com

              Comment

              • Keith Thompson

                #8
                Re: explicit cast to malloc()

                David Resnick <lndresnick@gma il.comwrites:
                On Jul 25, 4:10 pm, Keith Thompson <ks...@mib.orgw rote:
                [...]
                >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_Keit h) kst-u@mib.org <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"

                Comment

                • CBFalconer

                  #9
                  Re: explicit cast to malloc()

                  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(size of(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.


                  Comment

                  • Greg Comeau

                    #10
                    Re: explicit cast to malloc()

                    In article <84799233-4bea-453e-8f32-a97c1626a015@s2 1g2000prm.googl egroups.com>,
                    mohi <mohangupta13@g mail.comwrote:
                    >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(size of(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*
                    Does it literally say void *, or does it say int?
                    If it literally says void * you're probably compiling in C++
                    mode not in C mode. If it says int, then, you're probably
                    not #include'ing the right header for malloc, that is, stdlib.h
                    --
                    Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
                    Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
                    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
                    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

                    Comment

                    Working...