1

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!

simonc
  • 41,632
  • 12
  • 85
  • 103
misteryes
  • 2,167
  • 4
  • 32
  • 58
  • No,but yes to cast result from `malloc()`:http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Jack May 19 '13 at 22:11

5 Answers5

6

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
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • What is the point of the assembly? This question can, and should, be answered without reference to one specific implementation. – David Heffernan May 20 '13 at 06:34
4

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).

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
3

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

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
2

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.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • I assumed he meant that he did a stack allocation of a structure and assigned to it and requires some copy at some point. – Jesus Ramos May 19 '13 at 20:22
0

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490