Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #29788 > unrolled thread

deep copy or shallow copy?

Started bymbalover2014@gmail.com
First post2014-06-24 10:53 -0700
Last post2014-08-11 00:13 -0700
Articles 20 on this page of 62 — 23 participants

Back to article view | Back to comp.lang.c++


Contents

  deep copy or shallow copy? mbalover2014@gmail.com - 2014-06-24 10:53 -0700
    Re: deep copy or shallow copy? Robert Wessel <robertwessel2@yahoo.com> - 2014-06-24 13:01 -0500
    Re: deep copy or shallow copy? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2014-06-24 20:19 +0000
      Re: deep copy or shallow copy? drew@furrfu.invalid (Drew Lawson) - 2014-06-26 12:43 +0000
        Re: deep copy or shallow copy? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2014-07-01 15:23 +0000
    Re: deep copy or shallow copy? Victor Bazarov <v.bazarov@comcast.invalid> - 2014-06-24 16:32 -0400
    Re: deep copy or shallow copy? Juha Nieminen <nospam@thanks.invalid> - 2014-06-26 04:48 +0000
      Re: deep copy or shallow copy? JiiPee <no@notvalid.com> - 2014-07-18 10:37 +0100
      Re: deep copy or shallow copy? CHIN Dihedral <dihedral88888@gmail.com> - 2014-07-22 13:28 -0700
        Re: deep copy or shallow copy? Victor Bazarov <v.bazarov@comcast.invalid> - 2014-07-22 16:37 -0400
          Re: deep copy or shallow copy? CHIN Dihedral <dihedral88888@gmail.com> - 2014-07-23 03:17 -0700
            Re: deep copy or shallow copy? Victor Bazarov <v.bazarov@comcast.invalid> - 2014-07-23 08:47 -0400
              Re: deep copy or shallow copy? CHIN Dihedral <dihedral88888@gmail.com> - 2014-07-23 14:14 -0700
        Re: deep copy or shallow copy? Wouter van Ooijen <wouter@voti.nl> - 2014-07-23 09:28 +0200
        Re: deep copy or shallow copy? Juha Nieminen <nospam@thanks.invalid> - 2014-07-24 05:21 +0000
          Re: deep copy or shallow copy? CHIN Dihedral <dihedral88888@gmail.com> - 2014-07-25 08:05 -0700
    Re: deep copy or shallow copy? Lynn McGuire <lmc@winsim.com> - 2014-07-25 17:53 -0500
      Re: deep copy or shallow copy? Victor Bazarov <v.bazarov@comcast.invalid> - 2014-07-25 19:06 -0400
        Re: deep copy or shallow copy? CHIN Dihedral <dihedral88888@gmail.com> - 2014-07-25 18:59 -0700
          Re: deep copy or shallow copy? ojackson520@gmail.com - 2014-07-25 23:56 -0700
          Re: deep copy or shallow copy? scott@slp53.sl.home (Scott Lurndal) - 2014-07-28 13:35 +0000
            Re: deep copy or shallow copy? Lynn McGuire <lmc@winsim.com> - 2014-07-28 15:54 -0500
              Re: deep copy or shallow copy? David Brown <david.brown@hesbynett.no> - 2014-07-29 12:30 +0200
                Re: deep copy or shallow copy? Reinhardt Behm <rbehm@hushmail.com> - 2014-07-29 18:56 +0800
                Re: deep copy or shallow copy? Tobias Müller <troplin@bluewin.ch> - 2014-07-29 12:03 +0000
                Re: deep copy or shallow copy? scott@slp53.sl.home (Scott Lurndal) - 2014-07-29 14:21 +0000
                  Re: deep copy or shallow copy? Victor Bazarov <v.bazarov@comcast.invalid> - 2014-07-29 12:39 -0400
                    Re: deep copy or shallow copy? Martijn Lievaart <m@rtij.nl.invlalid> - 2014-07-29 18:48 +0200
                      Re: deep copy or shallow copy? Wouter van Ooijen <wouter@voti.nl> - 2014-07-29 19:16 +0200
                      Re: deep copy or shallow copy? Victor Bazarov <v.bazarov@comcast.invalid> - 2014-07-29 14:38 -0400
                    Re: deep copy or shallow copy? scott@slp53.sl.home (Scott Lurndal) - 2014-07-29 19:24 +0000
                    Re: deep copy or shallow copy? David Brown <david.brown@hesbynett.no> - 2014-07-30 11:08 +0200
                Re: deep copy or shallow copy? Paavo Helde <myfirstname@osa.pri.ee> - 2014-07-29 15:30 -0500
        Re: deep copy or shallow copy? Lynn McGuire <lmc@winsim.com> - 2014-07-26 01:37 -0500
          Re: deep copy or shallow copy? Ian Collins <ian-news@hotmail.com> - 2014-07-26 20:22 +1200
            Re: deep copy or shallow copy? CHIN Dihedral <dihedral88888@gmail.com> - 2014-07-26 03:19 -0700
              Re: deep copy or shallow copy? Wouter van Ooijen <wouter@voti.nl> - 2014-07-26 14:34 +0200
            Re: deep copy or shallow copy? Lynn McGuire <lmc@winsim.com> - 2014-07-26 16:56 -0500
              Re: deep copy or shallow copy? Richard Damon <Richard@Damon-Family.org> - 2014-07-26 18:26 -0400
              Re: deep copy or shallow copy? Ian Collins <ian-news@hotmail.com> - 2014-07-27 11:14 +1200
              Re: deep copy or shallow copy? Victor Bazarov <v.bazarov@comcast.invalid> - 2014-07-27 10:54 -0400
                Re: deep copy or shallow copy? "Osmium" <r124c4u102@comcast.net> - 2014-07-27 10:01 -0500
                  Re: deep copy or shallow copy? CHIN Dihedral <dihedral88888@gmail.com> - 2014-07-27 16:12 -0700
                Re: deep copy or shallow copy? Victor Bazarov <v.bazarov@comcast.invalid> - 2014-07-27 14:35 -0400
                Re: deep copy or shallow copy? Stuart <DerTopper@web.de> - 2014-07-28 17:24 +0200
                  Re: deep copy or shallow copy? Victor Bazarov <v.bazarov@comcast.invalid> - 2014-07-28 12:52 -0400
                    Re: deep copy or shallow copy? scott@slp53.sl.home (Scott Lurndal) - 2014-07-28 18:43 +0000
              Re: deep copy or shallow copy? scott@slp53.sl.home (Scott Lurndal) - 2014-07-28 13:39 +0000
          Re: deep copy or shallow copy? Victor Bazarov <v.bazarov@comcast.invalid> - 2014-07-26 11:34 -0400
            Re: deep copy or shallow copy? Lynn McGuire <lmc@winsim.com> - 2014-07-26 16:59 -0500
      Re: deep copy or shallow copy? scott@slp53.sl.home (Scott Lurndal) - 2014-07-28 13:32 +0000
        Re: deep copy or shallow copy? Lynn McGuire <lmc@winsim.com> - 2014-07-28 14:28 -0500
          Re: deep copy or shallow copy? Juha Nieminen <nospam@thanks.invalid> - 2014-08-04 05:56 +0000
            Re: deep copy or shallow copy? Lynn McGuire <lmc@winsim.com> - 2014-08-05 11:41 -0500
              Re: deep copy or shallow copy? Juha Nieminen <nospam@thanks.invalid> - 2014-08-07 11:03 +0000
                Re: deep copy or shallow copy? Lynn McGuire <lmc@winsim.com> - 2014-08-07 12:29 -0500
          Re: deep copy or shallow copy? Öö Tiib <ootiib@hot.ee> - 2014-08-04 05:02 -0700
            Re: deep copy or shallow copy? Lynn McGuire <lmc@winsim.com> - 2014-08-05 11:43 -0500
              Re: deep copy or shallow copy? Öö Tiib <ootiib@hot.ee> - 2014-08-05 17:13 -0700
                Re: deep copy or shallow copy? Lynn McGuire <lmc@winsim.com> - 2014-08-06 15:14 -0500
            Re: deep copy or shallow copy? CHIN Dihedral <dihedral88888@gmail.com> - 2014-08-20 20:56 -0700
    Re: deep copy or shallow copy? Mikołaj Małecki <ethouris@gmail.com> - 2014-08-11 00:13 -0700

