当前位置:网站首页>Unknown Bounded Array
Unknown Bounded Array
2022-08-01 03:02:00 【编程小段】
有两个文件,一个文件是数组的声明,另一个是数组的定义。如果数组的定义发生变化,比如说变成了含有5个元素的数组,那么相关联的声明也必须改变。一旦文件变多则会有部分文件忘记修改,就会发生意想不到的问题。
int array[4] = {
1, 2, 3, 4 };
#include<iostream>
extern int array[4];
int main()
{
……
return 0;
}
有的人想出一种解决办法,声明成指针。因为数组会隐式类型转换为指针,但这种解决方法会出现一个段错误(如果对array指针解引用的话),我们先看程序,顺便把各元素的地址打印出来:
#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;
}

很显然,第一个地址才符合C++语言的地址形式。那么为什么会出现这种问题呢?
首先这个不是编译错误,因为各文件单独编译都没有问题;其次也不是链接问题(类型是在编译器的概念,链接中是没有类型的,只有符号),array符号一致;因此它是一个运行错误。因为指针是64位的,所以它会把数组的前两个元素看成指针(由于大小端原因),而直接转换的地址是无效的,所以解引用会发生段错误。

正确的做法是使用Unknown Bounded Array(C++专门的术语叫不完整类):
#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 << std::endl;
return 0;
}
边栏推荐
- 【分层强化学习】HIRO:Data-Efficient Hierarchical Reinforcement Learning
- MYSQL Keyword Explain Analysis
- 机器学习初学者可以学哪些实战项目?
- Completely closed Chrome updated and in the top right corner of the tip
- TCP协议详解
- IDEA无法识别module(module右下角没有蓝色小方块)
- You need to know the TCP wave four times
- 内核的解压缩过程详解
- HCIP(14)
- MYSQL query interception optimization analysis
猜你喜欢

You need to know the TCP wave four times
![leetcode: 1562. Find latest grouping of size M [simulation + endpoint record + range merge]](/img/72/f52b5d3dcdb271f096c3e5108f0042.png)
leetcode: 1562. Find latest grouping of size M [simulation + endpoint record + range merge]

Flutter “Hello world“ 程代码

Device tree - conversion from dtb format to struct device node structure
![[cellular automata] based on matlab interface aggregation cellular automata simulation [including Matlab source code 2004]](/img/7b/831998e19591ce8b90cfb2eefae0d4.png)
[cellular automata] based on matlab interface aggregation cellular automata simulation [including Matlab source code 2004]

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

MYSQL two-phase commit

MYSQL Keyword Explain Analysis

Raspberry pie arm version of GCC installed configuration and environment variables

Which interpolation is better for opencv to zoom in and out??
随机推荐
MYSQL Index Analysis
Beijing suddenly announced that yuan universe big news
RTL8762DK uses DebugAnalyzer (four)
Fat interface in JQESAP system
leetcode6132. 使数组中所有元素都等于零(简单,周赛)
The IDEA can't find or unable to load The main class or Module "*" must not contain The source root "*" The root already belongs to The Module "*"
ARM 交叉编译
元宇宙改变人类工作模式的四种方式
Data Middle Office Construction (VII): Data Asset Management
By CSDN, torn
内核的解压缩过程详解
彻底关闭Chrome浏览器更新及右上角的更新提示
The device node structure is converted into a platform_device structure
Flink 部署和提交job
大佬们,MySQL cdc source在增量过程中回收 replication slave 和 r
How to get started with YOLO?How to implement your own training set?
Four ways the Metaverse is changing the way humans work
IDEA debugging
OSD read SAP CRM One Order application log way of optimization
gateway gateway cross domain