简体   繁体   English

声明大型全局数组

[英]Declare large global array

What is the best practice for declaring large, global arrays in C?在 C 中声明大型全局 arrays 的最佳做法是什么? For example, I want to use myArray throughout the application.例如,我想在整个应用程序中使用myArray However, I lose the use of sizeof() if I do this:但是,如果我这样做,我将失去对 sizeof() 的使用:

// file.c
char* myArray[] = { "str1", "str2", ... "str100" };

//file.h
extern char* myArray[];


// other_file.c
#include "file.h"
size_t num_elements = sizeof( myArray ); // Can determine size of incomplete type.

You could define the size:您可以定义大小:

// file.c
char *myArray[100] = { "str1", "str2", ... "str100" };

// file.h
extern char *myArray[100];

Then sizeof should work, but you could also just #define the size or set a variable.然后sizeof应该可以工作,但您也可以只#define大小或设置一个变量。

Another options is to count up the length and store it one time in your code...另一种选择是计算长度并将其存储在您的代码中一次......

// file.c
char *myArray[] = { "str1", "str2", ... "strN", (char *) 0 };
int myArraySize = -1;
int getMyArraySize() {
   if( myArraySize < 0 ) {
      for( myArraySize = 0; myArray[myArraySize]; ++myArraySize );
   }
   return myArraySize;
 }


// file.h
extern char *myArray[];
int getMyArraySize();

This would allow an unknown size at compile time.这将在编译时允许未知大小。 If the size is known at compile time just storing the constant will save the overhead of counting.如果在编译时知道大小,则仅存储常量将节省计数的开销。

In C you need to store the size separately (possibly in a struct with the array).在 C 中,您需要单独存储大小(可能在带有数组的结构中)。

An array is nothing more than a block of memory in C, it does not know how much items there are in the array and besides by looking at the type of the pointer, it does not even know the size of a single item in the array * .数组只不过是C中的一块memory,它不知道数组中有多少项,除了看指针的类型,它甚至不知道数组中单个项的大小* .

If you really want to use a global array, than I would recommend something like this:如果你真的想使用全局数组,我会推荐这样的东西:

#define ARRAY_SIZE 1000
char* myArray[ARRAY_SIZE] = {....};

* As some people have pointed out, this isn't completely true ofcourse (but still a good rule to program by imho). *正如一些人所指出的,这当然不是完全正确的(但仍然是恕我直言编程的好规则)。

I think this is the solution you're looking for:我认为这是您正在寻找的解决方案:

// file.c
char* myArray[] = { "str1", "str2", ... "str100" };
const size_t sizeof_myArray = sizeof myArray;

//file.h
extern char* myArray[];
extern const size_t sizeof_myArray;

It depends on whether it's a static or dynamic array.这取决于它是 static 还是动态数组。 Only if it's a static array can you use sizeof() .只有当它是一个 static 数组时,你才能使用sizeof()

#define MAX_ELE (100)
const static char* MyArray[] = {"str1","str2",....,"str100"}

In file.h , you can put:file.h中,您可以输入:

#include <stddef.h>

extern char *myArray[];
extern const size_t myArray_len;

Then in file.c , you have:然后在file.c中,您有:

#include "file.h"

char *myArray[] = { "str1", "str2", ... "str100" };
const size_t myArray_len = sizeof myArray / sizeof myArray[0];

This way your other modules can refer to myArray_len , and you do not need to explicitly write the size of the array anywhere.这样,您的其他模块可以引用myArray_len ,并且您无需在任何地方显式写入数组的大小。

(The downside is that myArray_len is merely a const -qualified variable, not a constant expression, which means that you can't use it to do things like initialise objects with static storage duration, or define the size of non-variably-modified arrays). (缺点是myArray_len只是一个const限定的变量,而不是一个常量表达式,这意味着您不能使用它来执行诸如初始化具有 static 存储持续时间的对象之类的事情,或者定义非可变修改数组的大小)。

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

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