warning: incompatible implicit declaration of built-in function 'malloc'??

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

    #1

    warning: incompatible implicit declaration of built-in function 'malloc'??

    On a gentoo linux system i don't get any warnings when I comlpile this code:

    #include <stdio.h>
    typedef struct test
    {
    void *content;
    struct test_ *bob;

    } test_;

    int main(void)
    {

    test_ *tt;
    tt =(test_ *) malloc(sizeof(t est_));
    return 0;
    }

    But on my home Ubuntu box I get this warning:

    mos@ubuntu:~/lab1$ gcc test2.c -o test2
    test2.c: In function 'main':
    test2.c:16: warning: incompatible implicit declaration of built-in function
    'malloc'
    mos@ubuntu:~/lab1$


    What does this warning mean and why does it appear on one system while it
    does not appear on another?

  • Vladimir S. Oka

    #2
    Re: warning: incompatible implicit declaration of built-in function 'malloc'??

    Paminu wrote:[color=blue]
    > On a gentoo linux system i don't get any warnings when I comlpile this code:[/color]

    Good example of why casting the result of malloc() is a bad idea. It
    masks the problem you have below...
    [color=blue]
    > #include <stdio.h>[/color]

    You need:

    #include <stdlib.h>

    as well, if you want to use malloc().
    [color=blue]
    > typedef struct test
    > {
    > void *content;
    > struct test_ *bob;
    >
    > } test_;
    >
    > int main(void)
    > {
    >
    > test_ *tt;
    > tt =(test_ *) malloc(sizeof(t est_));
    > return 0;
    > }
    >
    > But on my home Ubuntu box I get this warning:
    >
    > mos@ubuntu:~/lab1$ gcc test2.c -o test2
    > test2.c: In function 'main':
    > test2.c:16: warning: incompatible implicit declaration of built-in function
    > 'malloc'
    > mos@ubuntu:~/lab1$
    >
    > What does this warning mean and why does it appear on one system while it
    > does not appear on another?[/color]

    It means that you did not include <stdlib.h> and the compiler does not
    know about malloc(), and is therefore exercising its right to assume it
    returns and `int`. You're then warned about it, which is very nice on
    the part of the compiler, as it's not required to do so (since, by
    casting, you told it: "I know what I'm doing here").

    My guess is that you're using different gcc versions on your two
    machines, or at least different set of default options.

    --
    BR, Vladimir

    Comment

    • Martin Ambuhl

      #3
      Re: warning: incompatible implicit declaration of built-in function'malloc '??

      Paminu wrote (with snippage applied):[color=blue]
      > On a gentoo linux system i don't get any warnings when I comlpile this code:[/color]
      [Code including a call to malloc without including <stdlib.h> or
      otherwise providing an explicit declaration]:
      [color=blue]
      > tt =(test_ *) malloc(sizeof(t est_));[/color]
      [color=blue]
      > test2.c: In function 'main':
      > test2.c:16: warning: incompatible implicit declaration of built-in function
      > 'malloc'[/color]
      [color=blue]
      > What does this warning mean and why does it appear on one system while it
      > does not appear on another?[/color]

      malloc returns a void *, but by not providing a declaration you have
      implicitly declared it as returning an int. One of your compilers knows
      that the standard library function malloc returns a void * and notices
      that you have implicitly declared it otherwise; the other compiler
      doesn't have this information. Further, the unnecessary and bogus cast
      of an int to a pointer has masked your error of attempting to assign an
      int to a pointer and encouraged the compiler to ignore your error.

      Comment

      • Emmanuel Delahaye

        #4
        Re: warning: incompatible implicit declaration of built-in function'malloc '??

        Paminu a écrit :[color=blue]
        > What does this warning mean and why does it appear on one system while it
        > does not appear on another?[/color]

        In both cases, <stdlib.h>, where malloc() is prototyped, is missing.

        --
        A+

        Emmanuel Delahaye

        Comment

        • FlyingBird

          #5
          Re: warning: incompatible implicit declaration of built-in function 'malloc'??

          typedef struct test
          {
          void *content;
          struct test_ *bob;

          } test_;

          I don't understand why the struct can be written like this. More
          specifically, why doesn't the compiler report an error when it sees
          struct test_ *bob. test_ is a typedef a struct test. Can we add the
          keyword struct before test_? I read FAQ 1.14
          http://c-faq.com/decl/selfrefstruct.html but there is no such an
          example.

          Comment

          • Emmanuel Delahaye

            #6
            Re: warning: incompatible implicit declaration of built-in function'malloc '??

            FlyingBird a écrit :[color=blue]
            > typedef struct test
            > {
            > void *content;
            > struct test_ *bob;
            >
            > } test_;
            >
            > I don't understand why the struct can be written like this. More
            > specifically, why doesn't the compiler report an error when it sees
            > struct test_ *bob. test_ is a typedef a struct test. Can we add the
            > keyword struct before test_? I read FAQ 1.14
            > http://c-faq.com/decl/selfrefstruct.html but there is no such an
            > example.
            >[/color]
            It is perfecty legal to define an incomplete structure name loke this :

            struct name

            Being incomplete, the compiler don'sn't know what is the size of such a
            structure, hence an instanciation of such a type is illegal :

            struct name object; /* compile error */

            But, and this is the magic of C, it is possible to define a pointer to
            such a type.

            struct name *p_object;
            struct name const *p_object;

            This can be used to define :

            * a single pointer (not very useful),
            * an element of structure (useful for recursive structures like node of
            liked lists, trees etc.),
            * a function parameter (same behaviour that a void *, but typed, hence
            the ability of a type control by the compiler)

            This trick is used to implement ADT (Abstract Data Types) that have an
            'opaque' type from the outside (interface, user) and a well defined type
            in the inside (implementation )

            /* xxx.h */
            /* add guards... */

            /* public incomplete type */
            struct xxx;

            /* public functions */
            struct xxx *xxx_create (void);
            void xxx_delete (struct xxx *p_context);

            void xxx_function (struct xxx *p_context);

            /* xxx.c */
            #include "xxx.h"

            /* private complete type */
            struct xxx
            {
            int x;
            char *y;
            };

            /* public function */
            struct xxx *xxx_create (void)
            {
            struct xxx *this = malloc(sizeof *this);
            if (this != NULL)
            {
            /* clear */
            static const struct xxx z = {0};
            *this = z;
            }
            return this;
            }

            void xxx_delete (struct xxx *this)
            {
            free (this);
            }

            void xxx_function (struct xxx *this)
            {
            this->x = 123;
            }

            /* main.c */
            #include "xxx.h"

            #include <assert.h>

            int main (void)
            {
            struct xxx *p = xxx_create();

            if (p != NULL)
            {
            xxx_function(p) ;

            /* ... */

            /* end */
            xxx_delete(p), p = NULL;
            }

            assert (p == NULL);
            return 0;
            }

            --
            A+

            Emmanuel Delahaye

            Comment

            • Mark McIntyre

              #7
              Re: warning: incompatible implicit declaration of built-in function 'malloc'??

              On Tue, 07 Feb 2006 15:23:37 +0100, in comp.lang.c , Paminu
              <sdef@asd.com > wrote:
              [color=blue]
              >On a gentoo linux system i don't get any warnings when I comlpile this code:[/color]

              You need to increase warninglevels in your gentoo compiler.
              [color=blue]
              >#include <stdio.h>
              >typedef struct test
              >{
              > void *content;
              > struct test_ *bob;[/color]

              Where did you declare this type?
              [color=blue]
              > test_ *tt;
              > tt =(test_ *) malloc(sizeof(t est_));[/color]

              NEVER cast the return of malloc. It hides a serious error which in
              this case is the cause of your problems.
              [color=blue]
              >test2.c:16: warning: incompatible implicit declaration of built-in function[/color]

              this is the warning you should have got on gentoo.
              [color=blue]
              >What does this warning mean[/color]

              it means you forgot to include stdlib.h, which is a bad mistake.
              Mark McIntyre
              --
              "Debugging is twice as hard as writing the code in the first place.
              Therefore, if you write the code as cleverly as possible, you are,
              by definition, not smart enough to debug it."
              --Brian Kernighan

              ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
              http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
              ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

              Comment

              • FlyingBird

                #8
                Re: warning: incompatible implicit declaration of built-in function 'malloc'??

                Thanks for your perfect describing :-) I also found some useful
                discussions in another thread "initializi ng a pointer?".

                Comment

                Working...