简体   繁体   English

在c中填充没有calloc和malloc的数组?

[英]populating array without calloc and malloc in c?

I have code that looks like我有看起来像的代码

int main(int argc, char *argv[]){
char ***matrix;
matrix = calloc(2, sizeof(char **));
for (int i=0; i<2; i++){
    matrix[i] = calloc(ROWS+2, sizeof(char*));
    for (int j=0; j<COLS; j++){
        matrix[i][j] = malloc(COLS+2);
        memset(matrix[i][j], (int)DEFAULT, COLS+2);
    }
}

Is there a way to do a similar kind of thing without the use of malloc and calloc?有没有办法在不使用 malloc 和 calloc 的情况下做类似的事情? For example, in the case of 1d array I know you can do something like this例如,在一维数组的情况下,我知道你可以做这样的事情

unsigned char malloc_data[MAX_SIZE];
size_t malloc_used;  /* automatically initialized to zero */

void *stack_malloc(size_t size) {
void *p = &malloc_data[malloc_used];
if(size + malloc_used > MAX_SIZE) return 0;  /* out of memory */
malloc_used += size;
return p;
}

OR something like this或者像这样的东西

static char *allocp = allocbuf[0];
if (allocbuf + ALLOCSIZE - allocp >= n) { /* it fits */ 

and

if (p >= allocbuf && p < allocbuf + ALLOCSIZE)

I want to try to use stack variables and temps instead of dynamic memory allocated ways.我想尝试使用堆栈变量和临时文件而不是动态内存分配方式。 However, when I try to think about how to apply these types of ways to my three dimensional sense, my head starts spinning.然而,当我试图考虑如何将这些类型的方法应用于我的三维感时,我的头开始旋转。 Please help clarify things to me请帮我澄清事情

Assuming you want to keep the triple pointer usage, the storage can be allocated on the stack and initialized like this:假设您想保留三重指针的使用,可以在堆栈上分配存储并像这样初始化:

char ***matrix;
char **mats_[2];
char *mat_rows_[2][ROWS+2];
char mat_cols_[2][ROWS+2][COLS+2];
matrix = &mats_[0];
for (int i = 0; i < 2; i++) {
    matrix[i] = &mat_rows_[i][0];
    for (int j = 0; j < ROWS+2; j++) {
        matrix[i][j] = &mat_cols_[i][j][0];
        memset(matrix[i][j], (int)DEFAULT, COLS+2);
    }
}

You could also define matrix as an array[2] of char ** instead of a single char *** , which would allow you to eliminate the mats_ variable:您还可以将matrix定义为char **的数组 [2] 而不是单个char *** ,这将允许您消除mats_变量:

char **matrix[2];
char *mat_rows_[2][ROWS+2];
char mat_cols_[2][ROWS+2][COLS+2];
for (int i = 0; i < 2; i++) {
    ...

In the comments below, OP asked whether the memset was necessary.在下面的评论中,OP 询问是否需要memset All the elements of mat_cols_[2][ROWS+2][COLS+2] need to be set to the same value DEFAULT . mat_cols_[2][ROWS+2][COLS+2]所有元素都需要设置为相同的值DEFAULT If DEFAULT can be replaced with 0 , this is easy to do by defining mat_cols_ with an initializer with pretty much everything defaulting to 0:如果DEFAULT可以用0替换,这很容易通过定义mat_cols_和一个几乎所有东西都默认为 0 的初始化程序来实现:

char mat_cols_[2][ROWS+2][COLS+2] = { 0 };

If DEFAULT cannot be replaced with 0, the only way to do it would be for the initializer to supply values for all the elements explicitly, which has a maintainability problem since the dimensions are defined in terms of the macros ROWS and COLS .如果DEFAULT不能用 0 替换,那么唯一的方法就是让初始化程序显式地为所有元素提供值,这具有可维护性问题,因为维度是根据宏ROWSCOLS The GNU C Compiler (GCC) has an extension to the C language designated initializers that allows a range of array elements to be initialized to the same value. GNU C 编译器 (GCC) 对 C 语言指定的初始化程序进行了扩展,允许将一系列数组元素初始化为相同的值。 That could be used as follows:可以按如下方式使用:

char mat_cols_[2][ROWS+2][COLS+2] =
    {[0 ... 1] =
        {[0 ... ROWS+2-1] =
            {[0 ... COLS+2-1] = DEFAULT}
        }
    };

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

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