473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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

Feb 7 '06 #1
7 90215
Paminu wrote:
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.

--
BR, Vladimir

Feb 7 '06 #2
Paminu wrote (with snippage applied):
On a gentoo linux system i don't get any warnings when I comlpile this code: [Code including a call to malloc without including <stdlib.h> or
otherwise providing an explicit declaration]:
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.
Feb 7 '06 #3
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.

--
A+

Emmanuel Delahaye
Feb 7 '06 #4
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.

Feb 7 '06 #5
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;
}

--
A+

Emmanuel Delahaye
Feb 7 '06 #6
On Tue, 07 Feb 2006 15:23:37 +0100, in comp.lang.c , Paminu
<sd**@asd.com> wrote:
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
--
"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 =----
Feb 7 '06 #7
Thanks for your perfect describing :-) I also found some useful
discussions in another thread "initializing a pointer?".

Feb 7 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: jerrygarciuh | last post by:
Hi all, I was just wondering what popular opinion is on PHP giving this warning: Warning: Invalid argument supplied for foreach() in /home/boogerpic/public_html/my.php on line 6 when...
9
by: sb | last post by:
If there is at least one user-defined constructor, no constructors are implicitly declared. struct my_vec : public vector<int> { double foo; my_vec(size_t n) : vector<int>(2*n) {} // oops, no...
4
by: DFP | last post by:
I am posting the gcc 3.3.2 compiler error and the source lines below them. Please explain this warning and how to fix it. crt/crtmap.hpp:17: warning: `std::map<K, D, C,...
1
by: Marcin Kalicinski | last post by:
Hi, The following code template<class Type> class Base::List: public std::list<Type> { public: explicit List(size_type count): std::list<Type>(count) { } // this is line 14 /*...*/
3
by: Šimon Tóth | last post by:
Can anybody tell me how to turn of this warning? -Wno-deprecated doesn't work. I don't want to edit the source file, because it works fine and it's a bit hardcore stuff for me :) warning:...
1
by: pmatos | last post by:
Hi all, I'm starting to use templates and I just got a weird error which I was able to simplify into this code: #include <vector> using std::vector; template<class T> class foo {
1
by: Josh Wilson | last post by:
Hey gang, So I have my stdin which is user defined file, and am wanting to write it to a temporary file (seems odd I know, but there is a sincere reason), and then use that file as the stdin for...
4
by: j0mbolar | last post by:
What is the chapter and verse for the following? #include <stdio.h> void foo(struct bar *baz); struct bar { int x, y; };
12
by: elliot.li.tech | last post by:
Hello everyone, I'm migrating a C++ program from 32-bit systems to 64-bit systems. The old programmers used some annoying assignments like assigning "long" to "int", which may lead to data loss....
20
by: Bill Cunningham | last post by:
I am stumped on this one. I tried two methods and something just doesn't seem right. I'll try my new syle. #include <stdio.h> #include <stdlib.h> main() { srand(2000); /*seed unsigned */...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.