且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

遇到问题与结构复制到另一个存储单元中的memcpy()

更新时间:2023-11-27 22:52:10

您只复制有一个的结构,所分配的400个字节,而的malloc 以任何方式不初始化的内存。

You only copy one structure to the 400 bytes you allocated, and malloc does not initialize the memory in any way.

新分配的内存从的malloc 的内容是的不确定的,并从未初始化的内存读取导致的未定义行为

The contents of newly allocated memory from malloc is indeterminate, and reading from the uninitialized memory leads to undefined behavior.

此外,当你做 *(K + 1)你把 K 为一个数组,以及访问的第二的元素( *(K + 1)等同于 K [1] ) 。然后,你做的东西更加古怪和打印值作为一个十进制数,它实际上不是。

Also, when you do *(k + 1) you treat k as an array, and accessing the second element (*(k + 1) is equivalent to k[1]). You then do something even weirder and print the value as a decimal number, which it really isn't.

最后,打印使用指针时, 的printf 你应该使用%p格式说明。

Lastly, when printing a pointer using printf you should use the "%p" format specifier.

要真正的答案的你的问题,然后是你复制的数据是存在的,在分配内存的开始。你只是不打印任何它。

To actually answer your question, then yes the data you copy is there, at the beginning of the allocated memory. You just don't print any of it.