Thread: memcpy with struct and pointer

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    2

    memcpy with struct and pointer

    Hi everyone.

    I am trying the following.
    Code:
    //*********************
    typedef struct{
     unsigned char Dlength;
     unsigned char *Value;
     } AttributeA;
    
     //In the main
     unsigned char userData[] = {0x01,0x02,0x03,0x04,0x5};
     
     AttributeA buffSrc;
     AttributeA buffDst;
     buffSrc.Dlength = 5;
     buffDst.Dlength = 5;
     buffSrc.Value = userData;
    
     memcpy(buffDst.Value, buffSrc.Value, buffDst.Dlength);
     
    //**********************
    printf of buffSrc.Value gives me the output = 0x01,0x02,0x03,0x04,0x5;
    But printf of buffDst.Value does not give the expected output (as buffSrc.Value).

    I wonder what am I doing wrong?
    I have even tried
    memcpy(&buffDst.Value[0], &buffSrc.Value[0], buffDst.Dlength);

    I shall appreciate.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You have a pointer inside struct.
    You need to allocate enough memory before copying anything to it.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    try at the end:

    buffDst = buffSrc;

    and it works.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Define "works"?

    Soma

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    buffDst.Value is an uninitialized pointer, so I'm wondering why it doesn't crash for you. It has to point to some allocated memory of size at least buffDst.Dlength.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    2
    Bayint & maxorator.
    I got it.
    Thank you.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by phantomotap View Post
    O_o

    Define "works"?

    Soma
    Code:
     unsigned char userData[] = {0x01,0x02,0x03,0x04,0x5};
    
     AttributeA buffSrc;
     AttributeA buffDst;
    
     buffSrc.Dlength = 5;
     buffSrc.Value = userData;
     fwrite(buffSrc.Value,buffSrc.Dlength,1,stdout),fflush(stdout);
    
     buffDst=buffSrc;
     fwrite(buffDst.Value,buffDst.Dlength,1,stdout),fflush(stdout);
    
     assert(5==buffSrc.Dlength && 5==buffDst.Dlength && !memcmp(buffSrc.Value,buffDst.Value,5));
    You see, no new memory needed.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    OP wanted to make a copy of the data. Your code does NOT make a copy of the data - it uses the same data for both structures, not 2 separate copies of it.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Code:
    printf of buffSrc.Value gives me the output = 0x01,0x02,0x03,0x04,0x5;
    But printf of buffDst.Value does not give the expected output (as buffSrc.Value).
    The question was only an expected output for buffDst like buffSrc, therefore to make an extra copy is overdesigned.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    You see, no new memory needed.
    O_o

    Your code is... broken? That doesn't seem to be the right word. Stupid? Crazy? Ignorant? Foolish? Dangerous?

    I'll leave that to the other philosophers, but safe to say, your "it works" code certainly does not do anything useful.

    Oh, and the source and destination "point" to the same location so the `memcmp' is pointless (probably a "NOP").



    The question was only an expected output for buffDst like buffSrc, therefore to make an extra copy is overdesigned.
    o_O

    I think I'll go with "foolish" on this one.

    Soma

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > The question was only an expected output for buffDst like buffSrc, therefore to make an extra copy is overdesigned.
    Try applying that logic to your file system, when you try to make a backup copy of a file.

    "It's only a copy, I'll just point at it, no need to waste disk space".

    "WTF - my backup's been trashed!"

    Do you now understand why shallow copies are a PITA?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM