我想读取 he5
格式,但 HDF-EOS5
库中出现错误。我的库版本是 HDF5 1.8.14、EOS5 1.15 和 MSVS 2010。
这是我的代码:
void main()
{
hid_t gridfileid = 0;
hid_t gridid = 0;
short *terrain[720];
hsize_t i, j;
int a = 0;
for( a = 0; a < 720; a++ )
{
terrain[a] = (short*)malloc(sizeof(short) * 1440);
}
gridfileid = HE5_GDopen( "D:\\MyHDFEOS\\OMI-Aura_L3-OMAEROe_2004m1001_v003-2009m0114t094640.he5", H5F_ACC_RDONLY );
if( gridfileid == FAIL )
{
printf( "he5 File Open FAIL!" );
return;
}
gridid =HE5_GDattach( gridfileid, "ColumnAmountAerosol" );
if( gridid == FAIL )
{
printf( "Grid Open FAIL!" );
return;
}
HE5_GDreadfield( gridid, "TerrainReflectivity", NULL, NULL, NULL, terrain );
HE5_GDdetach( gridid );
HE5_GDclose( gridfileid );
for( i = 0; i < 7; ++i ) {
for( j = 0; j < 14; ++j ) {
printf( "%d", terrain[i][j ]);
}
}
printf("\n");
for( a = 0; a < 720; a++ )
{
free( terrain[a] );
}
free( terrain );
}
此代码错误。
HE5_GDreadfield( gridid, "TerrainReflectivity", NULL, NULL, NULL, terrain );
'HDF-EOS5.exe 的 0x76f315fe 中存在未处理的异常。 0xC0000005:访问冲突'
这个错误是什么?
最佳答案
According to the documentation ,在
HE5_GDreadfield( gridid, "TerrainReflectivity", NULL, NULL, NULL, terrain );
terrain
参数应为:
short terrain[720][1440];
这是一个 1,036,800 字节的连续内存块。
但是,您使用:
short *terrain[720];
然后一次分配1440字节。这会提供 720 个非连续内存块,每 block 长度为 1440 字节,因此您的代码会失败。查看源代码,我看到该函数的声明是:
herr_t HE5_GDreadfield(hid_t gridID, const char *fieldname, const hssize_t start[],
const hsize_t stride[], const hsize_t edge[],
void * buffer);
terrain
参数是一个 void *
,这意味着可以传入任何指针,因此如果您使用:
short *terrain;
terrain = malloc(sizeof(short) * 720 * 1440);
/* etc... */
HE5_GDreadfield( gridid, "TerrainReflectivity", NULL, NULL, NULL, terrain );
/* etc... */
free( terrain );
然后要访问元素,您必须执行以下操作:
printf( "%d", terrain[i * 1440 + j]);
或者,您可以使用:
short (*terrain)[1440];
terrain = malloc(sizeof(short) * 720 * 1440);
/* etc... */
HE5_GDreadfield( gridid, "TerrainReflectivity", NULL, NULL, NULL, terrain );
/* etc... */
free( terrain );
然后要访问元素,您可以使用更自然的方式:
printf( "%d", terrain[i][j]);
关于c - HDF-EOS5 中的 MSVS 2010 C 代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31799632/