更新时间:2023-12-04 13:53:54
此memcpy是否将所有数据正确发送到我分配给堆的heapNode?即在此过程中没有任何损坏&*数据是否完整?
Does this memcpy all data correctly to my heap allocated heapNode? i.e. nothing is corrupted in the process & the *data is intact?
这个问题的答案是, memcpy
使用 shallow copy
概念.在 shallow copy
中,原始结构(节点)中的指针 TSNode node = get_node();
将被复制到新节点 heapNode
( TSNode * heapNode
)一点一点地调用逐位复制.因此,新的 heapNode
指针也将指向与原始节点的指针( TSNode node = get_node();
)指针相同的位置,该指针是通过复制值的位置memcpy
.因此,一旦控件从函数 myFun
返回,则原始节点将从内存中释放.因此,您的新节点指针将变为悬挂指针
.这是 memcpy
的严重副作用.
The answer to this question is, memcpy
use shallow copy
concept. In shallow copy
the pointer in your original structure (node) TSNode node = get_node();
will be copied to the new node heapNode
(TSNode* heapNode
) bit by bit also call bit wise copy. So your new heapNode
pointers will also pointing to the same location as your original node's (TSNode node = get_node();
) pointers from where you copied the value with memcpy
. Hence once control returns from function myFun
your original node will be released from memory. Therefore your new node pointers will become dangling pointers
. That is a critical side effect of memcpy
.