简体   繁体   English

是否可以在C / C ++中声明全局2D数组?

[英]Is it possible to declare a global 2D array in C/C++?

When I try to declare a global two-dimensional array in C++ like so: 当我尝试在C++声明一个global二维数组时 ,如下所示:

int maxX = 10;
int maxZ = 10;
SDL_Rect mapX[maxX][maxZ];

I get an error that says 我得到一个错误说
error: variable-size type declared outside of any function

(At the first sight it looked to me as a "difference between C and C++ question". I could be mistaken though.) (乍一看,它看起来像是“C和C ++问题之间的区别”。虽然我可能会弄错。)

Since you used non-constant values as array sizes, you are attempting to declare a variable-length array (VLA). 由于您使用非常量值作为数组大小,因此您尝试声明可变长度数组(VLA)。 C++ does not support VLAs at all, while C supports only local VLAs. C ++根本不支持VLA,而C仅支持本地 VLA。 The latter is exactly what the compiler is telling you in that error message. 后者正是编译器在该错误消息中告诉您的内容。 (And I believe this error message came from a C compiler, since C++ compiler would give you a completely different error.) (我相信这个错误消息来自C编译器,因为C ++编译器会给你一个完全不同的错误。)

So, strictly speaking, you cannot declare such array either in C or in C++. 因此,严格地说,您无法在C或C ++中声明此类数组。 While C language supports VLAs, they still have to be local. 虽然C语言支持VLA,但它们仍然必须是本地的。 You are not allowed to declare VLAs with static storage duration in C. 您不能在C中声明具有静态存储持续时间的VLA。

In C++ all arrays have to have fixed pre-determined compile-time size. 在C ++中,所有数组都必须具有固定的预定编译时大小。 Which means that the array sizes have to be specified by compile-time constants. 这意味着数组大小必须由编译时常量指定。 You used non-constant values instead, which is what caused the error. 您使用非常量值,这是导致错误的原因。

In other words, in both C and C++ you are required to use constant expressions when specifying sizes for arrays with static storage duration (including so called "global" arrays). 换句话说,在C和C ++中,在为具有静态存储持续时间的数组(包括所谓的“全局”数组)指定大小时,需要使用常量表达式。

In C++ in order to make your sizes constant you have to declare them with const 在C ++中为了使你的大小保持不变,你必须用const声明它们

const int maxX = 10;
const int maxZ = 10;

In C this will not work, since in C const objects are not really constants in a sense that they don't form constant expressions . 在C中,这将不起作用,因为在C const对象在某种意义上并不是真正的常量,它们不会形成常量表达式 In C you'd have to use either 在C中你必须使用其中之一

#define maxX 10
#define maxZ 10

or 要么

enum {
  maxX = 10,
  maxZ = 10
};

In C++, you cannot use a variable to define the size of an array, since the compiler needs to know at compile time where to assign the array's memory and how much memory it needs. 在C ++中,您不能使用变量来定义数组的大小,因为编译器需要在编译时知道在何处分配数组的内存以及需要多少内存。 Declaring maxX and maxZ as int makes them variables, and therefore unsuitable for array dimensions. maxXmaxZ声明为int会使它们成为变量,因此不适合数组维度。 However, if you declare them as const int , they will then be contants, and the compiler will know that those values will be fixed over the execution of your program, and it will allow their use to define array dimensions. 但是,如果将它们声明为const int ,则它们将成为内容,编译器将知道这些值将在程序执行时得到修复,并允许它们用于定义数组维度。

Therefore, this doesn't work: 因此,这不起作用:

int maxX = 10;
int maxZ = 10;
SDL_Rect mapX[maxX][maxZ]; // Not allowed

But this is fine: 但这很好:

const int maxX = 10;
const int maxZ = 10;
SDL_Rect mapX[maxX][maxZ]; // Allowed
static const int maxX = 10;
static const int maxZ = 10;
SDL_Rect mapX[maxX][maxZ];

works fine for me. 对我来说很好。


Note 注意

It isn't generally good practise to use either globals, or raw arrays, or global raw arrays in general. 一般来说,使用全局变量或原始数组或全局原始数组通常不是好的做法。 Obviously I don't have enough context to comment on whether or not it is the right choice here. 显然,我没有足够的背景来评论它是否是正确的选择。


Historical Note 历史记录

Before modern constness, it was common to either enumerate or #define constants to make them work correctly in this situation. 在现代constness之前,通常枚举或#define常量使它们在这种情况下正常工作。

I do not think so this is the correct way to use SDL_rect. 我不这么认为这是使用SDL_rect的正确方法。

The syntax should be like 语法应该是这样的

SDL_Rect rect = {0,0,10,10}

So you can do the following; 所以你可以做到以下几点;

int maxX = 10;
int maxZ = 10;
SDL_Rect rect = {0,0,maxX,maxZ}

而不是C风格的数组,我建议使用std::vector

std::vector< std::vector<SDL_Rect> > mapX;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM