Originally posted by: Markbnj
For an x,y grid of data types it's basically y * (x * element size). Just calculate the size of the whole block and malloc it in one chunk, then set up your pointers correctly.
Edit lousydood beat me to the post. But I am not sure what the benefit of individually allocating the rows is if the data type is homogenous and the arrays are not jagged.
Plus the space for the extra layer of pointers.Originally posted by: Markbnj
For an x,y grid of data types it's basically y * (x * element size). Just calculate the size of the whole block and malloc it in one chunk, then set up your pointers correctly.
Well, if your array is big, you might be able to find y blocks of size x*elementsize but not one single block of y*x*e. I have no idea how often that would come into play or how much memory you'd need to use though.Edit lousydood beat me to the post. But I am not sure what the benefit of individually allocating the rows is if the data type is homogenous and the arrays are not jagged.
Originally posted by: kamper
Plus the space for the extra layer of pointers.Originally posted by: Markbnj
For an x,y grid of data types it's basically y * (x * element size). Just calculate the size of the whole block and malloc it in one chunk, then set up your pointers correctly.
Well, if your array is big, you might be able to find y blocks of size x*elementsize but not one single block of y*x*e. I have no idea how often that would come into play or how much memory you'd need to use though.Edit lousydood beat me to the post. But I am not sure what the benefit of individually allocating the rows is if the data type is homogenous and the arrays are not jagged.
Yep, that's what I was getting at.Originally posted by: Markbnj
Yeah, for a really large chunk of data it might give you some memory management granularity; like a row-level paging scheme.
I'm probably missing something here, I haven't done any c development in a while (I actually started a thread on this topic a while ago, I should try to find it).On the first point, the classic example of a 2D array in memory is a screen buffer. It's just y * (x * color_depth). The key point is that there is no fundamental difference in the memory layout of int[16], int[4][4], int[2][8], int[8][2], int[2][2][4], etc. It's all in how the indexes are interpreted.
Originally posted by: lousydood
int **array; // example
int i, n=5,m=6; // dimensions
array = (int **) malloc (sizeof(int *) * n);
if (array == NULL) {
perror("malloc");
exit(1);
}
for(i=0; i<n; i++) {
array = (int *) malloc (sizeof(int) * m);
if (array == NULL) {
perror("malloc");
exit(1);
}
}
.
.
.
for(i=0; i<n; i++) free (array);
free (array);
Originally posted by: kamper
Yep, that's what I was getting at.Originally posted by: Markbnj
Yeah, for a really large chunk of data it might give you some memory management granularity; like a row-level paging scheme.
I'm probably missing something here, I haven't done any c development in a while (I actually started a thread on this topic a while ago, I should try to find it).On the first point, the classic example of a 2D array in memory is a screen buffer. It's just y * (x * color_depth). The key point is that there is no fundamental difference in the memory layout of int[16], int[4][4], int[2][8], int[8][2], int[2][2][4], etc. It's all in how the indexes are interpreted.
So, if you declare, say, an int[6][4], the compiler has to remember that your second dimension is 4. Otherwise it won't know how far to jump when you try to address something like foo[3][1]. What happens when you pass it to some function as an **int?
I thought that if you had a 2-d array, the first index would give you an array of pointers, from whence you would follow a pointer to your array of data, like in lousydood's example. Is there a difference between **int and int[][]?
Gotcha (I also read my old text book about this). So a disadvantage then is that you have to set the row length at compile time for any functions which will process the array.Originally posted by: Markbnj
It's a little early in the AM for thinking about arrays and addresses, but I think you're confusing the actual layout of the data in memory with the _type_ of an expression involving the array. If you allocate an array int[4][4] the compiler will create a contiguous region of memory and perform the indexing calculations to address the right row and column.
Originally posted by: kamper
Gotcha (I also read my old text book about this). So a disadvantage then is that you have to set the row length at compile time for any functions which will process the array.Originally posted by: Markbnj
It's a little early in the AM for thinking about arrays and addresses, but I think you're confusing the actual layout of the data in memory with the _type_ of an expression involving the array. If you allocate an array int[4][4] the compiler will create a contiguous region of memory and perform the indexing calculations to address the right row and column.
