当前位置:网站首页>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;
}
边栏推荐
- HIRO: Hierarchical Reinforcement Learning 】 【 Data - Efficient Hierarchical Reinforcement Learning
- 树莓派 的 arm 版的 gcc 安装 和环境变量的配置
- 二舅
- This map drawing tool is amazing, I recommend it~~
- 初出茅庐的小李第112篇博客项目笔记之机智云智能浇花器实战(1)-基础Demo实现
- Four implementations of
batch insert: have you really got it? - IDEA调试
- High dimensional Gaussian distribution basics
- 【SemiDrive源码分析】系列文章链接汇总(全)
- Dart named parameter syntax
猜你喜欢

【入门教程】Rollup模块打包器整合

The fledgling Xiao Li's 113th blog project notes: Wisdom cloud smart flower watering device combat (2) - basic Demo implementation

IDEA修改注释字体

软件测试周刊(第82期):其实所有纠结做选择的人心里早就有了答案,咨询只是想得到内心所倾向的选择。

HIRO: Hierarchical Reinforcement Learning 】 【 Data - Efficient Hierarchical Reinforcement Learning

纽约大学等 | TM-Vec:用于快速同源检测和比对的模版建模向量

Introduction to Oracle

普通用户无法访问hgfs目录

Basic implementation of vector

"Youth Pie 2": The new boyfriend stepped on two boats, and the relationship between Lin Miaomiao and Qian Sanyi warmed up
随机推荐
初出茅庐的小李第112篇博客项目笔记之机智云智能浇花器实战(1)-基础Demo实现
Replacing the Raspberry Pi Kernel
Introduction to machine learning how to?
Solve the problem that Excel opens very slowly after installing MySQL
When opening a MYSQL table, some can display editing, some do not, how to set.
HCIP(15)
解决安装MySQL后,Excel打开很慢的问题
button去除黑框
链式编程、包、访问权限
Four ways the Metaverse is changing the way humans work
Raspberry pie arm version of GCC installed configuration and environment variables
MYSQL transactions
You need to know the TCP wave four times
Data Middle Office Construction (VII): Data Asset Management
New York University et al | TM-Vec: Template Modeling Vectors for Rapid Homology Detection and Alignment
HIRO: Hierarchical Reinforcement Learning 】 【 Data - Efficient Hierarchical Reinforcement Learning
简单易用的任务队列-beanstalkd
Summary of MVCC
The bigger and bigger the project is, I split it like this
785. 快速排序