Page 1 of 4  [1] 2 3 4  Next page →


#29788 — deep copy or shallow copy?

Frommbalover2014@gmail.com
Date2014-06-24 10:53 -0700
Subjectdeep copy or shallow copy?
Message-ID<645bf8d9-4e56-4398-aa39-056ba5e5d434@googlegroups.com>
Hi all,

lets assume that I have a structure defined as

struct A
{
float arr[100];
}


A a1;
A a2;

// Initialize a1, a2.

memcpy(&a1,&a2,sizeof(A));

is it a deep copy or still shallow copy?

Also how about a2 = a1; is it a deep copy or shallow copy from a1 to a2?


Thank you.

[toc] | [next] | [standalone]


#29789

FromRobert Wessel <robertwessel2@yahoo.com>
Date2014-06-24 13:01 -0500
Message-ID<57fjq991884fofiv67r27mv5dlhekmu08b@4ax.com>
In reply to#29788
On Tue, 24 Jun 2014 10:53:02 -0700 (PDT), mbalover2014@gmail.com
wrote:

>Hi all,
>
>lets assume that I have a structure defined as
>
>struct A
>{
>float arr[100];
>}
>
>
>A a1;
>A a2;
>
>// Initialize a1, a2.
>
>memcpy(&a1,&a2,sizeof(A));
>
>is it a deep copy or still shallow copy?
>
>Also how about a2 = a1; is it a deep copy or shallow copy from a1 to a2?


