简体   繁体   中英

Declare large global array

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

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

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

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

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. Only if it's a static array can you use sizeof() .

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

In file.h , you can put:

#include <stddef.h>

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

Then in file.c , you have:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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