4

Given the following type

typedef struct {
  const void *data;
  uint32_t offset[2];
} TSNode;

I've got a function which contains an assignement:

TSNode* myFun() {
    TSNode node = get_node();
    // rest of code
}

Since node is allocated on stack memory here, it'll vanish once the function has ended. I want to keep track of that node at later points, so I thought to just copy it to the heap.

TSNode* myFun() {
    TSNode node = get_node();

    TSNode* heapNode = malloc(sizeof(TSNode));
    memcpy(heapNode, &node, sizeof(TSNode));
    return heapNode;
}

Does this memcpy all data correctly to my heap allocated heapNode? i.e. nothing is corrupted in the process & the *data is intact?

7
  • 1
    what this function do get_node(); ? Commented Mar 16, 2018 at 14:22
  • Answer is yes. What makes you think memcpy would corrupt anything? But this is somewhat odd, please show get_node(), depending on that you might need to do things differently alltogether. Commented Mar 16, 2018 at 14:25
  • 1
    No, memcpy just copies the whole struct byte per byte and this is exactly what we want. Manually copying each field is also possible, but it's slower and error prone. Commented Mar 16, 2018 at 14:27
  • 1
    You just encountered the rationale for copy constructors in C++! Commented Mar 16, 2018 at 14:33
  • 1
    By the way, there is no need to memcpy. Simply assign: *heapnode = node;. I think that was part of the first ANSI C version (The original Kernighan/Ritchie C allowed only two things with a struct: Applying the . operator and the & operator. No assignment, no parameter passing). That is, it has been available for 39 years. I feel old. Commented Mar 16, 2018 at 14:36

1 Answer 1

5

Does this memcpy all data correctly to my heap allocated heapNode? i.e. nothing is corrupted in the process & the *data is intact?

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.

Sign up to request clarification or add additional context in comments.

7 Comments

On the other hand, the data pointer member is a const pointer, so it is probably pointing to constant data that should not be deallocated.
So if the const void *data wasn't constant, I would need to make a deep copy?
Whether data gets invalid after returning from the function depends on the content that is assigned by get_node. The returned pointer itself will not become dangling anyway.
@DavidRTribble, Good catch. Even though it is a const pointer it will not be a const anymore.
@Seneca, Use deep copy concept
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.