简体   繁体   English

内存混乱

[英]memcpy confusion

I am trying to copy a small array into a bigger array, and I could not figure out how to get it working (program always crashes on Visual studio 2008 x32)我正在尝试将一个小数组复制到一个更大的数组中,但我不知道如何让它工作(程序在 Visual Studio 2008 x32 上总是崩溃)

memcpy work for memcpy 为

   memcpy( raster+(89997000), abyRaster, sizeof(abyRaster));

but not但不是

   memcpy( raster+(line*3000), abyRaster, sizeof(abyRaster));

I just want to get it working in the for loop, but got confused about the pointer arithmetic and the size of int and unsigned char.我只是想让它在 for 循环中工作,但对指针算法以及 int 和 unsigned char 的大小感到困惑。

Ideas?想法?

    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));          
    }

The code seems ok, except that代码看起来不错,除了

unsigned char raster[3000*3000];

declares a huge array on the stack, and you may run out of stack space for this operation (typical stack sizes are just a few megabytes).在堆栈上声明一个巨大的数组,您可能会用完此操作的堆栈空间(典型的堆栈大小只有几兆字节)。

Try to declare raster as a dynamic array, using malloc .尝试使用mallocraster声明为动态数组。

That raster array is quite large (9 MB) for a stack variable.对于堆栈变量,该raster数组非常大(9 MB)。 Try allocating it from the heap.尝试从堆中分配它。

The program can not be run directly because of not enough memory 
If the system supports high enough the memory allocated by the program. then  
the program copies bytes of a variable abyRaster to another variable on     
every position (line*3000) of  variable raster.
abyRaster[0] is copied to  raster[0]
abyRaster[1] is copied to  raster[3000]
abyRaster[2] is copied to  raster[6000]
  :                             :
  :                             :
int line=0; line<3000;line++ used to identify only the index values of array 

portoalet,门廊,

http://www.cplusplus.com/reference/clibrary/cstring/memcpy/ says: 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. 

I personally find the "address-of-the-element" syntax (below) much more self-explanatory than the equivalent base-of-the-array-plus-the-index syntax... especially once you get into offsets-into-arrays-of-structs.我个人发现“元素地址”语法(如下)比等效的数组基数加索引语法更不言自明......尤其是当你进入偏移量时结构数组。

memcpy( &raster[arrayPosition], abyRaster, sizeof(abyRaster));  

And BTW: I agree with the other previous posters... everything bigger than "one line" (say 4096 bytes) should be allocated on the heap... otherwise you VERY quickly run out of stack space.顺便说一句:我同意其他以前的海报......所有大于“一行”(比如4096字节)的东西都应该在堆上分配......否则你很快就会用完堆栈空间。 Just DON'T FORGET to free EVERYTHING you malloc... the heap isn't self-cleansing like the stack, and ANSI C doesn't have a garbage collector (to follow you around and clean-up after you).只是不要忘记释放你的所有东西 malloc ......堆不像堆栈那样自我清理,并且 ANSI C 没有垃圾收集器(跟随你并在你之后清理)。

Cheers.干杯。 Keith.基思。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM