memcpy/memmove 的奇怪行为
我遇到的问题是 memcpy/memmove 更改了 struct FOO foo 的指针,该指针既不是函数的 src 也不是目标。以下是 gdb 输出:
在 memmove(y,y_temp,size_y);
之前:
(gdb) p y
$38 = 0xbff7a2e0
(gdb) p y_temp
$39 = (float *) 0x815ea20
(gdb) p foo
$40 = (FOO *) 0x81d4e90
和之后:
(gdb) p y_temp
$41 = (float *) 0x343434
(gdb) p y
$42 = 0x343434
(gdb) p foo
$43 = (FOO *) 0x343434
以下是变量的定义:
FOO *foo;
float y[nsamples];
float size_y = nsamples*sizeof(y);
float* y_temp = (float*) malloc(size_y);
我知道,这不是 memcpy/move 的错误,所以我正在寻找提示,我这边的编程错误可能导致它。
谢谢
I have the problem that memcpy/memmove change the pointer of a struct FOO foo
, which is neither src nor destination of the function. Here are the gdb outputs:
Before memmove(y,y_temp,size_y);
:
(gdb) p y
$38 = 0xbff7a2e0
(gdb) p y_temp
$39 = (float *) 0x815ea20
(gdb) p foo
$40 = (FOO *) 0x81d4e90
and after:
(gdb) p y_temp
$41 = (float *) 0x343434
(gdb) p y
$42 = 0x343434
(gdb) p foo
$43 = (FOO *) 0x343434
Here are the definitions of the variables:
FOO *foo;
float y[nsamples];
float size_y = nsamples*sizeof(y);
float* y_temp = (float*) malloc(size_y);
I know, that it is not a bug with memcpy/move, so I looking for a tipp, what programming error on my side could have caused it.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
sizeof(y)
已经给出了整个数组的大小,并且类型应该是size_t
。如果这样做,
y
和y_temp
指向的内存将具有相同的大小。您还应该确保您朝着正确的方向前进。现在,y 是目的地。另外,如果源和目标不重叠(这里不会重叠),请使用memcpy。sizeof(y)
already gives you the size of the whole array, and the type should besize_t
.If you do this,
y
and the memoryy_temp
points to will be the same size. You should also make sure you're moving in the right direction. Right now, y is the destination. Also, if the source and destination don't overlap (which they won't here), usememcpy
.然后你就可以了
但是
y
没有足够的空间容纳所有size_y
字节!and then you do
But
y
does not have enough space for all ofsize_y
bytes!