I'm aware that in C it's best practice to never cast the return value of malloc()
. I've read that the compiler assumes that malloc()
returns an int if you don't include stdlib.h
. Of course it would produce an error if you tried to implicitly assign an int to something that's not an int, but that error could be covered up by an explicit cast -- hence the danger of explicitly casting malloc()
.
我知道在C语言中,最好不要强制转换malloc()的返回值。我已经读过,如果不包含stdlib.h,编译器会假定malloc()返回一个int。当然,如果你试图隐式地将int分配给不是int的东西,那么它会产生错误,但是这个错误可能会被显式强制转换所掩盖 - 因此显式地转换malloc()的危险。
For any function I've created, if the function doesn't exist then the compiler will tell me so. Why does the compiler assume that malloc()
returns int even if you haven't included stdlib.h
? Shouldn't malloc()
just remain undefined until you include stdlib.h
?
对于我创建的任何函数,如果该函数不存在,那么编译器会告诉我。为什么编译器假设malloc()返回int,即使你没有包含stdlib.h?在包含stdlib.h之前,malloc()是否应该保持未定义状态?
4 个解决方案
#1
Actually, if the compiler hasn't seen a declaration for any function you call (not just malloc) it will assume it is extern
and returns an int
. Most compilers I've used only give a warning for this, not an error, unless you turn up the warning level.
实际上,如果编译器没有看到你调用的任何函数的声明(不仅仅是malloc),它将假定它是extern并返回一个int。我使用的大多数编译器只会发出警告,而不是错误,除非你调高警告级别。
This goes back to the early days of C, I don't think this is allowed in C99.
这可以追溯到C的早期,我不认为在C99中这是允许的。
@Michael's comment: You seem to be correct, according to K&R (page 72):
@ Michael的评论:根据K&R(第72页),你似乎是正确的:
If a name that has not been previously declared occurs in an expression and is followed by a left parenthesis, it is declared by context to be a function name, the function is assumed to return an int, and nothing is assumed about its arguments.
如果先前未声明的名称出现在表达式中并且后跟左括号,则上下文将其声明为函数名称,假定该函数返回int,并且不假设其参数。
#2
Like @Burkhard said, it's the default. The compiler will spit out a warning if you haven't defined a prototype for the function, and it will assume the function returns an int. Then, if you really haven't defined it, the linker will be the one to give you an immediately-stopping error.
就像@Burkhard说的那样,这是默认的。如果您没有为函数定义原型,编译器将发出警告,并假设函数返回一个int。然后,如果您确实没有定义它,链接器将是一个给您立即停止错误的链接器。
#3
It's the default for every function.
这是每个功能的默认设置。
My compiler tells me, if I didn't define a function, that int testFunction()
is not defined.
我的编译器告诉我,如果我没有定义一个函数,那么就没有定义int testFunction()。
#4
All functions are assumed to return int
by default unless otherwise specified.
除非另有说明,否则假定所有函数都默认返回int。
See here.
#1
Actually, if the compiler hasn't seen a declaration for any function you call (not just malloc) it will assume it is extern
and returns an int
. Most compilers I've used only give a warning for this, not an error, unless you turn up the warning level.
实际上,如果编译器没有看到你调用的任何函数的声明(不仅仅是malloc),它将假定它是extern并返回一个int。我使用的大多数编译器只会发出警告,而不是错误,除非你调高警告级别。
This goes back to the early days of C, I don't think this is allowed in C99.
这可以追溯到C的早期,我不认为在C99中这是允许的。
@Michael's comment: You seem to be correct, according to K&R (page 72):
@ Michael的评论:根据K&R(第72页),你似乎是正确的:
If a name that has not been previously declared occurs in an expression and is followed by a left parenthesis, it is declared by context to be a function name, the function is assumed to return an int, and nothing is assumed about its arguments.
如果先前未声明的名称出现在表达式中并且后跟左括号,则上下文将其声明为函数名称,假定该函数返回int,并且不假设其参数。
#2
Like @Burkhard said, it's the default. The compiler will spit out a warning if you haven't defined a prototype for the function, and it will assume the function returns an int. Then, if you really haven't defined it, the linker will be the one to give you an immediately-stopping error.
就像@Burkhard说的那样,这是默认的。如果您没有为函数定义原型,编译器将发出警告,并假设函数返回一个int。然后,如果您确实没有定义它,链接器将是一个给您立即停止错误的链接器。
#3
It's the default for every function.
这是每个功能的默认设置。
My compiler tells me, if I didn't define a function, that int testFunction()
is not defined.
我的编译器告诉我,如果我没有定义一个函数,那么就没有定义int testFunction()。
#4
All functions are assumed to return int
by default unless otherwise specified.
除非另有说明,否则假定所有函数都默认返回int。
See here.