当前位置:网站首页>C compiler - implicit function declaration
C compiler - implicit function declaration
2022-06-29 07:54:00 【qazw9600】
Introduction
- For beginners C Language programming , Sometimes there is an abnormal crash , as follows :
* test.c
#include <stdio.h>
int main(){
printf("%s\n", test());
return 0;
}
* xxx.c
char *test(){
return "hello world";
}
* compile
gcc test.c xxx.c -o test
* perform
xxx:~/demo$ ./test
Segment error ( The core has been dumped )
Question why
- stay C In language , If the function is not declared before the call , The compiler will not report errors , Just a warning , If the function is undefined , The error prompt function will be reported when linking undefined.
- If there is no function declaration , How does the compiler know the number of parameters and the return type ? The compiler compiles with a single code file as the compilation unit , Function not declared , The compiler cannot know the number of parameters of a function , The function definition can only be found when linking , The compiler will follow the default rules , For the calling function C Code generates assembly code .
Implicit function declaration
- The compiler's default rule is implicit function declarations , If the function is not declared , The compiler makes function calls according to the following declaration by default .
int function();
Possible problems
- If the implicit function declaration may differ from the real definition , May cause an exception , And some anomalies are not easy to find .
- The return type in the function definition is not int
- Such as the example in the primer , The real return is char * type , Convert to according to the implicit function declaration int after , Last call printf Access the value as the memory address of the string , You are visiting a wrong address , So it causes the program to crash .
- If the real return is long type , convert to int Data may be truncated after .
- The parameter type in the function definition does not match the call
- for example :
* test.c
#include <stdio.h>
int main(){
test(1)
return 0;
}
* xxx.c
int test(char *a){
printf("%s\n", a);
return 0;
}
- The above program will crash after compilation .
Problem avoidance
- To avoid this problem , Programmers need to heed the compiler's warnings about implicit declarations , Eliminate this warning in a timely manner by including the necessary header files .
- In some cases ,gcc The compiler doesn't even prompt a warning , Use C Linguistic C99 In the version , Warnings will be given anyway , and C++ The compiler is more stringent , Implicit function declarations are no longer supported , If the function is not declared , Will report an error directly .
边栏推荐
- 音视频开发案例99讲-目录
- Behaviortree in ros2
- Cv:: mat and Base64 conversion (including picture compression and decompression)
- PostgreSQL安装:The database cluster initialisation failed,stack Hbulider安装
- qtcreator设置字符集
- From XX import* is equivalent to from XX import *, and no space is required
- Fluent imitates uiswitch
- 面试官:为什么数据库连接很消耗资源,资源都消耗在哪里?
- 【量化投资系统】问题记录及解决方法
- 基础知识 - 语法标准(ANSI C、ISO C、GNU C)
猜你喜欢

Explanation of swing transformer theory

AI and the meta universe sparked a spark: human beings lost only shackles and gained all-round liberation

【工控老马】基于PLC的花样喷泉设计原理详解

PostgreSQL安装:The database cluster initialisation failed,stack Hbulider安装

Prompt during packaging: property 'sqlsessionfactory' or 'sqlsessiontemplate'‘

从Nacos客户端谈Nacos配置中心

【修复收藏功能、更新登录接口】知识付费小程序、博客小程序、完整版开源源码、资源变现小程序,带299整站资源数据

How to permanently set Mysql to utf8 encoding?

Vibration signal generation and processing based on MATLAB Doppler effect

4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
随机推荐
MIPS instruction set and brief analysis
手撕二叉搜索树(Binary Search Tree)
from xx import*等价于from xx import *,不一定要加空格
基础语法 - 位运算
js实现图片懒加载的一个详细方案(引入即可使用)
1032 Sharing
Simulation analysis of sailing dynamics
Cartographer中的线程池操作
软重启(reboot)
指针引用数组元素
498. diagonal traversal (simulation)
Detailed explanation of top and free commands
VMware vcenter/ESXI系列漏洞总结
Appium environment setup
SQL 注入绕过(六)
Vulnhub's dc6 target
C#Mqtt订阅消息
Dump (cl\alv\tree\base================================cp|set\items\for\column) when expanding node or clicking toolbar button
Viewing application and installation of Hana database license
Behaviortree in ros2