[ale] Copy a Structure - A C Question

Byron A Jeff byron at cc.gatech.edu
Fri Jun 14 07:53:15 EDT 2002


> 
> Good Morning ALE'rs:
> 
> This is a question for the C programmers out there. I have a situation 
> where I need to copy a rather large data structure to another memory 
> location.

OK.

> Is there a C function that I can use to do this rather than 
> going through the arduous task of allocating memory and coping each 
> element manually?

Each element manually? In one of very strange quirks of C, while arrays
generally cannot be copied wholesale by the assignment operator (due to
the definition of the name of an array), structures can. So this is perfectly
legal:

// Structure copy example-------------------------------------------------
struct mystruct one,two;

// ...

two = one; // Copy an entire struct including all the elements.

// End of example---------------------------------------------------------

> In addition, since this project is still in 
> development, the makeup of the structure may change and I don't want to 
> worry about having to keep my manual routine updated.

Agreed. But no matter. The assignment will copy all of the elements.

> The structure 
> contains a combination of 
>character arrays,

No problem.

> integer values

No problem.

> and pointers to other structures.

It depends here. If it's OK for both structures, the original and the copy, to
point to same 'other struct' (this is called a shallow copy in C++ speak), then
no problem. You'd have to do more if you wanted to clone a copy of the 'other
struct'. Simple assignment won't do that.


> Since this is a structure, is all the allocated 
> memory for the structure in one contiguous chunk?

Yes. But there's no guarantee that the individual elements are contiguous.
Often compilers will insert padding between the elements to maintain alignment.

> If so, could I then 
> use something like memcpy to do the job?

Sure. But why when a simple assignment does the job so well.

Here are the major differences between arrays and structs:

* The name of an array is defined as a pointer to the first element. A struct's
  name defines the struct itself.

* Assignment on arrays is illegal because array names cannot be assigned with
  new values. Struct assignment copies a struct as you would expect.

* Array parameters are pass by reference, since it implicitly passes a pointer
  to the array. Struct parameters are copied. One needs to watch that when
  passing a very large struct. It generally better to pass a pointer to the
  struct, even when you have no intention of changing it (use a const...)

* Arrays cannot be returned as the result of a function. Structs can.

In the end structs are much more like simple scalars and behave in very much
the same manner.

So just assign the structs and don't worry about it. It'll do the same thing
as the memcpy, but will be much more intuitive.

It's kind of funny how C's quirkiness with arrays throws folks off when 
structs come behind it...

BAJ

---
This message has been sent through the ALE general discussion list.
See http://www.ale.org/mailing-lists.shtml for more info. Problems should be 
sent to listmaster at ale dot org.






More information about the Ale mailing list