Without subordinate linked objects, there's no difference.

[toc] | [prev] | [next] | [standalone]


#29793

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2014-06-24 20:19 +0000
Message-ID<slrnlqjn9i.o3q.grahn+nntp@frailea.sa.invalid>
In reply to#29788
On Tue, 2014-06-24, mbalover2014@gmail.com wrote:
> Hi all,
>
> lets assume that I have a structure defined as
>
> struct A
> {
> float arr[100];
> }
>
>
> A a1;
> A a2;
>
> // Initialize a1, a2.
>
> memcpy(&a1,&a2,sizeof(A));
>
> is it a deep copy or still shallow copy?

Doesn't matter; you shouldn't use memcpy() in C++ except in very
special situations where you break the type checking on purpose.
(And even in ancient C the memcpy() above is bad style IMO -- it adds
unsafety but gives you no benefits at all.)

There's std::copy and the '=' operator.

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .

[toc] | [prev] | [next] | [standalone]


#29845

Fromdrew@furrfu.invalid (Drew Lawson)
Date2014-06-26 12:43 +0000
Message-ID<loh4hm$1qo7$1@raid.furrfu.com>
In reply to#29793
In article <slrnlqjn9i.o3q.grahn+nntp@frailea.sa.invalid>
	Jorgen Grahn <grahn+nntp@snipabacken.se> writes:
>On Tue, 2014-06-24, mbalover2014@gmail.com wrote:
>>
>> memcpy(&a1,&a2,sizeof(A));
>>
>> is it a deep copy or still shallow copy?
>
>Doesn't matter; you shouldn't use memcpy() in C++ except in very
>special situations where you break the type checking on purpose.
>(And even in ancient C the memcpy() above is bad style IMO -- it adds
>unsafety but gives you no benefits at all.)

That depends on your metric for "ancient C."
When I learned C, you couldn't assign structs.

Not that C++ ever had that problem.

-- 
Drew Lawson            |  We were taking a vote when
                       |  the ground came up and hit us.
                       |      -- Cylon warrior

[toc] | [prev] | [next] | [standalone]


#29883

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2014-07-01 15:23 +0000
Message-ID<slrnlr5kjv.o3q.grahn+nntp@frailea.sa.invalid>
In reply to#29845
On Thu, 2014-06-26, Drew Lawson wrote:
> In article <slrnlqjn9i.o3q.grahn+nntp@frailea.sa.invalid>
> 	Jorgen Grahn <grahn+nntp@snipabacken.se> writes:
>>On Tue, 2014-06-24, mbalover2014@gmail.com wrote:
>>>
>>> memcpy(&a1,&a2,sizeof(A));
>>>
>>> is it a deep copy or still shallow copy?
>>
>>Doesn't matter; you shouldn't use memcpy() in C++ except in very
>>special situations where you break the type checking on purpose.
>>(And even in ancient C the memcpy() above is bad style IMO -- it adds
>>unsafety but gives you no benefits at all.)
>
> That depends on your metric for "ancient C."
> When I learned C, you couldn't assign structs.

Poor wording by me.  I meant that C has little to offer in this area
compared to C++ -- and yet this particular memcpy() isn't needed in C
either, and hasn't been needed for about thirty years.

