需要验证才能创建连续数组

时间:2017-05-25 17:28:24

标签: c arrays

我需要动态创建在我的代码中使用“x”(更准确地说明为double **x,如下所示)声明的2D连续数组

size of 2D array = [size_tot_y, size_tot_x] = [number of lines, number of columns]

我在这里做了什么:

  /* 2D Array */
  double **x;
  /* 1D array */
  double *x_vals;

  /* Allocating arrays */
  x = malloc(size_tot_y*sizeof(*x));
  x_vals = malloc(size_tot_x*size_tot_y*sizeof(*x_vals));

  /* Make x contiguous */
  for (j=0;j<=size_tot_y-1;j++) 
     x[j] = &x_vals[j*size_tot_x];

此代码段是否正确使x[i][j]数组连续?

感谢您的发言。

2 个答案:

答案 0 :(得分:2)

  

此代码段是否正确使x [i] [j]数组连续?

是的,<android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/phone_number" android:textColorHint="@color/colorText"> <android.support.design.widget.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" android:maxLines="1" android:textColorHint="@color/colorText" /> </android.support.design.widget.TextInputLayout>x[0][0]的所有数据都在相邻的区域。

请注意,第二个x[rows - 1][cols - 1]不需要另一个指针,我建议:

malloc

如果你在C99或C11下,你可以使用指向VLA的指针一步x = malloc(size_tot_y * sizeof(*x)); x[0] = malloc(size_tot_x * size_tot_y * sizeof(**x)); for (j = 1; j < size_tot_y; j++) x[j] = x[0] + j * size_tot_x;

malloc

答案 1 :(得分:1)

是的,它提供看似相邻的内存(有更多空间,8而不是4在同一条船上)。您可以尝试我的版本(在Ubuntu 17.04和Mac OS X上使用valgrind测试)和您的版本(在Ubuntu 17.04和Mac OS X上使用valgrind测试)。

// your saying, size of 2D array = 
// [size_tot_y, size_tot_x] = [number of lines, number of columns]

int (*x)[size_tot_y][size_tot_x] = malloc (sizeof(*x));
(*x)[r][c] = something;
...
free(x);

您是否意识到代码之间的字节差异?

测试,

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    size_t size_tot_y = 3; //rows
    size_t size_tot_x = 5; //columns

    printf("sizeof(int) = %li\n\n", sizeof(int));

    int (*x)[size_tot_y][size_tot_x] = malloc(sizeof(*x));
    printf("array starts at %p\n", x);
    printf("sizeof(array) = %li\n", sizeof(*x));  // Note the *
    printf("sizeof(array[0][0]) = 0x%lx\n", sizeof((*x)[0][0]));
    puts("");

    size_t r, c;

    for (r = 0; r <= size_tot_y - 1; r++) {
        for (c = 0; c <= size_tot_x - 1; c++) {
            printf("array[%i][%i] is at %p\n", r, c, &((*x)[r][c]));
        };
        puts("");
    };

    free(*x);
}

测试结果:

sizeof(int) = 4

array starts at 0x5201480
sizeof(array) = 60
sizeof(array[0][0]) = 0x4

array[0][0] is at 0x5201480
array[0][1] is at 0x5201484
array[0][2] is at 0x5201488
array[0][3] is at 0x520148c
array[0][4] is at 0x5201490

array[1][0] is at 0x5201494
array[1][1] is at 0x5201498
array[1][2] is at 0x520149c
array[1][3] is at 0x52014a0
array[1][4] is at 0x52014a4

array[2][0] is at 0x52014a8
array[2][1] is at 0x52014ac
array[2][2] is at 0x52014b0
array[2][3] is at 0x52014b4
array[2][4] is at 0x52014b8

--25548-- REDIR: 0x4ec00e0 (libc.so.6:free) redirected to 0x4c2ecf0 (free)
==25548== 
==25548== HEAP SUMMARY:
==25548==     in use at exit: 0 bytes in 0 blocks
==25548==   total heap usage: 2 allocs, 2 frees, 1,084 bytes allocated
==25548== 
==25548== All heap blocks were freed -- no leaks are possible
==25548== 
==25548== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==25548== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

您的版本:

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    size_t size_tot_y = 3; //rows
    size_t size_tot_x = 5; //columns
    size_t i,j;

    /* 2D Array */
    double **x;
    /* 1D array */
    double *x_vals;

    /* Allocating arrays */
    x = malloc(size_tot_y*sizeof(*x));
    x_vals = malloc(size_tot_x*size_tot_y*sizeof(*x_vals));

    /* Make x contiguous */
    for (j=0;j<=size_tot_y-1;j++)
        x[j] = &x_vals[j*size_tot_x];


    printf("array starts at %p\n", x);
    printf("sizeof(array) = %li\n", sizeof(x) * size_tot_x * size_tot_y);
    printf("sizeof(array[0][0]) = 0x%lx\n", sizeof((x)[0][0]));
    puts("");

    size_t r, c;

    for (r = 0; r <= size_tot_y - 1; r++) {
        for (c = 0; c <= size_tot_x - 1; c++) {
            printf("array[%i][%i] is at %p\n", r, c, &((x)[r][c]));
        };
        puts("");
    };
    free((void*) x_vals);
    free((void*) x);

}

你的输出:

array starts at 0x5201040
sizeof(array) = 120
sizeof(array[0][0]) = 0x8

array[0][0] is at 0x52010a0
array[0][1] is at 0x52010a8
array[0][2] is at 0x52010b0
array[0][3] is at 0x52010b8
array[0][4] is at 0x52010c0

array[1][0] is at 0x52010c8
array[1][1] is at 0x52010d0
array[1][2] is at 0x52010d8
array[1][3] is at 0x52010e0
array[1][4] is at 0x52010e8

array[2][0] is at 0x52010f0
array[2][1] is at 0x52010f8
array[2][2] is at 0x5201100
array[2][3] is at 0x5201108
array[2][4] is at 0x5201110

--28481-- REDIR: 0x4ec00e0 (libc.so.6:free) redirected to 0x4c2ecf0 (free)
==28481== 
==28481== HEAP SUMMARY:
==28481==     in use at exit: 0 bytes in 0 blocks
==28481==   total heap usage: 3 allocs, 3 frees, 1,168 bytes allocated
==28481== 
==28481== All heap blocks were freed -- no leaks are possible
==28481== 
==28481== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==28481== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
相关问题