Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Question about memcopying damned structs

Author: Dieter Buerssner

Date: 14:00:13 07/24/04

Go up one level in this thread


On July 24, 2004 at 12:56:11, Matthias Gemuh wrote:

>
>
>
>
>a) memcpy(&ChsStrct->PlyData[ChsStrct->nPly+1],
> &ChsStrct->PlyData[ChsStrct->nPly], sizeof(PLYDATA));
>
>b) ChsStrct->PlyData[ChsStrct->nPly+1] = ChsStrct->PlyData[ChsStrct->nPly];
>
>
>Will either of these copy the array element of type struct PLYDATA ?.

Yes.

>Any special thing to mention concerning speed, padding, aligninment ?

One wouldn't use the memcpy anymore. Nowadays typically compilers inline memcpy,
so probably no speed difference. If any, assignment should be faster. No need to
be concerned about padding in either case.

Long time ago, struct assignement was not possible in C (before the first ANSI C
Standard). One would need to use memcpy for struct assignments. One still sees
it in some books. But today, there is really no rason anymore, to use it. b)
also looks clearer (at least to me). And probably is less prone to errors.
memcpy expects void pointers. So you can assign non compatible structs with
memcpy without any warning of the compiler. The assignment would give an error.
Also, when you change the type, it might be easy to forget the sizeof argument.
The later can be avoided often by using

  memcpy(&a, &b, sizeof a);

instead of

  memcpy(&a, &b, sizeof(TYPE_OF_A));

Regards,
Dieter



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.