2017-05-25 35 views
1

我需要通過「x」動態地創建在我的代碼中聲明的2D連續陣列(更確切地說如下聲明double **x)與需要驗證用於創建連續陣列

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]陣列連續?

感謝您的評論。

+0

兩個不同的malloc可以返回內存中任意位置的地址 – stark

+0

**注意:** double ** x是指向double的指針,不是*數組*。它們是C中的兩個完全不同的對象。 –

回答

2

此代碼片段是否使x [i] [j]數組連續正確?

是的,從x[0][0]x[rows - 1][cols - 1]的所有數據都在連續區域。

請注意,您不需要另一個指針第二malloc,我建議:

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; 

如果你是C99或C11下,你可以使用一個指向VLA一步malloc

double (*x)[size_tot_x]; 

x = malloc(sizeof(double [size_tot_y][size_tot_x])); 
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) 
+0

奇怪,valgrind告訴你2個分配,並且你在第一個片段中只調用一次'malloc',與第二個片段相同(2'malloc's和valgrind說3) –

+0

確實如此,你當然知道C比我先生好。但是,結果如圖所示。 @KeineLust – snr

+0

_你當然知道C比我優秀sir_:eeing?我不批評你,你的代碼是正確的,valgrind謊言(或者你正在與另一個程序呼叫valgrind) –