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

P

Paminu

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:

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?
 
V

Vladimir S. Oka

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

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

You need:

#include <stdlib.h>

as well, if you want to use malloc().
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:

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?

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.
 
M

Martin Ambuhl

Paminu wrote (with snippage applied):
tt =(test_ *) malloc(sizeof(test_));
test2.c: In function 'main':
test2.c:16: warning: incompatible implicit declaration of built-in function
'malloc'
What does this warning mean and why does it appear on one system while it
does not appear on another?

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.
 
E

Emmanuel Delahaye

Paminu a écrit :
What does this warning mean and why does it appear on one system while it
does not appear on another?

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

FlyingBird

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.
 
E

Emmanuel Delahaye

FlyingBird a écrit :
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.
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;
}
 
M

Mark McIntyre

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

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

Where did you declare this type?
test_ *tt;
tt =(test_ *) malloc(sizeof(test_));

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

this is the warning you should have got on gentoo.
What does this warning mean

it means you forgot to include stdlib.h, which is a bad mistake.
Mark McIntyre
 
F

FlyingBird

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

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top