threads / discuss / 36959

Conventions on struct copying?

Subject: Conventions on struct copying?

## tl;dr

4 messages between Jun 18, 2014 and Jun 20, 2014.

replies: 3people: 3as markdown or json

brian m. carlson· Jun 18, 2014, 23:31 UTC · lore

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· Jun 19, 2014, 17:10 UTC · re: brian m. carlson · lore

Re: Conventions on struct copying?

"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· Jun 19, 2014, 17:22 UTC · re: Junio C Hamano · lore

RE: Conventions on struct copying?

Show 15 quoted lines
> -----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· Jun 20, 2014, 01:26 UTC · re: Jason Pyeron · lore

Re: Conventions on struct copying?

On Thu, Jun 19, 2014 at 01:22:44PM -0400, Jason Pyeron wrote:
Show 11 quoted lines
> > -----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.

Show 5 quoted lines
> > > 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

← back to recent threads