[출처] malloc() warning in gcc|작성자 엉겁결
gcc 컴파일시malloc() 함수 warning
아래 문서의 개략적 내용
#include <stdlib.h> 를 추가하라.
명시되어있지 않은함수의 return type은 int 이고
실제 malloc()함수는 void* 리턴타입을 가지기 때문에
incompatible implict declaration Warning 발생
malloc() warning 관련 내용보다 부가적인 내용이 더 많은데
알아둘만한 내용인것 같다.
정리하자면
int main() 보다는 int main(void) 명시하는것이 낫다.
char* p; 보다는 char *p; 코딩 스타일이 낫다.( char *p,q; 일때 p는 char포인터 , q는 char 타입이다 )
malloc()함수 앞에 타입캐스팅은 따로 필요 없다. (컴파일러가 알아서 해준다.)
malloc(10 * sizeof(*p)); 에서 sizeof(*p) 는 허용되는 표현이다.(*p의 주소값의 크기가 아니라 *p의 타입의 크기를 알려준다)
return(0) 을 쓸때 괄호는 필요 없다. (return은 함수가 아니기 때문이다. 괄호를 쓴다해도 일반적인 괄호의 의미외에는 없다.)
출처: http://newsgroups.derkeiler.com/Archive/Comp/comp.lang.c.moderated/2005-10/msg00019.html
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?
'UNIX_LINUX_C_C++' 카테고리의 다른 글
[FreeTDS] FreeTDS를 이용한 UNIX에서 ODBC(MSSQL)이용하기 (0) | 2011.10.16 |
---|---|
make예제 (0) | 2011.10.16 |
token 함수를 만들어서 사용해보자 (0) | 2011.10.16 |
구조체, 배열, 포인트변수 설명 (0) | 2011.10.16 |
네크워크 정보가져오기 (0) | 2011.10.16 |