0

我有两个指向指针的结构指针

typedef struct Square {
...
...
}Square;

Square **s1; //Representing 2D array of say, 100*100
Square **s2; //Representing 2D array of say, 200*200

两者都使用malloc(). 我已经s1使用某些值进行s2了初始化,并使用默认值完全初始化。基本上我需要在保持其 ( ) 值s1的同时调整其大小,并且“添加”的值将与它们原来的值一样- 默认值。s2s1s2

我从较小的数组到较大的数组写了这个问题memcpy()但显然我在数组和指针之间感到困惑/

我的问题是,如何将这种大小调整s1s2. 我不必保留原始指针。s1如果这是一种更好的方式,我可以复制s2并返回s2,我希望我能正确解释我所追求的。谢谢!

4

3 回答 3

1

二维数组在内存中按顺序排列:row1 row2 row3 等。

memcpy 执行从一个内存位置到另一个内存位置的线性复制。

所以要实现你所需要的:

a) 创建一个新数组

Square **s3 = malloc(sizeof(s2));

b) 将 s2 复制到其中

c) 将 s1 中的内容逐行复制到新的

for(r = 0; r < NROWS_S1; r++)
    memcpy(s3[r], s1[r], sizeof(Square) * NCOLS_S1);

http://www.fredosaurus.com/notes-cpp/arrayptr/23two-dim-array-memory-layout.html

于 2012-08-02T23:33:30.443 回答
1

你可以尝试这样的事情:

typedef struct {
    //
} Square;

Square** s1; // 100x100, needs to resize and be like s2.
Square** s2; // 200x200

void resize_s1()
{
    // resize the outer array
    s1 = realloc(s1, sizeof(Square*)*200);
    memset(s1 + 100, 0, 100*sizeof(Square*)); // we initialize the newly allocated pointers to NULL

    for(int i = 0; i < 200; ++i)
    {
        // resize the inner array. Since we initialized the last
        // 100 pointers to null, realloc will just behave like
        // malloc for them.
        s1[i] = realloc(s1[i], 200*sizeof(Square));

        // ... and copy the new values in! You can omit this step,
        // but anything outside of the original bounds of s1 will
        // be uninitialized. All your pointers will be valid though.
        if(i >= 100)
            memcpy(s1[i] + 100, s2[i] + 100, 100*sizeof(Square));
    }
}

作为警告 - 我在这里使用 realloc 玩得非常快速和松散。阅读其手册页以了解更多详细信息,但如果您遇到内存不足的情况,可能会发生不好的事情。

于 2012-08-02T23:37:44.160 回答
1

您已经在堆上分配了二维矩阵,并且您正在使用 aSquare**来访问它。这意味着您已经:(1)在一次或多次调用中为每个元素分配空间malloc,以及(2)在调用中为所有行指针分配空间malloc。如何进行很大程度上取决于您分配数组的方式。

下面,我assert用来强调每个 malloc/realloc 都可以返回 NULL(表示它无法完成请求)。您可能希望正确处理这些情况。

选项 1:您分别分配每一行

您像这样分配s1矩阵:

Square** s1 = malloc(M1*sizeof(s1[0]));
for (size_t i=0; i < M1; i++)
  s1[i] = malloc(N1*sizeof(s1[i][0]));

在这种情况下,您必须分别处理每一行:

/* M1 and N1 set to size of s1 (M1 x N1) */
/* M2 and N2 set to size of s2 (M2 x N2) */

/* First, reallocate the pointers to each row */
Square** tmpRows = realloc(s1, M2*sizeof(*tmpRows));
assert( (tmpRows != NULL) && "Out of memory reallocating rows" );

s1 = tmpRows;

/* Now, reallocate each row */
for (size_t i=0; i < M1; i++) {
  Square* tmpVals = realloc(s1[i], N2*sizeof(tmpVals[0]));
  assert( (tmpVals != NULL) && "Out of memory reallocating row" );

  /* copy elements of s2 into new column */
  memcpy(tmpVals+N1, s2[i]+N1, (N2-N1)*sizeof(s1[i][0]));
  s1[i] = tmpVals;
}

/* Now, allocate each new row of s1 and copy the additional rows of s2 into s1 */
for (size_t i=M1; i < M2; i++) {
  s1[i] = malloc( N2 * sizeof(s1[i][0]) );
  assert( (s1[i] != NULL) && "Out of memory allocating new row" );
  memcpy(s1[i], s2[i], N2*sizeof(s1[i][0]));
}

选项 2:您一次分配所有行

在这种情况下,您将所有行分配在一个大块中,然后将指针分配到每一行的开头。像这样:

Square** s1 = malloc(M1*sizeof(s1[0]));
s1[0] = malloc( M1*N1*sizeof(s1[0][0]) );
for(size_t i=1; i < M1; i++) 
  s1[i] = s1[i-1] + N1;

要调整数组的大小(并使用 s2 的元素初始化其新元素),您应该执行以下操作:

/* M1 and N1 set to size of s1 (M1 x N1) */
/* M2 and N2 set to size of s2 (M2 x N2) */

/* Make a new copy of the elements of s1.  Linear layout of a 200x200 
 * matrix will be different than the linear layout of a 100x100 matrix.
 * Making a new copy makes it easier to initialize its values.
 */
Square* new_mat = malloc( M2*N2*sizeof(new_mat[0]) );
assert( (new_mat != NULL) && "Out of memory allocating new matrix" );

/* Initialize with values of s2.  Assumption: s2 is also allocated
 * as a contiguous array...
 */
memcpy(new_mat, s2[0], M2*N2*sizeof(s2[0][0]));

/* Now, reallocate the rows */
Square** tmpRows = realloc(s1, M2*sizeof(s1[0]));
assert( (tmpRows != NULL) && "Out of memory reallocating rows" );

s1 = tmpRows;
/* Copy data from old rows into new rows... */
for (size_t i=0; i < M1; i++) {
  /* rows of s1 still point to old_mat data, copy it into new_mat.
   * Each row in new_mat starts at (new_mat + N2*i)
   */
  memcpy( new_mat + N2*i, s1[i], N1*sizeof(s1[i][0]) );
}

/* Free old memory and assign new row pointers... */
free(s1[0]);
s1[0] = new_mat;
for (size_t i=1; i < M2; i++)
  s1[i] = s1[i-1] + N2;
于 2012-08-03T02:30:34.880 回答