(I'm surprised by the amount of /new/ C code which prefers memcpy()
to struct assignment, but I've complained about that in c.l.c and will
try not to repeat that here.)

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .

[toc] | [prev] | [next] | [standalone]


#29796

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2014-06-24 16:32 -0400
Message-ID<locn9q$3ei$1@dont-email.me>
In reply to#29788
On 6/24/2014 1:53 PM, mbalover2014@gmail.com wrote:
> lets assume that I have a structure defined as
>
> struct A
> {
> float arr[100];
> }
>
>
> A a1;
> A a2;
>
> // Initialize a1, a2.

Just a nit-pick.  You can't *initialize* after the definition.  You can 
only *initialize* at the moment of definition.  So, for instance,

    A a123 = { 1,2,3 };

would be "initialize", and whatever you do before calling 'memcpy' ought 
to be called "give value" or "assign to" or something like that.

> memcpy(&a1,&a2,sizeof(A));
>
> is it a deep copy or still shallow copy?
>
> Also how about a2 = a1; is it a deep copy or shallow copy from a1 to a2?

    a2 = a1;

is called "copy-assignment", and it's performed as *member-wise* 
assignment, which for members which are arrays means, in turn, 
element-wise assignment.

Next time, do your own homework by *reading your textbook* and thinking, 
please.

V
-- 
I do not respond to top-posted replies, please don't ask

[toc] | [prev] | [next] | [standalone]


#29840

FromJuha Nieminen <nospam@thanks.invalid>
Date2014-06-26 04:48 +0000
Message-ID<log8ma$vhr$1@adenine.netfront.net>
In reply to#29788
mbalover2014@gmail.com wrote:
> memcpy(&a1,&a2,sizeof(A));

I'm curious to know why you would even *want* to do that when you can
simply do a2=a1.

--- news://freenews.netfront.net/ - complaints: news@netfront.net ---

[toc] | [prev] | [next] | [standalone]


#29981

FromJiiPee <no@notvalid.com>
Date2014-07-18 10:37 +0100
Message-ID<4V5yv.182424$Ea4.161710@fx21.am4>
In reply to#29840
On 26/06/2014 05:48, Juha Nieminen wrote:
> mbalover2014@gmail.com wrote:
>> memcpy(&a1,&a2,sizeof(A));
> I'm curious to know why you would even *want* to do that when you can
> simply do a2=a1.
>


  Yes, Bjarne would not be happy us using memcpy in C++ :). And simple 
solution is better than complex, thus

a2=a1

is much more simple than

memcpy.

[toc] | [prev] | [next] | [standalone]


#30037

FromCHIN Dihedral <dihedral88888@gmail.com>
Date2014-07-22 13:28 -0700
Message-ID<01a02e25-0f7f-4b87-8359-f0f91a716bee@googlegroups.com>
In reply to#29840
On Thursday, June 26, 2014 12:48:13 PM UTC+8, Juha Nieminen wrote:
> mbalover2014@gmail.com wrote:
> 
> > memcpy(&a1,&a2,sizeof(A));
> 
> 
> 
> I'm curious to know why you would even *want* to do that when you can
> 
> simply do a2=a1.
> 
> 
> 
> --- news://freenews.netfront.net/ - complaints: news@netfront.net ---

Well, how about assigning an object 
of a muti-inheritance subclass to 
an objet of its parrent class. 

The headaques created by  C++'s
 design in the language outweighted
its advance in teaching OOP.
 

[toc] | [prev] | [next] | [standalone]


#30038

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2014-07-22 16:37 -0400
Message-ID<lqmi1v$t91$1@dont-email.me>
In reply to#30037
On 7/22/2014 4:28 PM, CHIN Dihedral wrote:
> On Thursday, June 26, 2014 12:48:13 PM UTC+8, Juha Nieminen wrote:
>> mbalover2014@gmail.com wrote:
>>
>>> memcpy(&a1,&a2,sizeof(A));
>>
>>
>>
>> I'm curious to know why you would even *want* to do that when you can
>>
>> simply do a2=a1.
>>
>>
>>
>> --- news://freenews.netfront.net/ - complaints: news@netfront.net ---
>
> Well, how about assigning an object
> of a muti-inheritance subclass to
> an objet of its parrent class.

