Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Copying the contents of a struct from a pointer

Copying the contents of a struct from a pointer

Scheduled Pinned Locked Moved C / C++ / MFC
c++questioncsharpperformancehelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KellyR
    wrote on last edited by
    #1

    Hi, here's my problem: I have a pointer to a struct (in C++, using VC++ .NET 2005 with MFC). The struct has been allocated from heap memory and contains many fields that are also allocated from the heap (character arrays, etc). How can I copy this straight to another struct? These are the two things I've tried, neither have worked: This is where oldData is a pointer to a MyStruct that has been allocated from the heap: struct MyStruct *newData = new MyStruct; memcpy(newData, oldData, sizeof(struct MyStruct)); // Do stuff, then copy the data back memcpy(oldData, newData, sizeof(struct MyStruct)); delete newData; And: struct MyStruct newData; newData = *oldData; // Do stuff, then copy the data back oldData = &newData; Neither of these methods work. Is there a way to do this or do I have to copy field by field with the proper memory allocations in newData etc? Thanks!

    KR

    K D 2 Replies Last reply
    0
    • K KellyR

      Hi, here's my problem: I have a pointer to a struct (in C++, using VC++ .NET 2005 with MFC). The struct has been allocated from heap memory and contains many fields that are also allocated from the heap (character arrays, etc). How can I copy this straight to another struct? These are the two things I've tried, neither have worked: This is where oldData is a pointer to a MyStruct that has been allocated from the heap: struct MyStruct *newData = new MyStruct; memcpy(newData, oldData, sizeof(struct MyStruct)); // Do stuff, then copy the data back memcpy(oldData, newData, sizeof(struct MyStruct)); delete newData; And: struct MyStruct newData; newData = *oldData; // Do stuff, then copy the data back oldData = &newData; Neither of these methods work. Is there a way to do this or do I have to copy field by field with the proper memory allocations in newData etc? Thanks!

      KR

      K Offline
      K Offline
      KellyR
      wrote on last edited by
      #2

      It seems like it works if I copy all of the character arrays and heap allocated data separately and then do a memcpy on all the rest. Let me know if anyone knows a better way, please! Thanks!

      KR

      1 Reply Last reply
      0
      • K KellyR

        Hi, here's my problem: I have a pointer to a struct (in C++, using VC++ .NET 2005 with MFC). The struct has been allocated from heap memory and contains many fields that are also allocated from the heap (character arrays, etc). How can I copy this straight to another struct? These are the two things I've tried, neither have worked: This is where oldData is a pointer to a MyStruct that has been allocated from the heap: struct MyStruct *newData = new MyStruct; memcpy(newData, oldData, sizeof(struct MyStruct)); // Do stuff, then copy the data back memcpy(oldData, newData, sizeof(struct MyStruct)); delete newData; And: struct MyStruct newData; newData = *oldData; // Do stuff, then copy the data back oldData = &newData; Neither of these methods work. Is there a way to do this or do I have to copy field by field with the proper memory allocations in newData etc? Thanks!

        KR

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        KellyR wrote:

        Neither of these methods work.

        No surprise there, since memcpy() has no way of getting to that other heap data. It only knows about the data in the MyStruct structure.

        KellyR wrote:

        Is there a way to do this or do I have to copy field by field with the proper memory allocations in newData etc?

        I would implement an assignemnt operator in the structure:

        const MyStruct& operator=( const MyStruct &b )
        {
        ...
        return *this;
        }

        "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups