misteryes
misteryes

Reputation: 2287

is there any consequence if I do assignment but not memcpy after malloc

in the following program:

int main()
{
  struct Node node;
  struct Node* p = (Struct Node*) malloc(sizeof(struct Node));
  *p =node;
  printf("%d\n", *p->seq);
}

usually I did memcpy(p, node, sizeof(node))

now I tried the code above, and it works fine, I'm afraid there are any consequence or faulty stuff if I do assignment but not memcpy after malloc. are there any or the assignment is very correct? thanks!

Upvotes: 1

Views: 152

Answers (5)

jamesdlin
jamesdlin

Reputation: 90015

Directly assigning should be fine. An implementation may in fact implement it via memcpy. From footnote 42 from section 6.2.6.1 of the ISO C99 specification:

Thus, for example, structure assignment may be implemented element-at-a-time or via memcpy.

The only difference between using memcpy and assigning the struct is that assignment (if implemented as element-at-a-time) will not preserve whatever values are stored in any padding bits to the struct. This, however, would matter only in uncommon cases where you happen to care what those bits store (e.g. if you intend to use memcmp on the struct afterward).

Upvotes: 4

David Heffernan
David Heffernan

Reputation: 613013

Ignoring the fact that you are copying an uninitialized value, I think you've got the wrong end of the stick regarding assignment.

Assignment is meant to be performed using the assignment operator. Assignment is a central part of the language and so the designers provided a first class operator to perform it. Use that operator, =, to perform assignment.

In many cases you can perform assignment using memcpy but why would you want to? It's much more verbose and it's completely opaque to the reader. You would never replace

i = 42;

with a call to memcpy.

Bottom line, perform assignment using the = assignment operator.

Upvotes: 0

paulsm4
paulsm4

Reputation: 121689

Jesus Ramos is correct:

1) *p =node; copies everything in "node" to "*p"

2) You do not need an extraneous "memcpy()"

3) You must allocate "*p" (with "malloc()") before you do the copy.

Here is a standalone test:

// C source
#include <stdio.h>
#include <malloc.h>

struct Node {
    int a;
    int b;
    struct Node *next;
};

int
main() {
  struct Node node;
  struct Node *p = malloc(sizeof(struct Node));
  *p = node;
  return 0;
}


# Resulting assembler
main:
        leal    4(%esp), %ecx
        andl    $-16, %esp
        pushl   -4(%ecx)
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %ecx
        subl    $20, %esp
        movl    $12, (%esp)
        call    malloc
        movl    %eax, -8(%ebp)
        movl    -8(%ebp), %edx
        movl    -20(%ebp), %eax
        movl    %eax, (%edx)
        movl    -16(%ebp), %eax
        movl    %eax, 4(%edx)
        movl    -12(%ebp), %eax
        movl    %eax, 8(%edx)
        movl    $0, %eax
        addl    $20, %esp
        popl    %ecx
        popl    %ebp
        leal    -4(%ecx), %esp
        ret

Upvotes: 6

Dietrich Epp
Dietrich Epp

Reputation: 213368

Unless you've left out some code, the assignment is pointless.

struct Node node; // uninitialized variable
struct Node *p = malloc(sizeof(struct Node));
*p = node; // copy an uninitialized variable to a new place

Imagine you are a secretary, and your boss gives you instructions:

  1. Get some paper out of the recycling bin.

  2. Get a second sheet out of the recycling bin.

  3. Copy the contents of the first sheet onto the second sheet.

It doesn't make any sense. Note memcpy() is essentially equivalent to assignment here. Both memcpy() and assignment are pointless, because node is uninitialized.

Upvotes: 2

Jesus Ramos
Jesus Ramos

Reputation: 23268

This will work just fine it does the copy for you.

Upvotes: 3

Related Questions