Simple structure and copying data to pointer of the same structure

Discussion in 'C++' started by A, Apr 14, 2011.

  1. A

    A Guest

    let's say I have:

    struct TWhatever
    {
    int MyInt;
    bool MyBool;
    }

    Now I have already preallocated memory to where pWhatever points so:

    TWhatever* pWhatever;

    // Memory is already preallocated so the below lines work properly
    pWhatever->MyInt = 1;
    pWhatever->MyBool = false;

    And now I also have vector:

    std::vector<TWhatever> MyVect;
    MyVect.pushBack(TWhatever());
    MyVect[0].MyInt = 1;
    MyVect[0].MyBool = false;


    And now the question finally - all I want is to copy data from vector to
    memory that pWhatever points to.

    So I tried:
    pWhatever = &MyVect[0]; // doesn't seem to work

    But this worked:
    pWhatever->MyInt = MyVect[0].MyInt; // works
    pWhatever->MyBool = MyVect[0].MyBool; // also works

    The question - can I do it somehow simpler than the last example? The
    problem is not in last 2 lines but there are 30 or so lines for each
    initialization so something simpler would be much more readable in code.
    A, Apr 14, 2011
    #1
    1. Advertising

  2. A

    A Guest

    > If your objects are types that perform their own initializationsor have
    > internal
    > pointers (which they do not seem to be given the above TWhatever) , then
    > your
    > struct should also define a copy constructor. (cctor)


    I want to avoid having additional constructors within structure itself
    because structure is allocated thousands of times so additional constructors
    means additional memory for each of them.

    So effectively you're saying there is no other way except memcpy or the
    stuff I just did above right (if we rule out putting a copy constructor to
    save memory)?
    A, Apr 14, 2011
    #2
    1. Advertising

  3. A

    A Guest

    OK I tried:

    memcpy(pWhatever, &MyVect[0], sizeof(TWhatever));

    and that seems to work and is simple enough for what I need.
    Thanks for your suggestion.

    If anyone has anything to add go ahead ;)
    A, Apr 14, 2011
    #3
  4. A

    A Guest

    > memcpy(pWhatever, &MyVect[0], sizeof(TWhatever));

    one added question: if the original structure contains dynamically allocated
    stuff like for example std::string

    struct TWhatever
    {
    int a;
    bool b;
    std::string c;
    }

    will the above memcpy still work without any memory leaks because size of
    the structure is constant.... or the std::string handles that automatically?
    A, Apr 14, 2011
    #4
  5. A

    A Guest

    > Your question appears to be about how to copy POD structs, cctor likely
    > will not
    > be needed, but the question of whether or not to use the copy constructor
    > is one


    Yes, but it's not just POD's... in fact, in C++ Builder which I use this
    struct also contains UnicodeString's
    But I got no memory leaks so I'm guessing it works with memcpy....
    A, Apr 14, 2011
    #5
  6. Am 14.04.2011 17:44, schrieb A:
    >> If your objects are types that perform their own initializationsor have
    >> internal
    >> pointers (which they do not seem to be given the above TWhatever) , then
    >> your
    >> struct should also define a copy constructor. (cctor)

    >
    > I want to avoid having additional constructors within structure itself
    > because structure is allocated thousands of times so additional constructors
    > means additional memory for each of them.


    That's wrong. Constructors don't consume any memory in the objects.

    > So effectively you're saying there is no other way except memcpy or the
    > stuff I just did above right (if we rule out putting a copy constructor to
    > save memory)?


    You can just assign the object. Don't assign the pointers.

    Instead of
    memcpy(pWhatever, &MyVect[0], sizeof(TWhatever));
    you can do:
    *pWhatever = MyVect[0];

    --
    Thomas
    Thomas J. Gritzan, Apr 14, 2011
    #6
  7. Am 14.04.2011 17:59, schrieb A:
    >> memcpy(pWhatever,&MyVect[0], sizeof(TWhatever));

    >
    > one added question: if the original structure contains dynamically allocated
    > stuff like for example std::string
    >
    > struct TWhatever
    > {
    > int a;
    > bool b;
    > std::string c;
    > }
    >
    > will the above memcpy still work without any memory leaks because size of
    > the structure is constant.... or the std::string handles that automatically?


    No, don't use memcpy with that struct. memcpy only works on POD types,
    that is (basicly, but not fully correct) structs that could be used in a
    C program. Since a std::string is not a POD, the struct that contains a
    std::string also is not a POD.

    You can use a simple assignment instead, since the compiler
    automatically generates an assignment operator for you. Example:

    TWhatever a,b;
    .... // initialize b here
    a = b;

    --
    Thomas
    Thomas J. Gritzan, Apr 14, 2011
    #7
  8. * A, on 14.04.2011 17:25:
    > let's say I have:
    >
    > struct TWhatever
    > {
    > int MyInt;
    > bool MyBool;
    > }
    >
    > Now I have already preallocated memory to where pWhatever points so:
    >
    > TWhatever* pWhatever;
    >
    > // Memory is already preallocated so the below lines work properly
    > pWhatever->MyInt = 1;
    > pWhatever->MyBool = false;
    >
    > And now I also have vector:
    >
    > std::vector<TWhatever> MyVect;
    > MyVect.pushBack(TWhatever());
    > MyVect[0].MyInt = 1;
    > MyVect[0].MyBool = false;
    >
    >
    > And now the question finally - all I want is to copy data from vector to
    > memory that pWhatever points to.
    >
    > So I tried:
    > pWhatever =&MyVect[0]; // doesn't seem to work
    >
    > But this worked:
    > pWhatever->MyInt = MyVect[0].MyInt; // works
    > pWhatever->MyBool = MyVect[0].MyBool; // also works
    >
    > The question - can I do it somehow simpler than the last example?


    Yes.

    *pWhatever = myVect[0];


    > The
    > problem is not in last 2 lines but there are 30 or so lines for each
    > initialization so something simpler would be much more readable in code.


    Define constructors to do initialization.


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Apr 14, 2011
    #8
  9. * A, on 14.04.2011 17:59:
    >> memcpy(pWhatever,&MyVect[0], sizeof(TWhatever));

    >
    > one added question: if the original structure contains dynamically allocated
    > stuff like for example std::string
    >
    > struct TWhatever
    > {
    > int a;
    > bool b;
    > std::string c;
    > }
    >
    > will the above memcpy still work without any memory leaks because size of
    > the structure is constant.... or the std::string handles that automatically?


    The memcpy yields Undefined Behavior.

    Most likely that some internal buffer will be deallocated twice or more. That
    is, that the code will attempt to do so.

    Remember: each occurrence of "memcpy" indicates a novice. So to progress out of
    that category, you have to (temporarily) forget about existence of "memcpy".
    Simply, don't use it -- and that way you also avoid a lot of bugs.


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Apr 14, 2011
    #9
  10. A

    A Guest

    > Instead of
    > memcpy(pWhatever, &MyVect[0], sizeof(TWhatever));
    > you can do:
    > *pWhatever = MyVect[0];


    ok, that worked too :)

    but it copies data right? it doesn't just change memory pointer points to?
    A, Apr 14, 2011
    #10
  11. Am 14.04.2011 18:15, schrieb A:
    >> Instead of
    >> memcpy(pWhatever,&MyVect[0], sizeof(TWhatever));
    >> you can do:
    >> *pWhatever = MyVect[0];

    >
    > ok, that worked too :)
    >
    > but it copies data right? it doesn't just change memory pointer points to?


    Yes.

    pWhatever = &MyVect[0]; // this assigns the *pointer*
    *pWhatever = MyVect[0]; // this assigns the object; i.e. all of its data

    --
    Thomas
    Thomas J. Gritzan, Apr 14, 2011
    #11
  12. A

    Paul Guest

    "A" <> wrote in message news:io74pd$9ru$...
    >> If your objects are types that perform their own initializationsor have
    >> internal
    >> pointers (which they do not seem to be given the above TWhatever) , then
    >> your
    >> struct should also define a copy constructor. (cctor)

    >
    > I want to avoid having additional constructors within structure itself
    > because structure is allocated thousands of times so additional
    > constructors means additional memory for each of them.


    This is not the case, there will be only 1 constructor even if you have 500
    objects allocated.

    struct TWhatever{
    TWhatever():MyInt(0), MyBool(0){}
    TWhatever(const TWhatever& rhs){
    MyInt= rhs.MyInt;
    MyBool= rhs.MyBool;}
    TWhatever& operator=(const TWhatever& rhs){
    MyInt= rhs.MyInt;
    MyBool = rhs.MyBool;
    return *this;}
    ~TWhatever (){}
    int MyInt;
    bool MyBool;
    };

    struct TWhatever2{
    int MyInt;
    bool MyBool;
    };


    int main(){
    std::cout<<sizeof(TWhatever) << "\t" << sizeof(TWhatever2);
    }
    Paul, Apr 14, 2011
    #12
  13. Re: Simple structure and copying data to pointer of the samestructure

    * Alf P. Steinbach /Usenet:
    > Remember: each occurrence of "memcpy" indicates a novice.


    A statement like that doesn't help. In fact, it shows more about you than
    anything else.

    Just because *you* are safely hiding in an ivory tower where you don't need
    memcpy doesn't mean that people who *do* have a clue are idiots.

    --
    Martijn van Buul -
    Martijn van Buul, Apr 14, 2011
    #13
  14. A

    Noah Roberts Guest

    On 4/14/2011 8:25 AM, A wrote:
    > let's say I have:
    >
    > struct TWhatever
    > {
    > int MyInt;
    > bool MyBool;
    > }
    >
    > Now I have already preallocated memory to where pWhatever points so:
    >
    > TWhatever* pWhatever;
    >
    > // Memory is already preallocated so the below lines work properly
    > pWhatever->MyInt = 1;
    > pWhatever->MyBool = false;
    >
    > And now I also have vector:
    >
    > std::vector<TWhatever> MyVect;
    > MyVect.pushBack(TWhatever());
    > MyVect[0].MyInt = 1;
    > MyVect[0].MyBool = false;
    >
    >
    > And now the question finally - all I want is to copy data from vector to
    > memory that pWhatever points to.
    >
    > So I tried:
    > pWhatever =&MyVect[0]; // doesn't seem to work
    >
    > But this worked:
    > pWhatever->MyInt = MyVect[0].MyInt; // works
    > pWhatever->MyBool = MyVect[0].MyBool; // also works


    *pWhatever = MpVect[0];

    --
    http://crazycpp.wordpress.com
    Noah Roberts, Apr 14, 2011
    #14
  15. * Martijn van Buul, on 14.04.2011 18:30:
    > * Alf P. Steinbach /Usenet:
    >> Remember: each occurrence of "memcpy" indicates a novice.

    >
    > A statement like that doesn't help.


    If the statement does not help you, then just ignore it. No need to tell the
    world that it doesn't help you. It's akin to yelling "i'm a super programmer who
    is not helped by simple advice like that" -- nobody believes it...


    > In fact, it shows more about you than anything else.


    What do you think it shows?


    > Just because *you* are safely hiding in an ivory tower where you don't need
    > memcpy


    I'm not hiding, I'm not in an ivory tower, and it's not the case that I never
    need memcpy.

    Why this hostility & lying?


    > doesn't mean that people who *do* have a clue are idiots.


    Why do you think that "people who do have a clue are idiots"?


    Puzzled,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Apr 14, 2011
    #15
  16. Re: Simple structure and copying data to pointer of the samestructure

    * Alf P. Steinbach /Usenet:
    > * Martijn van Buul, on 14.04.2011 18:30:
    >> * Alf P. Steinbach /Usenet:
    >>> Remember: each occurrence of "memcpy" indicates a novice.

    > I'm not hiding, I'm not in an ivory tower, and it's not the case that I never
    > need memcpy.


    Ah, so it's "Each occurrence of memcpy indicates a novice unless it's me" ?

    --
    Martijn van Buul -
    Martijn van Buul, Apr 14, 2011
    #16
  17. * Martijn van Buul, on 14.04.2011 19:01:
    > * Alf P. Steinbach /Usenet:
    >> * Martijn van Buul, on 14.04.2011 18:30:
    >>> * Alf P. Steinbach /Usenet:
    >>>> Remember: each occurrence of "memcpy" indicates a novice.

    >> I'm not hiding, I'm not in an ivory tower, and it's not the case that I never
    >> need memcpy.

    >
    > Ah, so it's "Each occurrence of memcpy indicates a novice unless it's me" ?


    No, it's just as I wrote.

    If you find that sentence inordinately difficult to understand, then just ask
    about it.

    For my part, I admit that I failed to guess that your out of the blue ad hominem
    attack, could be really just a conversation opener, preparing you for asking
    your so-extremely-hard-to-ask question (whatever it is)?


    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Apr 14, 2011
    #17
  18. A

    A Guest

    thank you all for your comments. now i know more about pointers ;)
    A, Apr 14, 2011
    #18
  19. A

    gwowen Guest

    On Apr 14, 6:12 pm, "Alf P. Steinbach /Usenet" <alf.p.steinbach
    > wrote:
    > > Ah, so it's "Each occurrence of memcpy indicates a novice unless it's me" ?

    >
    > No, it's just as I wrote.
    >
    > If you find that sentence inordinately difficult to understand, then justask
    > about it.


    To be honest Alf, I find most of your posts an enlightening breath of
    sanity, but I'm not altogether sure what you mean about memcpy being
    the sign of a novice. Using memcpy *wrongly*, or a using it a lot is
    the sign of a novice, sure but ... anyway, I miss your point.
    gwowen, Apr 14, 2011
    #19
  20. * gwowen, on 14.04.2011 23:32:
    > On Apr 14, 6:12 pm, "Alf P. Steinbach /Usenet"<alf.p.steinbach
    > > wrote:
    >>> Ah, so it's "Each occurrence of memcpy indicates a novice unless it's me" ?

    >>
    >> No, it's just as I wrote.
    >>
    >> If you find that sentence inordinately difficult to understand, then just ask
    >> about it.

    >
    > To be honest Alf, I find most of your posts an enlightening breath of
    > sanity, but I'm not altogether sure what you mean about memcpy being
    > the sign of a novice. Using memcpy *wrongly*, or a using it a lot is
    > the sign of a novice, sure but ... anyway, I miss your point.


    It's about the same as, in the 1970's, one could say that every "goto" was the
    sign of a novice.

    Sure, there are situations where it's the right thing to do. And yes, when there
    is a "goto" which is at least arguably acceptable, then it was most likely
    placed there by a very competent person (since chances of anyone else managing
    that feat would be infinitesimal). Then you can weight up other evidence to
    counter the evidence that the "goto" gives by itself.

    In modern C++ "memcpy", on its own, just says: this programmer don't even know
    about curly braces initializations, or was not able to express the logical types
    as C++ types, or... So, that would most likely be a novice. Much less likely, I
    think, but still possible, it could perhaps be an incompetent or someone forced
    by a silly coding guideline, or perhaps an MS engineer[1].


    Cheers & hth.,

    - Alf

    Notes:
    [1] Hey, that's just trolling. ;-)

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Apr 14, 2011
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. anonymous

    copying data to a structure member array

    anonymous, Jan 4, 2005, in forum: C Programming
    Replies:
    12
    Views:
    607
    Keith Thompson
    Jan 11, 2005
  2. Replies:
    6
    Views:
    364
    Jack Klein
    Aug 27, 2006
  3. Hicham Mouline
    Replies:
    100
    Views:
    2,033
    Noah Roberts
    Aug 25, 2009
  4. Replies:
    4
    Views:
    233
    Paul Lalli
    Aug 1, 2007
  5. Stephen Howe
    Replies:
    2
    Views:
    280
    Stephen Howe
    Nov 6, 2012
Loading...

Share This Page