What's a "parrent class"?  Which one of {A,B} here:

     class B {}; class A : B {};

is the "parrent"?

Conversion from an object of the derived type to any of its 
*unambiguous* base classes is well defined in C++ (and is a standard 
conversion).  Does it present a problem to you?  Or are you talking 
about a conversion from a base class to a derived class?

> The headaques created by  C++'s
>   design in the language outweighted
> its advance in teaching OOP.

Have you taught OOP using C++?  Or have you been taught OOP using C++?

V
-- 
I do not respond to top-posted replies, please don't ask

[toc] | [prev] | [next] | [standalone]


#30045

FromCHIN Dihedral <dihedral88888@gmail.com>
Date2014-07-23 03:17 -0700
Message-ID<a0f9957a-3d84-4601-ac21-e6aff941cb5a@googlegroups.com>
In reply to#30038
> What's a "parrent class"?  Which one of {A,B} here:
> 
> 
> 
>      class B {}; class A : B {};
> 
> 
> 
> is the "parrent"?
> 
> 
> 
> Conversion from an object of the derived type to any of its 
> 
> *unambiguous* base classes is well defined in C++ (and is a standard 
> 
> conversion).  Does it present a problem to you?  Or are you talking 
> 
> about a conversion from a base class to a derived class?
> 
> 
Seriously speaking, C++ is more like
an experimental result with a lot 
burdens in the language developing
process.

I have no problem resolving those 
burdens, but when I have to lead 
and work with a team of novice 
programmers in C++ then I begin to 
get headaques.

 

[toc] | [prev] | [next] | [standalone]


#30047

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2014-07-23 08:47 -0400
Message-ID<lqoatk$fvo$1@dont-email.me>
In reply to#30045
On 7/23/2014 6:17 AM, CHIN Dihedral wrote:
>
>> What's a "parrent class"?  Which one of {A,B} here:
>>
>>
>>
>>       class B {}; class A : B {};
>>
>>
>>
>> is the "parrent"?
>>
>>
>>
>> Conversion from an object of the derived type to any of its
>>
>> *unambiguous* base classes is well defined in C++ (and is a standard
>>
>> conversion).  Does it present a problem to you?  Or are you talking
>>
>> about a conversion from a base class to a derived class?
>>
>>
> Seriously speaking, C++ is more like
> an experimental result with a lot
> burdens in the language developing
> process.

"Seriously speaking"?  Sounds more like trolling to me.  Not going to 
dignify it with a technical response (since you moved the discussion off 
its technical direction.)

> I have no problem resolving those
> burdens, but when I have to lead
> and work with a team of novice
> programmers in C++ then I begin to
> get headaques.

You better take something for that.  You know, I've heard a cup of Java 
helps somewhat.  Novice programmers feel quite at ease there...

V
-- 
I do not respond to top-posted replies, please don't ask

[toc] | [prev] | [next] | [standalone]


#30058

FromCHIN Dihedral <dihedral88888@gmail.com>
Date2014-07-23 14:14 -0700
Message-ID<c693b3b9-84f4-4655-8600-bc2d1953490e@googlegroups.com>
In reply to#30047
On Wednesday, July 23, 2014 8:47:47 PM UTC+8, Victor Bazarov wrote:
> On 7/23/2014 6:17 AM, CHIN Dihedral wrote:
> 
> >
> 
> >> What's a "parrent class"?  Which one of {A,B} here:
> 
> >>
> 
> >>
> 
> >>
> 
> >>       class B {}; class A : B {};
> 
> >>
> 
> >>
> 
> >>
> 
> >> is the "parrent"?
> 
> >>
> 
> >>
> 
> >>
> 
> >> Conversion from an object of the derived type to any of its
> 
> >>
> 
> >> *unambiguous* base classes is well defined in C++ (and is a standard
> 
> >>
> 
> >> conversion).  Does it present a problem to you?  

The chance of misusing member funtions
of the same name will be increased
in multi-inheritance classes.

 




 



[toc] | [prev] | [next] | [standalone]


#30044

FromWouter van Ooijen <wouter@voti.nl>
Date2014-07-23 09:28 +0200
Message-ID<53cf640b$0$4687$e4fe514c@dreader36.news.xs4all.nl>
In reply to#30037
>>> memcpy(&a1,&a2,sizeof(A));
>>
>> I'm curious to know why you would even *want* to do that when you can
>>
>> simply do a2=a1.
>>
> Well, how about assigning an object
> of a muti-inheritance subclass to
> an objet of its parrent class.

