// 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)
兩個不同的malloc可以返回內存中任意位置的地址 – stark
**注意:** double ** x是指向double的指針,不是*數組*。它們是C中的兩個完全不同的對象。 –