我正在尝试将一个小数组复制到一个大数组中,但我不知道如何让它工作(程序在Visual studio 2008 x32上总是崩溃)
memcpy的工作
memcpy( raster+(89997000), abyRaster, sizeof(abyRaster));但不是
memcpy( raster+(line*3000), abyRaster, sizeof(abyRaster));我只想让它在for循环中工作,但对指针算法以及int和unsigned char的大小感到困惑。
想法?
unsigned char raster[3000*3000];
unsigned char abyRaster[3000*1];
for( int line=0; line<3000;line++ ) {
int arrayPosition = line*3000;
memcpy( raster+(arrayPosition), abyRaster, sizeof(abyRaster));
}发布于 2011-04-27 19:11:31
代码看起来没问题,除了
unsigned char raster[3000*3000];在堆栈上声明一个巨大的数组,您可能会耗尽此操作的堆栈空间(典型的堆栈大小只有几兆字节)。
尝试使用malloc将raster声明为动态数组。
发布于 2011-04-27 19:12:21
对于堆栈变量来说,该raster数组非常大(9MB)。尝试从堆中分配它。
发布于 2011-04-27 19:26:32
portoalet,
http://www.cplusplus.com/reference/clibrary/cstring/memcpy/说:
void * memcpy ( void * destination, const void * source, size_t num );
destination : Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.
source : Pointer to the source of data to be copied, type-casted to a pointer of type void*.
num : Number of bytes to copy. 我个人发现"address-of- the -element“语法(见下文)比等效的base-of-array-plus- the index语法更容易理解……尤其是在你进入结构的偏移量数组之后。
memcpy( &raster[arrayPosition], abyRaster, sizeof(abyRaster)); 顺便说一句:我同意之前的其他帖子…所有大于“一行”(比如4096字节)的数据都应该分配到堆中……否则你很快就会用完堆栈空间。别忘了把你的所有东西都放出来...堆不像堆栈那样是自清理的,ANSI C也没有垃圾收集器(它会跟踪您并在您之后进行清理)。
干杯。基思。
https://stackoverflow.com/questions/5803059
复制相似问题