简体   繁体   English

调用 memcpy 并将 num 设置为比需要更多的字节

[英]calling memcpy with num set to more bytes than needed

consider the following C code:考虑以下 C 代码:

#define SIZE_A // >= SIZE_B
#define SIZE_B 
#define SOME_SIZE // > SIZE_B

int main() {
    int a[SIZE_A];
    int b[SIZE_B] = {0};
    memcpy(a, b, sizeof(int)*(SOME_SIZE));
    return 0;
}

assume that SIZE_A, SIZE_B are some integers and SOME_SIZE > SIZE_B and SIZE_A>=SIZE_B.假设 SIZE_A、SIZE_B 是一些整数并且 SOME_SIZE > SIZE_B 和 SIZE_A>=SIZE_B。 what would be the consequences if:如果出现以下情况会有什么后果:

a. SOME_SIZE < SIZE_A
b. SOME_SIZE = SIZE_A
c. SOME_SIZE > SIZE_A

I tried to run it with some values but didn't understand if there is any constancy.我试图用一些值运行它,但不明白是否有任何恒定性。 Thanks谢谢

In your case (since you are copying from and to start addresses of a and b ), it must hold that SOME_SIZE <= SIZE_A AND SOME_SIZE <= SIZE_B otherwise it is undefined behaviour .在您的情况下(因为您从ab起始地址复制和复制),它必须保持SOME_SIZE <= SIZE_A AND SOME_SIZE <= SIZE_B否则它是未定义的行为

In simple terms the number of bytes you copy from source to destination, should never be more than available bytes starting from the source and destination addresses which you specify to memcpy .简单来说的字节从源复制到目标的数量,应该不会超过从您指定的源地址目的地址开始可用的字节memcpy For example, if in your case you were to copy from middle of array b , you would have to narrow down the bound for SOME_SIZE even more - as starting from middle of b there are even fewer bytes available than SIZE_B .例如,如果在您的情况下,您要从数组b中间复制,则必须进一步缩小SOME_SIZE - 因为从b中间开始,可用字节数甚至比SIZE_B还要少。

You should only copy from a zone you have declared to a zone you have declared.您应该只从已声明的区域复制到已声明的区域。 That means that as soon as SOME_SIZE > SIZE_B you invoke Undefined Behaviour.这意味着只要SOME_SIZE > SIZE_B你就会调用 Undefined Behaviour。

Now what could happen on common implementations:现在常见的实现会发生什么:

ab) SOME_SIZE <= SIZE_A : provided you hit no segment limit, that mean trying to read from non readable memory, you will just copy garbage to the end of a after the content of b - but if you fall in non readable memory you will get a memory violation signal. AB) SOME_SIZE <= SIZE_A :只要你打不段的限制,这意味着试图从非可读存储器读,你只是垃圾复制到年底a的内容后, b -但是如果你在非可读存储器爱河,你获取内存违规信号。

c) SOME_SIZE > SIZE_A : in addition to the problems of reading undefined memory, you write it somewhere you do not know . c) SOME_SIZE > SIZE_A :除了读取未定义内存的问题,你把它写在你不知道的地方 Very bad things are likely to happen here:这里很可能会发生非常糟糕的事情:

  • you could overwrite over variables你可以覆盖变量
  • you could smash return addresses in the stack你可以粉碎堆栈中的返回地址
  • you could try to access non writable memory and get a memory violation signal您可以尝试访问不可写内存并获得内存冲突信号

TL/DR: as it is essentially Undefined Behaviour, what will happen is just, hmm, undefined TL/DR:因为它本质上是未定义的行为,所以会发生的只是,嗯,未定义

You are copying data from after the b variable which in this case, will be random stack contents.您正在从 b 变量之后复制数据,在这种情况下,将是随机堆栈内容。 You won't see any constancy because it is undefined.你不会看到任何恒定性,因为它是未定义的。 The good news (or bad news, depending upon how you look at it) is that it does has not crashed so far.好消息(或坏消息,取决于你如何看待它)是它到目前为止还没有崩溃。 If your SOME_SIZE was HUGE then I think you could get a crash on some platforms.如果您的 SOME_SIZE 很大,那么我认为您可能会在某些平台上崩溃。

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

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