当前位置:网站首页>Unknown Bounded Array
Unknown Bounded Array
2022-08-01 03:17:00 【programming snippet】
有两个文件,A file is a declaration of an array,The other is the definition of the array.If the definition of the array changes,For example, it has become contained5个元素的数组,Then the associated statement must also be changed.Once there are more files, some files will be forgotten to be modified,Unexpected problems will occur.
int array[4] = {
1, 2, 3, 4 };
#include<iostream>
extern int array[4];
int main()
{
……
return 0;
}
Some people come up with a solution,declared as a pointer.Because arrays are implicitly type converted to pointers,But this workaround gives a segfault(如果对arrayPointer dereference words),Let's look at the program first,By the way, the address of each element is printed out:
#include<iostream>
int array[4] = {
1, 2, 3, 4 };
void func()
{
std::cout << array << std::endl;
}
#include<iostream>
void func();
extern int* array;
int main()
{
func();
//std::cout << array[1] << std::endl;
std::cout << array << std::endl;
return 0;
}
很显然,Only the first address matchesC++The address form of the language.那么为什么会出现这种问题呢?
First of all this is not a compile error,Because each file is compiled separately without any problem;Second is not a link problem(Types are a compiler concept,There is no type in the link,只有符号),array符号一致;So it is a runtime error.因为指针是64位的,So it will treat the first two elements of the array as pointers(due to endianness),Directly translated addresses are invalid,So dereferencing will segfault.
正确的做法是使用Unknown Bounded Array(C++The specialized term is called incomplete class):
#include<iostream>
int array[4] = {
1, 2, 3, 4 };
void func()
{
std::cout << array << std::endl;
}
#include<iostream>
void func();
extern int array[];//Statements can be written like this,But definitions can't
int main()
{
func();
std::cout << array << std::endl;
return 0;
}
边栏推荐
- Chain programming, packages, access
- 设备树——dtb格式到struct device node结构体的转换
- 移动端页面秒开优化总结
- ARM cross compilation
- Talking about hardware device computing storage and data interaction
- Daily practice of LeetCode - Circular linked list question (interview four consecutive questions)
- Flutter “Hello world“ 程代码
- Input输入框光标在前输入后自动跳到最后面的bug
- MYSQL query interception optimization analysis
- TypeScript简化运行之ts-node
猜你喜欢
New York University et al | TM-Vec: Template Modeling Vectors for Rapid Homology Detection and Alignment
MYSQL Classic Interview Questions
普通用户无法访问hgfs目录
How to get started with YOLO?How to implement your own training set?
IDEA 找不到或无法加载主类 或 Module “*“ must not contain source root “*“ The root already belongs to module “*“
Compiled on unbutu with wiringPi library and run on Raspberry Pi
每周小结(*67):为什么不敢发表观点
【Make YOLO Great Again】YOLOv1-v7全系列大解析(Neck篇)
[cellular automata] based on matlab interface aggregation cellular automata simulation [including Matlab source code 2004]
Four ways the Metaverse is changing the way humans work
随机推荐
初出茅庐的小李第114篇博客项目笔记之机智云智能浇花器实战(3)-基础Demo实现
IDEA does not recognize the module (there is no blue square in the lower right corner of the module)
Inheritance Considerations
Flutter "Hello world" program code
This map drawing tool is amazing, I recommend it~~
大佬们,MySQL cdc source在增量过程中回收 replication slave 和 r
The fledgling Xiao Li's 114th blog project notes: Wisdom cloud intelligent flower watering device combat (3) - basic Demo implementation
MYSQL transactions
Replacing the Raspberry Pi Kernel
【元胞自动机】基于matlab界面聚合元胞自动机模拟【含Matlab源码 2004期】
How to download the Keil package
Solve the problem that Excel opens very slowly after installing MySQL
Raspberry pie arm version of GCC installed configuration and environment variables
预言机简介
IDEA modifies the annotation font
TypeScript简化运行之ts-node
Elastic Stack的介绍
MYSQL Keyword Explain Analysis
Nmap manuals - the full version
Introduction to machine learning how to?