C语言 声明大型全局数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6514651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:01:51  来源:igfitidea点击:

Declare large global array

carrays

提问by Lang Jas

What is the best practice for declaring large, global arrays in C? For example, I want to use myArraythroughout the application. However, I lose the use of sizeof() if I do this:

在 C 中声明大型全局数组的最佳实践是什么?例如,我想在myArray整个应用程序中使用。但是,如果我这样做,我将失去 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.

回答by LavaSlider

You could define the size:

您可以定义大小:

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

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

Then sizeofshould work, but you could also just #definethe 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.

这将在编译时允许未知大小。如果在编译时已知大小,则仅存储常量将节省计数开销。

回答by R.. GitHub STOP HELPING ICE

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;

回答by Wolph

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语言中无非是一块内存,它不知道数组中有多少项,除了看指针的类型,它甚至不知道数组中单个项的大小*.

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).

*正如一些人指出的那样,当然这并不完全正确(但仍然是 imho 编程的好规则)。

回答by vharry85

It depends on whether it's a static or dynamic array. Only if it's a static array can you use sizeof().

这取决于它是静态数组还是动态数组。只有当它是静态数组时,您才能使用sizeof().

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

回答by caf

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_lenis 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-qualified 变量,而不是一个常量表达式,这意味着你不能用它来做一些事情,比如用静态存储持续时间初始化对象,或者定义非可变修改数组的大小)。