私は2次元配列をCで奇妙なことができ知っている、とのmallocで、私はこのようなものだろう:2つのFORS、配列で、Cで二重配列をshmgetおよびshmatする方法は?
/* This is your 2D array. */
double** matrix;
/* The size dimensions of your 2D array. */
int numRows, numCols;
/* Used as indexes as in matrix[x][y]; */
int x, y;
/*
* Get values into numRows and numCols somehow.
*/
/* Allocate pointer memory for the first dimension of a matrix[][]; */
matrix = (double **) malloc(numCols * sizeof(double *));
if(NULL == matrix){free(matrix); printf("Memory allocation failed while allocating for matrix[].\n"); exit(-1);}
/* Allocate integer memory for the second dimension of a matrix[][]; */
for(x = 0; x < numCols; x++)
{
matrix[x] = (double *) malloc(numRows * sizeof(double));
if(NULL == matrix[x]){
free(matrix[x]); printf("Memory allocation failed while allocating for matrix[x][].\n");
exit(-1);
}
}
と初期化を。 は今、私は**配列に共有メモリ内のスペースを割り当てたいが、私はこれを行うことができるかどうかわからない:
shmid2 = shmget(IPC_PRIVATE, numCols * sizeof (int*), IPC_CREAT | 0700);
my_array = (double**) shmat(shmid2, NULL, 0);
をし、それを初期化します。これは正しいです。もしそうでなければ、私はこれをどのように正しい方法で行うことができますか?
は、配列が大きくなると、私はので、メモリの断片化のさえmalloc関数と、それをこのようにしないだろう、事前に
また、if(NULL ==行列){空(行列);}が正しくありません。 –
'free(NULL)'はノーオペレーションとして標準化されていますが、実際には正しいとは限りません。 – dreamlax