# Conventions on struct copying?

4 messages from 2014-06-18 to 2014-06-20. Participants: brian m. carlson, Junio C Hamano, Jason Pyeron.
Thread: https://gitlist.dev/t/36959

## brian m. carlson, 2014-06-18 23:31

Subject: Conventions on struct copying?
Message-ID: <20140618233129.GK368384@vauxhall.crustytoothpaste.net>
URL: https://gitlist.dev/e/20140618233129.GK368384%40vauxhall.crustytoothpaste.net

```
I'm still working on the struct object_id patches, and I had a style
question.  In several places throughout the code, we do something like
this:

  unsigned char a[20], b[20];

  /* do some stuff with b */
  hashcpy(a, b);

I could implement an oidcpy that does the same thing.

  struct object_id a, b;

  /* do some stuff with b */
  oidcpy(&a, &b);

Or I could just write that as:

  a = b;

and let the compiler do the heavy lifting.  Is there any reason that
we'd want the function for that purpose, or is it okay to just use the
assignment?  I don't know of any place we explicitly copy structs like
this, but I don't know of any prohibition against it, either.

-- 
brian m. carlson / brian with sandals: Houston, Texas, US
+1 832 623 2791 | http://www.crustytoothpaste.net/~bmc | My opinion only
OpenPGP: RSA v4 4096b: 88AC E9B2 9196 305B A994 7552 F1BA 225C 0223 B187

```

## Junio C Hamano, 2014-06-19 17:10

Subject: Re: Conventions on struct copying?
Message-ID: <xmqqvbrwhlki.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqvbrwhlki.fsf%40gitster.dls.corp.google.com
In-Reply-To: <20140618233129.GK368384@vauxhall.crustytoothpaste.net>

```
"brian m. carlson" <sandals@crustytoothpaste.net> writes:

> I don't know of any place we explicitly copy structs like
> this,...

which should be a reason enough.  The first concrete guideline is
"just imitate the existing code".

> but I don't know of any prohibition against it, either.

So now you know ;-).

```

## Jason Pyeron, 2014-06-19 17:22

Subject: RE: Conventions on struct copying?
Message-ID: <5A7F0CEE2DEC4F1B825568DB8358D397@black>
URL: https://gitlist.dev/e/5A7F0CEE2DEC4F1B825568DB8358D397%40black
In-Reply-To: <xmqqvbrwhlki.fsf@gitster.dls.corp.google.com>

```
> -----Original Message-----
> From: Junio C Hamano
> Sent: Thursday, June 19, 2014 13:11
> 
> "brian m. carlson" <sandals@crustytoothpaste.net> writes:
> 
> > I don't know of any place we explicitly copy structs like
> > this,...
> 
> which should be a reason enough.  The first concrete guideline is
> "just imitate the existing code".
> 
> > but I don't know of any prohibition against it, either.
> 
> So now you know ;-).

To expand, on that do not trust the compiler to do deep copies.

http://stackoverflow.com/questions/2302351/assign-one-struct-to-another-in-c

Hit #1 on https://www.google.com/search?q=c+assignment+of+struct

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-                                                               -
- Jason Pyeron                      PD Inc. http://www.pdinc.us -
- Principal Consultant              10 West 24th Street #100    -
- +1 (443) 269-1555 x333            Baltimore, Maryland 21218   -
-                                                               -
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
This message is copyright PD Inc, subject to license 20080407P00.

```

## brian m. carlson, 2014-06-20 01:26

Subject: Re: Conventions on struct copying?
Message-ID: <20140620012608.GA856079@vauxhall.crustytoothpaste.net>
URL: https://gitlist.dev/e/20140620012608.GA856079%40vauxhall.crustytoothpaste.net
In-Reply-To: <5A7F0CEE2DEC4F1B825568DB8358D397@black>

```
On Thu, Jun 19, 2014 at 01:22:44PM -0400, Jason Pyeron wrote:
> > -----Original Message-----
> > From: Junio C Hamano
> > Sent: Thursday, June 19, 2014 13:11
> > 
> > "brian m. carlson" <sandals@crustytoothpaste.net> writes:
> > 
> > > I don't know of any place we explicitly copy structs like
> > > this,...
> > 
> > which should be a reason enough.  The first concrete guideline is
> > "just imitate the existing code".

Right.  The reason I asked is that most of our structures end up
containing pointers, so it wouldn't make sense to do the equivalent of a
memcpy on them anyways.  This is just data, so I thought it might be
different.  I'll implement an oidcpy function to do the work.

> > > but I don't know of any prohibition against it, either.
> > 
> > So now you know ;-).
> 
> To expand, on that do not trust the compiler to do deep copies.

I'm not.  The definition looks like;

  struct object_id {
    unsigned char sha1[20];
  };

I only want it to memcpy those 20 bytes; there's no pointers or anything
to complicate it.

-- 
brian m. carlson / brian with sandals: Houston, Texas, US
+1 832 623 2791 | http://www.crustytoothpaste.net/~bmc | My opinion only
OpenPGP: RSA v4 4096b: 88AC E9B2 9196 305B A994 7552 F1BA 225C 0223 B187

```
