Thread: memcpy()

  1. #1
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104

    Question memcpy()

    Code:
    AnyClass *s1 = (AnyClass *) malloc(sizeof(AnyClass));
    
    AnyClass *s2 = (AnyClass *) malloc(sizeof(AnyClass));
    Now if I use something like this, where AnyClass contains any pointer variable in its private section, will this work??? or how this can be achieved???

    (after some work with the object *s2)

    Code:
    memcpy( (AnyClass *) s1, (AnyClass *) s2, sizeof(AnyClass) );
    Last edited by Moni; 09-05-2006 at 03:14 AM.
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Depends what you mean by "work".

    One problem with;
    Code:
    AnyClass *s1 = (AnyClass *) malloc(sizeof(AnyClass));
    is that no constructor of class AnyClass is invoked. In practice, with most compilers, the virtual function table for the object will be uninitialised, so any attempt to call a virtual member function will yield unpredictable behaviour.

    The code;
    Code:
       memcpy( (AnyClass *) s1, (AnyClass *) s2, sizeof(AnyClass) );
    I suspect (but haven't checked) that this will yield undefined behaviour if AnyClass is not a POD type (i.e. it is not a type that makes sense to a C compiler). At best, the results are poorly defined as the intent is that a programmer will not use memcpy() to copy C++ objects (which may have constructors, destructors, and other member functions).

    Instead of trying to use memcpy() like this, make use of the copy constructor and assignment operator for the class. There may be either supplied by the compiler or written by hand (to do things that the compiler-supplied versions do not). The only exception to this statement would be instances where the class designer has deliberately suppressed copy constructor and assignment operator --- and memcpy() is not a valid workaround for that.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    In general, no it won't work except in very limited cases.

    Why do you ask, is there a reason you feel you need this kind of code?
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > AnyClass *s1 = (AnyClass *) malloc(sizeof(AnyClass));
    Use new / delete not malloc, so that
    a) constructors and destructors get called
    b) you don't need icky castarama to get it to compile.

    > memcpy( (AnyClass *) s1, (AnyClass *) s2, sizeof(AnyClass) );
    If your class has a properly written copy constructor, then
    *s1 = *s2;
    will do the right thing all the time, even if it's non-POD.

    If this is in relation to your other C++ thread about rolling your own list, I'd say you need to go learn some C++ first.
    Cos at the moment, it looks like C munged into C++.
    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. Disagreement about memcpy
    By ch4 in forum C Programming
    Replies: 9
    Last Post: 05-28-2009, 10:12 AM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Memcpy(); Errors...
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2006, 11:35 AM
  4. memcpy with 128 bit registers
    By grady in forum Linux Programming
    Replies: 2
    Last Post: 01-19-2004, 06:25 AM
  5. memcpy
    By doubleanti in forum C++ Programming
    Replies: 10
    Last Post: 02-28-2002, 04:44 PM