You think bitwise copy is the appropriate way to do that? I smell 
aliasing, memory leaks, heap corruption...

> The headaques created by  C++'s
>   design in the language outweighted
> its advance in teaching OOP.

When your idea of using C++ is memcpy'ing objects I agree that there 
will be lots of headaches!

Wouter van Ooijen
(who teaches C++)

[toc] | [prev] | [next] | [standalone]


#30073

FromJuha Nieminen <nospam@thanks.invalid>
Date2014-07-24 05:21 +0000
Message-ID<lqq53u$1h58$1@adenine.netfront.net>
In reply to#30037
CHIN Dihedral <dihedral88888@gmail.com> wrote:
> On Thursday, June 26, 2014 12:48:13 PM UTC+8, Juha Nieminen wrote:
>> mbalover2014@gmail.com wrote:
>> 
>> > memcpy(&a1,&a2,sizeof(A));
>> 
>> 
>> 
>> I'm curious to know why you would even *want* to do that when you can
>> 
>> simply do a2=a1.
> 
> Well, how about assigning an object 
> of a muti-inheritance subclass to 
> an objet of its parrent class. 

Yeah, because using memcpy totally fixes that problem...

WTF?

--- news://freenews.netfront.net/ - complaints: news@netfront.net ---

[toc] | [prev] | [next] | [standalone]


#30102

FromCHIN Dihedral <dihedral88888@gmail.com>
Date2014-07-25 08:05 -0700
Message-ID<ea033cb9-c44b-4501-a062-96efb430b8c7@googlegroups.com>
In reply to#30073
On Thursday, July 24, 2014 1:21:05 PM UTC+8, Juha Nieminen wrote:
> CHIN Dihedral <dihedral88888@gmail.com> wrote:
> 
> > On Thursday, June 26, 2014 12:48:13 PM UTC+8, Juha Nieminen wrote:
> 
> >> mbalover2014@gmail.com wrote:
> 
> >> 
> 
> >> > memcpy(&a1,&a2,sizeof(A));
> 
> >> 
> 
> >> 
> 
> >> 
> 
> >> I'm curious to know why you would even *want* to do that when you can
> 
> >> 
> 
> >> simply do a2=a1.
> 
> > 
> 
> > Well, how about assigning an object 
> 
> > of a muti-inheritance subclass to 
> 
> > an objet of its parrent class. 
> 
> 
> 
> Yeah, because using memcpy totally fixes that problem...
> 
> 
> 
> WTF?
> 
> 
> 
> --- news://freenews.netfront.net/ - complaints: news@netfront.net ---

Well if a new instane of a class 
inheritanted under, say, 100 to 1000
classes,  the memory size used by a 
single instane can be quite large then
a naive memcpy.
 

[toc] | [prev] | [next] | [standalone]


#30113

FromLynn McGuire <lmc@winsim.com>
Date2014-07-25 17:53 -0500
Message-ID<lqun5m$she$1@dont-email.me>
In reply to#29788
On 6/24/2014 12:53 PM, mbalover2014@gmail.com wrote:
> Hi all,
>
> lets assume that I have a structure defined as
>
> struct A
> {
> float arr[100];
> }
>
>
> A a1;
> A a2;
>
> // Initialize a1, a2.
>
> memcpy(&a1,&a2,sizeof(A));
>
> is it a deep copy or still shallow copy?
>
> Also how about a2 = a1; is it a deep copy or shallow copy from a1 to a2?
>
>
> Thank you.

First, do not use structs in C++ code.  Sadly, we
have a lot of old C code that was promoted to C++
by changing the .c to a .cpp so we have structs
all over the place.

That said, the nice thing about the STL is that it
allows you to go shallow or deep.  You can use:
    std::vector <myClass> anObj;
or
    std::vector <myClass *> anObj;

We frequently use the pointer variant in order to
just make a temporary list of objects subject to
some criteria.  Passing around pointers is a lot
faster and the corresponding new and delete's for
these objects is a very controlled situation.  We
have very little memory leakage in our app of
700K lines of C++ code.

Lynn

[toc] | [prev] | [next] | [standalone]


