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

Discussion in 'C Programming' started by Paminu, Feb 7, 2006.

  1. Paminu

    Paminu Guest

    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(test_));
    return 0;
    }

    But on my home Ubuntu box I get this warning:

    [email protected]:~/lab1$ gcc test2.c -o test2
    test2.c: In function 'main':
    test2.c:16: warning: incompatible implicit declaration of built-in function
    'malloc'
    [email protected]:~/lab1$


    What does this warning mean and why does it appear on one system while it
    does not appear on another?
     
    Paminu, Feb 7, 2006
    #1
    1. Advertisements

  2. Good example of why casting the result of malloc() is a bad idea. It
    masks the problem you have below...
    You need:

    #include <stdlib.h>

    as well, if you want to use malloc().
    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.
     
    Vladimir S. Oka, Feb 7, 2006
    #2
    1. Advertisements

  3. Paminu wrote (with snippage applied):
     
    Martin Ambuhl, Feb 7, 2006
    #3
  4. Paminu a écrit :
    In both cases, <stdlib.h>, where malloc() is prototyped, is missing.
     
    Emmanuel Delahaye, Feb 7, 2006
    #4
  5. Paminu

    FlyingBird Guest

    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.
     
    FlyingBird, Feb 7, 2006
    #5
  6. FlyingBird a écrit :
    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;
    }
     
    Emmanuel Delahaye, Feb 7, 2006
    #6
  7. You need to increase warninglevels in your gentoo compiler.
    Where did you declare this type?
    NEVER cast the return of malloc. It hides a serious error which in
    this case is the cause of your problems.
    this is the warning you should have got on gentoo.
    it means you forgot to include stdlib.h, which is a bad mistake.
    Mark McIntyre
     
    Mark McIntyre, Feb 7, 2006
    #7
  8. Paminu

    FlyingBird Guest

    Thanks for your perfect describing :) I also found some useful
    discussions in another thread "initializing a pointer?".
     
    FlyingBird, Feb 7, 2006
    #8
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.