Make Copy of structures with huge no. of pointer members

This is a discussion on Make Copy of structures with huge no. of pointer members within the C Programming forums, part of the General Programming Boards category; Hi, I have a structure, that reads someting like: Code: struct _B { int info; struct _C *p4; } typedef ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    3

    Make Copy of structures with huge no. of pointer members

    Hi,
    I have a structure, that reads someting like:
    Code:
     
    struct _B {
        int info;
        struct _C *p4;
    }
    
    typedef struct _A{
        int a,b,c;
        int *p1;
        char *p2;
        struct _B *p3;
       /* Many more pointers */
    }A;
    Have to write a copy function,
    A *make_copy(A *src);
    i.e. make_copy makes a copy of 'src' and returns. Note that it should properly copy pointers also (i.e. make copy of pointer members also).

    Apart from copying member by member, is there any other way to do same?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One of the (imo) little known facts about structs is that you can copy them directly in C. Might have been our super sorter iMalc that mentioned that fact in a post.

    (Probably others did as well)

    I don't know if that includes strings and pointer members, however.

    Why not try it and see?

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    It will not make deep copies of any pointers, but it will copy all the shallow values.
    Regardless, if you want to make deep copies of the pointers, it is not a trivial task, and no easy solution exists.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Vithal, do you *really* need to make a copy of all these structs? You can't just pass a pointer to the struct to any function that needs it?

    In light of what Elysia posted, I would sure try hard not to have to copy a lot of these big structs. if the structs had different members on them that would make it even more tedious.

    uggh!

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    3
    Well, passing just pointer to struct not serving my purpose. I have a requirement where need to process msgs from API callers and this message might get queued if the system can't handle in caller's context (system too busy??). So, had caller passed pointer to structure (local variable), then queuing would crash the system. If we want to make no assumptions about caller's mem alloc scheme, want to make a copy and queue it.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Then you basically need to either create a custom, complex function which can make a copy of a struct by taking in a lot of arguments about its structure, or, you can make a simple, custom function that clones a specific structure.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    3
    Wanted to avoid writing copy function that looks into each member and does copy. Well, if no other solution exists, will do same for now.

    Thanks Elysia and others for replying.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Such a generic solution is difficult to do in C. You would have to consider a more high-level language to perform such a copy without hassle.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-11-2009, 07:52 AM
  2. pointer to array of structures
    By strokebow in forum C Programming
    Replies: 14
    Last Post: 12-09-2006, 01:14 PM
  3. Make an exe copy itself to another location
    By Machewy in forum C++ Programming
    Replies: 24
    Last Post: 06-16-2003, 09:17 PM
  4. Changing a Structures Members array size
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 06:45 PM
  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

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21