#30114

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2014-07-25 19:06 -0400
Message-ID<lquntl$u0$1@dont-email.me>
In reply to#30113
On 7/25/2014 6:53 PM, Lynn McGuire wrote:
> On 6/24/2014 12:53 PM, mbalover2014@gmail.com wrote:
>> Hi all,
>>
>> lets assume that I have a structure defined as
>>
>> struct A
>> {
>> float arr[100];
>> }
>>
>>
>> A a1;
>> A a2;
>>
>> // Initialize a1, a2.
>>
>> memcpy(&a1,&a2,sizeof(A));
>>
>> is it a deep copy or still shallow copy?
>>
>> Also how about a2 = a1; is it a deep copy or shallow copy from a1 to a2?
>>
>>
>> Thank you.
>
> First, do not use structs in C++ code.  Sadly, we
> have a lot of old C code that was promoted to C++
> by changing the .c to a .cpp so we have structs
> all over the place.

What's wrong with using 'struct'?

> That said, the nice thing about the STL is that it
> allows you to go shallow or deep.  You can use:
>     std::vector <myClass> anObj;
> or
>     std::vector <myClass *> anObj;
>
> We frequently use the pointer variant in order to
> just make a temporary list of objects subject to
> some criteria.  Passing around pointers is a lot
> faster and the corresponding new and delete's for
> these objects is a very controlled situation.  We
> have very little memory leakage in our app of
> 700K lines of C++ code.

So, for the sake of some miniscule (likely guessed, not measured) 
performance gain you risk "very little memory leakage"?  What's the big 
idea, a little leakage is OK, but a little time spent copying object is 
a no-no?

V
-- 
I do not respond to top-posted replies, please don't ask

[toc] | [prev] | [next] | [standalone]


#30115

FromCHIN Dihedral <dihedral88888@gmail.com>
Date2014-07-25 18:59 -0700
Message-ID<515ec8c1-2e8c-4f14-a52b-ca0b489cfb30@googlegroups.com>
In reply to#30114
On Saturday, July 26, 2014 7:06:28 AM UTC+8, Victor Bazarov wrote:
> On 7/25/2014 6:53 PM, Lynn McGuire wrote:
> 
> > On 6/24/2014 12:53 PM, mbalover2014@gmail.com wrote:
> 
> >> Hi all,
> 
> >>
> 
> >> lets assume that I have a structure defined as
> 
> >>
> 
> >> struct A
> 
> >> {
> 
> >> float arr[100];
> 
> >> }
> 
> >>
> 
> >>
> 
> >> A a1;
> 
> >> A a2;
> 
> >>
> 
> >> // Initialize a1, a2.
> 
> >>
> 
> >> memcpy(&a1,&a2,sizeof(A));
> 
> >>
> 
> >> is it a deep copy or still shallow copy?
> 
> >>
> 
> >> Also how about a2 = a1; is it a deep copy or shallow copy from a1 to a2?
> 
> >>
> 
> >>
> 
> >> Thank you.
> 
> >
> 
> > First, do not use structs in C++ code.  Sadly, we
> 
> > have a lot of old C code that was promoted to C++
> 
> > by changing the .c to a .cpp so we have structs
> 
> > all over the place.
> 
> 
> 
> What's wrong with using 'struct'?
> 
> 
> 
> > That said, the nice thing about the STL is that it
> 
> > allows you to go shallow or deep.  You can use:
> 
> >     std::vector <myClass> anObj;
> 
> > or
> 
> >     std::vector <myClass *> anObj;

Well in this case I'll ask my coworkers
to use "panObj" instead of "anObj"
in the review process.

I knew a lot people hated  
the MS Hungarian naming conventions,
but that's the life.

[toc] | [prev] | [next] | [standalone]


#30119

Fromojackson520@gmail.com
Date2014-07-25 23:56 -0700
Message-ID<8562f038-150d-441f-a92f-330f43a53b49@googlegroups.com>
In reply to#30115
On 2014-07-26, Stefan Ram <ram@zedat.fu-berlin.de> wrote:
> CHIN Dihedral <dihedral88888@gmail.com> writes:
>>Well in this case I'll ask my coworkers
>>to use "panObj" instead of "anObj"
>>in the review process.
>
>   I use pan objects in the kitchen!

plonk

[toc] | [prev] | [next] | [standalone]


Page 1 of 4  [1] 2 3 4  Next page →

Back to top | Article view | comp.lang.c++


csiph-web