Thread: Deep copy

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    12

    Deep copy

    Hello!
    I'd like to know if it's possible to create a function that performs a deep copy of a struct variable without being bound to its particular internal representation. In other words, a function that (with enough informations) can perform a deep copy of any struct that it receives as a parameter, regardless of its type.
    Thanks in advance!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Lookup the memcpy() function ... It's part of standard C and copies anything you tell it to.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Ahh, but if your struct has pointers to objects, theoretically deep copy has to make a copy of what is pointed to as well, not just the address of what the source struct pointed to. That's what makes it so deep. Again, like your iterator pattern, you will probably need to write a deep copy for each of your structs and use some sort of function pointer stuff to implement a generic interface.

    Can I ask why you're trying to turn C into C++? C is not and never was intended to do OO stuff. You can hack some of it in there, but it's not really well built for that.

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

    No.

    Soma

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    Quote Originally Posted by anduril462 View Post
    Ahh, but if your struct has pointers to objects, theoretically deep copy has to make a copy of what is pointed to as well, not just the address of what the source struct pointed to. That's what makes it so deep. Again, like your iterator pattern, you will probably need to write a deep copy for each of your structs and use some sort of function pointer stuff to implement a generic interface.

    Can I ask why you're trying to turn C into C++? C is not and never was intended to do OO stuff. You can hack some of it in there, but it's not really well built for that.
    Thanks again!
    As for your question, I'm not trying to turn C in C++. I have a university work to do that involves the creation of a library that adapts the design patterns of OO languages to be used on a non OO language like C. During the work I found that through low level programming some of the features of OO languages can be replicated in C, but in some other cases (i.e. in the case of reflection) this seemed impossible to me. But my knowledge of C is quite limited, so I decided to ask here, where there are people that surely know C much better than me!

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I have a university work to do that involves the creation of a library that adapts the design patterns of OO languages to be used on a non OO language like C
    Bummer! I was hoping I could dissuade you from such folly.

    I've actually messed around with implementing some of these features before, and it's definitely not easy. I somehow forgot to mention a few potentially useful preprocessor features (if you're not already using them). First is the ## command for token concatenation. The second is what they call x-macros: C preprocessor - Wikipedia, the free encyclopedia. I've used them for a number of things like supporting an isA() function for checking type, etc. Good luck!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy Constructors and Assignment Operator overloading.
    By leeor_net in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2009, 10:26 PM
  2. LinkedList operator= and deep copy
    By sethjackson in forum C++ Programming
    Replies: 11
    Last Post: 02-28-2008, 12:54 PM
  3. Deep and Shallow Copy
    By peckitt99 in forum C++ Programming
    Replies: 2
    Last Post: 05-13-2007, 07:28 AM
  4. In deep copy
    By Belzebuts in forum C Programming
    Replies: 1
    Last Post: 02-05-2006, 11:58 AM
  5. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM