Re: malloc warning gcc > 4.0
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Thu, 20 Oct 2005 06:53:19 -0000
mark <mark@xxxxxxxxxxx> writes:
> When I compile this code:
>
> int main() {
> char* p;
> p = (char*)malloc(10*sizeof(char));
> return(0);
> }
>
> with gcc 4.0.1, I get the warning:
> warning: incompatible implicit declaration of built-in function 'malloc'
>
> What is wrong? I know it works with gcc 3.3.6.
gcc 3.3.6 has a flaw, in that it failed to give you the warning. gcc
4.0.1 corrects the flaw. (It's not a flaw in the sense of violating
the language; the warning isn't required, but it's useful.)
Your error is in failing to add the line "#include <stdlib.h>" to the
top of your program. Without this directive, which brings in a
correct declaration of malloc(), the compiler assumes that malloc()
returns int rather than void*. gcc 4.0.1 happens to know that the
predefined malloc() returns void*, and is able to give you the
warning. (Presumably it wouldn't be able to issue the warning for a
function that's not predefined.)
That's not the only problem with your code.
"int main()" is acceptable, but "int main(void)" is better because
it's more explicit.
As a matter of style, "char *p;" is better than "char* p;" (though of
course the compiler doesn't care). C's declaration syntax needs to be
read from the inside out. The declaration
char *p;
in effect says that *p is of type char, so p is of type char*. This
matters if you declare multiple variables in a single declaration:
char *p, q;
declares p as a pointer to char, and q as a char. If you join the '*'
to the type name:
char* p, q;
it looks like p and q are both of type char*.
In "p = (char*)malloc(10*sizeof(char));", the cast is unnecessary and
ill-advised. malloc() returns a result of type void*, which can be
implicitly converted to any object pointer type. The cast hides the
failure to include <stdlib.h> (as you saw with gcc 3.3.6). In effect,
the cast tells the compiler, "Shut up, I know what I'm doing" -- even
if you don't.
sizeof(char) is by definition 1. So the line can be changed to
p = malloc(10);
or, if you might change the type of p later:
p = malloc(10 * sizeof *p);
In the latter case, we apply sizeof to *p rather than to the type so
we don't have to change the line if the type of p changes. (Don't
worry about dereferencing an uninitialized pointer here; the argument
to sizeof isn't evaluated unless it's a variable-length array.) Once
you get used to the idiom, it's more obvious at a glance that it's
correct. With sizeof(char), or sizeof(some_type), you have to go back
to the declaration of p to be sure you have the right type. With
p = malloc(10 * sizeof *p);
you're saying "Allocate an array of 10 of whatever type p points to",
which is exactly what you want.
In a real program, you'd want to check the result of malloc() and take
some action (possibly aborting the program) if it failed. You'd also
want to call free() to release the allocated memory. In a toy program
like this, that's not as important.
Finally, in
return(0);
the parentheses are unnecessary. The syntax of a return statement is
return <expression>;
A parenthesized expression is allowed, of course. The problem is that
it makes the return statement look like a function call. It's not
a function call, and it shouldn't look like one.
We don't often see a program that provides so much opportunity for
commentary in so few lines. Was it deliberate?
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
--
comp.lang.c.moderated - moderation address: clcm@xxxxxxxxxxxx -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
.
- Follow-Ups:
- Re: malloc warning gcc > 4.0
- From: Peter Pichler
- Re: malloc warning gcc > 4.0
- References:
- malloc warning gcc > 4.0
- From: mark
- malloc warning gcc > 4.0
- Prev by Date: Re: malloc warning gcc > 4.0
- Next by Date: Re: malloc warning gcc > 4.0
- Previous by thread: Re: malloc warning gcc > 4.0
- Next by thread: Re: malloc warning gcc > 4.0
- Index(es):
Relevant Pages
|