当前位置:网站首页>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;
}
边栏推荐
- Simple vim configuration
- The kernel of the decompression process steps
- 对无限debugger的一种处理方式
- Compiled on unbutu with wiringPi library and run on Raspberry Pi
- MYSQL Classic Interview Questions
- 一个service层需要调用另两个service层获取数据,并组装成最后的数据,数据都是list,缓存如何设计?
- 初出茅庐的小李第114篇博客项目笔记之机智云智能浇花器实战(3)-基础Demo实现
- Flutter “Hello world“ 程代码
- MYSQL two-phase commit
- 解决IDEA默认情况下新建文件时,右击,new,没有XML文件的问题
猜你喜欢

The fledgling Xiao Li's 112th blog project notes: Wisdom cloud intelligent flower watering device actual combat (1) - basic Demo implementation

How is the tree structure of the device tree reflected?

解决IDEA默认情况下新建文件时,右击,new,没有XML文件的问题

普通用户无法访问hgfs目录

彻底关闭Chrome浏览器更新及右上角的更新提示

Four ways the Metaverse is changing the way humans work

Open source project site must-have & communication area function

MYSQL query interception optimization analysis

HCIP(15)

软件测试周刊(第82期):其实所有纠结做选择的人心里早就有了答案,咨询只是想得到内心所倾向的选择。
随机推荐
软件测试周刊(第82期):其实所有纠结做选择的人心里早就有了答案,咨询只是想得到内心所倾向的选择。
Basic usage concepts of vim
One service layer needs to call the other two service layers to obtain data and assemble it into the final data. The data is all lists. How to design the cache?
Dart named parameter syntax
HIRO: Hierarchical Reinforcement Learning 】 【 Data - Efficient Hierarchical Reinforcement Learning
MYSQL Classic Interview Questions
gateway gateway cross domain
【元胞自动机】基于matlab界面聚合元胞自动机模拟【含Matlab源码 2004期】
785. 快速排序
After specifying set 'execution.savepoint.path', restart flinksql and report this error
IDEA修改注释字体
C string array reverse
Replacing the Raspberry Pi Kernel
The bigger and bigger the project is, I split it like this
MYSQL master-slave replication
win10 fixed local IP
Chinese version of Pylint inspection rules
普通用户无法访问hgfs目录
Daily practice of LeetCode - Circular linked list question (interview four consecutive questions)
初出茅庐的小李第113篇博客项目笔记之机智云智能浇花器实战(2)-基础Demo实现