问题描述
是否有可能宣布在C二维数组,然后设置其大小以后?我知道用C,你必须处理内存等,但我无法找到答案,尽管我的所有搜索了这个问题。
我现在的例子是..
INT boardsize,linewin;
字符数组[1] [1];
// boardsize距离这里设置。 阵列= [boardsize] [boardsize];
在C则需要使用指针做自己的动态数组管理。
请参阅如何去这样做使用分配的内存区域下面的文章。
Using对于malloc的多维数组不同的行长度的分配
既然您想修改这些,你可能还需要使用的realloc()
函数或免费()
函数来释放分配的内存。
有关使用realloc()的函数看看下面的堆栈溢出的信息。
双二维数组
two-dimensional动态数组(realloc的C语言)
编辑 - 添加一个例子
下面是两个函数的malloc()
二维数组和的realloc()
二维数组。其实你可以只使用的realloc()
版本,如果你传递一个NULL指针 realloc2dCArray()
的内存区域被重新分配。
我所试图做的是使用一个单一的的malloc()
和的realloc()
所有的内存需要,这样就可以免费()
与一个调用内存免费()
。
的char ** malloc2dCArray(INT NROWS,诠释NCOLS)
{
//使用单一的malloc为char指针到每行的第一个字符
//所以我们分配给指针空间,然后空间的实际行。
焦炭**粒子阵列=的malloc(sizeof的(字符*)* NROWS +的sizeof(char)的* NCOLS * NROWS); 如果(粒子阵列){
//计算偏移的实际数据空间的开始
字符*的Poffset =(字符*)(粒子阵列+ NROWS);
INT I; //修复了指针,以各行
对于(i = 0; I< NROWS;我++){
粒子阵列[我] =的Poffset;
的Poffset + = NCOLS;
}
} 返回粒子阵列;
}焦炭** realloc2dCArray(字符** POLD,诠释NROWS,诠释NCOLS)
{
//使用单一的realloc为char指针到每行的第一个字符
//所以我们重新分配的指针空间,然后空间的实际行。
焦炭**粒子阵列= realloc的(POLD,sizeof的(字符*)* NROWS +的sizeof(char)的* NCOLS * NROWS); 如果(粒子阵列){
//计算偏移的实际数据空间的开始
字符*的Poffset =(字符*)(粒子阵列+ NROWS);
INT I; //修复了指针,以各行
对于(i = 0; I< NROWS;我++){
粒子阵列[我] =的Poffset;
的Poffset + = NCOLS;
}
} 返回粒子阵列;
}
要使用这些功能,你会做一些这样的:
的char ** PChars中= malloc2dCArray(16,8);
INT I,J;对于(i = 0; I< 16;我++){
为(J = 0; J&下; 8; J ++){
PChars中[I] [J] = 0;
}
}
要办一个的realloc()
你将要检查的realloc()
工作这么使用临时变量,并在使用前检查NULL。
{
焦炭** pChars2 = realloc2dCArray(PChars中,25,8);
如果(pChars2)PChars中= pChars2;
}
您也可以只使用的realloc()
版本,如果你提供了,因为 NULL指针的realloc()
将做一个的malloc()
如果指针内存的realloc()
为NULL。
我做了这一些测试使用调试器,它看起来像它工作在我身上。
Is it possible to declare a 2D array in C, then set its size later on? I know in C you have to deal with memory and such, but I cannot find answer to this question despite all my searching.
My current example is..
int boardsize, linewin;
char array[1][1];
//boardsize is set within here.
array = [boardsize][boardsize];
With C you will need to do your own dynamic array management using pointers.
See the following articles on how to go about doing so using an allocated memory area.
Using malloc for allocation of multi-dimensional arrays with different row lengths
Since you are looking to modify these, you may also need to use the realloc()
function or the free()
function to release allocated memory.
For information about using the realloc() function look at the following stack overflow.
two-dimensional dynamic array (realloc in c)
EDIT - Adding an example
Here are two functions to malloc()
a two dimensional array and to realloc()
a two dimensional array. You could actually just use the realloc()
version if you pass a NULL pointer to realloc2dCArray()
for the memory area to be reallocated.
What I have tried to do is to use a single malloc()
and realloc()
for all of the memory needed so that you can free()
the memory with a single call to free()
.
char **malloc2dCArray (int nRows, int nCols)
{
// use a single malloc for the char pointers to the first char of each row
// so we allocate space for the pointers and then space for the actual rows.
char **pArray = malloc (sizeof(char *) * nRows + sizeof(char) * nCols * nRows);
if (pArray) {
// calculate offset to the beginning of the actual data space
char *pOffset = (char *)(pArray + nRows);
int i;
// fix up the pointers to the individual rows
for (i = 0; i < nRows; i++) {
pArray[i] = pOffset;
pOffset += nCols;
}
}
return pArray;
}
char **realloc2dCArray (char **pOld, int nRows, int nCols)
{
// use a single realloc for the char pointers to the first char of each row
// so we reallocate space for the pointers and then space for the actual rows.
char **pArray = realloc (pOld, sizeof(char *) * nRows + sizeof(char) * nCols * nRows);
if (pArray) {
// calculate offset to the beginning of the actual data space
char *pOffset = (char *)(pArray + nRows);
int i;
// fix up the pointers to the individual rows
for (i = 0; i < nRows; i++) {
pArray[i] = pOffset;
pOffset += nCols;
}
}
return pArray;
}
To use these functions you would do something like the following:
char **pChars = malloc2dCArray (16, 8);
int i, j;
for (i = 0; i < 16; i++) {
for (j = 0; j < 8; j++) {
pChars[i][j] = 0;
}
}
To do a realloc()
you will want to check that the realloc()
worked so use a temporary variable and check for NULL before using it.
{
char **pChars2 = realloc2dCArray (pChars, 25, 8);
if (pChars2) pChars = pChars2;
}
You could also just use the realloc()
version if you provide a NULL pointer since realloc()
will do a malloc()
if the pointer to the memory to realloc()
is NULL.
I did some testing of this using a debugger and it looks like it is working to me.
这篇关于安装一个二维数组,改变大小后 - Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!