如何为固定长度C字符串的任意大小的数组分配内存

时间:2021-04-18 21:37:14

I am trying to allocate memory for an array of C strings. I can guarantee that the strings fit within MAX_STRING_LENGTH characters, but I don't know at compile time how many strings will be in the array (this is computed dynamically). When I use the code...

我正在为一个C字符串数组分配内存。我可以保证字符串适合MAX_STRING_LENGTH字符,但是我不知道在编译时数组中有多少字符串(这是动态计算的)。当我使用代码…

char *strings[MAX_STRING_LENGTH] = malloc( sizeof(char *) * numstrings );

...the compiler complains that this is an invalid initializer. When I use the code...

…编译器抱怨这是一个无效的初始化器。当我使用代码…

char strings[MAX_STRING_LENGTH][] = malloc( sizeof(char *) * numstrings );

...the compiler complains about an incomplete element type. What am I doing wrong here, and how can I allocate memory for this array?

…编译器会报告不完整的元素类型。我在这里做错了什么,我如何为这个数组分配内存?

4 个解决方案

#1


2  

char (*strings)[MAX_STRING_LENGTH] = malloc(sizeof *strings * num_strings);

will allocate a num_strings x MAX_STRING_LENGTH array of char as a contiguous chunk so that you don't have to do multiple levels of allocation.

将一个num_strings x MAX_STRING_LENGTH数组分配为一个连续的块,这样您就不必做多个级别的分配。

strcpy(strings[i], "This is a test");
printf("%s\n", strings[j]);

etc. When you're done you only have to free(strings);.

当你完成时,你只需要释放(弦);。

The main drawbacks with this method are that you may not have enough memory to satisfy the request if num_strings is very large, and that you'll have some internal fragmentation if most of your strings are shorter than MAX_STRING_LENGTH.

这种方法的主要缺点是,如果num_string非常大,您可能没有足够的内存来满足请求,并且如果您的大多数字符串都小于MAX_STRING_LENGTH,那么您将有一些内部碎片。

#2


2  

With this declaration:

这个声明:

char *strings[MAX_STRING_LENGTH] = malloc(sizeof(char *) * numstrings);

It reads as if you're defining an array of C strings, whose count - not the individual string lengths - is MAX_STRING_LENGTH.

它的读取方式就好像您正在定义一个C字符串数组,它的计数—而不是单个字符串长度—是MAX_STRING_LENGTH。

Better to define a **char, like so:

最好定义一个**char,如下所示:

char **strings = malloc(sizeof(char *) * numstrings);

Later in your code, when you're ready to add a string to e.g. slot 5:

在后面的代码中,当您准备为例5添加一个字符串时:

strings[5] = malloc(sizeof(char) * MAX_STRING_LENGTH));

or in an init loop such as:

或在init循环中,例如:

for (i = 0; i < numstrings; i++)
    strings[i] = malloc(sizeof(char) * MAX_STRING_LENGTH);

And, if at some point you need room for more strings, use realloc to grow the initial memory allocation.

而且,如果在某些时候需要为更多的字符串留出空间,可以使用realloc来增加初始内存分配。

#3


1  

char **strings;

strings=(char **) malloc(number of strings);

strings[i]=(char *) malloc(MAX_STRING_LENGTH);

#4


1  

If it is truly dynamic, then it may need to be something like this:

如果它真的是动态的,那么它可能需要这样:

char **strings = malloc( sizeof(char*) * numstrings );
for ( int i = 0; i < numstrings; i++ )  
    strings[i] = malloc( MAX_STRING_LENGTH );

It is also possible to allocate it all in one chunk (makes freeing it easier) and then assign the individual pointers in the array to computed positions in the allocated buffer. That, though, adds some complexity that may not exceed the benefit.

还可以将其全部分配到一个块中(使释放更容易),然后将数组中的单个指针分配到已分配缓冲区中的计算位置。不过,这也增加了一些可能不会超过好处的复杂性。

#1


2  

char (*strings)[MAX_STRING_LENGTH] = malloc(sizeof *strings * num_strings);

will allocate a num_strings x MAX_STRING_LENGTH array of char as a contiguous chunk so that you don't have to do multiple levels of allocation.

将一个num_strings x MAX_STRING_LENGTH数组分配为一个连续的块,这样您就不必做多个级别的分配。

strcpy(strings[i], "This is a test");
printf("%s\n", strings[j]);

etc. When you're done you only have to free(strings);.

当你完成时,你只需要释放(弦);。

The main drawbacks with this method are that you may not have enough memory to satisfy the request if num_strings is very large, and that you'll have some internal fragmentation if most of your strings are shorter than MAX_STRING_LENGTH.

这种方法的主要缺点是,如果num_string非常大,您可能没有足够的内存来满足请求,并且如果您的大多数字符串都小于MAX_STRING_LENGTH,那么您将有一些内部碎片。

#2


2  

With this declaration:

这个声明:

char *strings[MAX_STRING_LENGTH] = malloc(sizeof(char *) * numstrings);

It reads as if you're defining an array of C strings, whose count - not the individual string lengths - is MAX_STRING_LENGTH.

它的读取方式就好像您正在定义一个C字符串数组,它的计数—而不是单个字符串长度—是MAX_STRING_LENGTH。

Better to define a **char, like so:

最好定义一个**char,如下所示:

char **strings = malloc(sizeof(char *) * numstrings);

Later in your code, when you're ready to add a string to e.g. slot 5:

在后面的代码中,当您准备为例5添加一个字符串时:

strings[5] = malloc(sizeof(char) * MAX_STRING_LENGTH));

or in an init loop such as:

或在init循环中,例如:

for (i = 0; i < numstrings; i++)
    strings[i] = malloc(sizeof(char) * MAX_STRING_LENGTH);

And, if at some point you need room for more strings, use realloc to grow the initial memory allocation.

而且,如果在某些时候需要为更多的字符串留出空间,可以使用realloc来增加初始内存分配。

#3


1  

char **strings;

strings=(char **) malloc(number of strings);

strings[i]=(char *) malloc(MAX_STRING_LENGTH);

#4


1  

If it is truly dynamic, then it may need to be something like this:

如果它真的是动态的,那么它可能需要这样:

char **strings = malloc( sizeof(char*) * numstrings );
for ( int i = 0; i < numstrings; i++ )  
    strings[i] = malloc( MAX_STRING_LENGTH );

It is also possible to allocate it all in one chunk (makes freeing it easier) and then assign the individual pointers in the array to computed positions in the allocated buffer. That, though, adds some complexity that may not exceed the benefit.

还可以将其全部分配到一个块中(使释放更容易),然后将数组中的单个指针分配到已分配缓冲区中的计算位置。不过,这也增加了一些可能不会超过好处的复杂性。