M
Malcolm Nooning
I can see that in the pasted results of the code snippet (also pasted
below), there are 48 decimal bytes between rows of an array. The
coded subtraction of row addresses is showing 12 instead of 48. I
cannot figure out why. I see the same result on my Windows cygwin and
my Linux CentOS boxes. Any idea what is going on?
//----------- Paste code snippet
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int nrows = 5; /* Both nrows and ncols could be evaluated */
int ncols = 10; /* or read in at run time */
int row;
int **rowptr;
rowptr = malloc(nrows * sizeof(int *));
if (rowptr == NULL)
{
puts("\nFailure to allocate room for row pointers.\n");
exit(0);
}
printf("\nIndex &rowptr[row] &rowptr[row][0] Ptr(hex) Ptr(dec)
Diff(dec)");
for (row = 0; row < nrows; row++)
{
rowptr[row] = malloc(ncols * sizeof(int));
if (rowptr[row] == NULL)
{
printf("\nFailure to allocate for row[%d]\n",row);
exit(0);
}
printf("\n %d %u %u %p %d",
row, &rowptr[row], &rowptr[row][0], rowptr[row],
rowptr[row] );
if (row > 0)
printf(" %d",(int)(rowptr[row] - rowptr[row-1]));
}
return 0;
}
//-----------------------------------------
// Results
//
// Index &rowptr[row] &rowptr[row][0] Ptr(hex) Ptr(dec) Diff(dec)
// 0 6685048 6750616 0x670198 6750616
// 1 6685052 6750664 0x6701c8 6750664 12
// 2 6685056 6750712 0x6701f8 6750712 12
// 3 6685060 6750760 0x670228 6750760 12
// 4 6685064 6750808 0x670258 6750808 12
below), there are 48 decimal bytes between rows of an array. The
coded subtraction of row addresses is showing 12 instead of 48. I
cannot figure out why. I see the same result on my Windows cygwin and
my Linux CentOS boxes. Any idea what is going on?
//----------- Paste code snippet
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int nrows = 5; /* Both nrows and ncols could be evaluated */
int ncols = 10; /* or read in at run time */
int row;
int **rowptr;
rowptr = malloc(nrows * sizeof(int *));
if (rowptr == NULL)
{
puts("\nFailure to allocate room for row pointers.\n");
exit(0);
}
printf("\nIndex &rowptr[row] &rowptr[row][0] Ptr(hex) Ptr(dec)
Diff(dec)");
for (row = 0; row < nrows; row++)
{
rowptr[row] = malloc(ncols * sizeof(int));
if (rowptr[row] == NULL)
{
printf("\nFailure to allocate for row[%d]\n",row);
exit(0);
}
printf("\n %d %u %u %p %d",
row, &rowptr[row], &rowptr[row][0], rowptr[row],
rowptr[row] );
if (row > 0)
printf(" %d",(int)(rowptr[row] - rowptr[row-1]));
}
return 0;
}
//-----------------------------------------
// Results
//
// Index &rowptr[row] &rowptr[row][0] Ptr(hex) Ptr(dec) Diff(dec)
// 0 6685048 6750616 0x670198 6750616
// 1 6685052 6750664 0x6701c8 6750664 12
// 2 6685056 6750712 0x6701f8 6750712 12
// 3 6685060 6750760 0x670228 6750760 12
// 4 6685064 6750808 0x670258 6750808 12