Monday 24 June 2013

Constructors for C Structures

So, if you're not Chuck... consider using structures
in your C code.



Structures

Structures are part of ANSI-C and enable the programmer to structure a cleaner code. According to the ANSI C standard:

A structure type describes a sequentially allocated set of
member objects, each of which has an optionally specified name
and possibly distinct type.

In this post we suggest ways to instantiate a structure using "constructor" functions ((ab)using the terminology of OO-programming). We shall describe two types of constructor functions, each having different functionality and scope.

The copy-constructor

This constructor function creates memory copies of the data that you need to store. Take for example the following structure definition:

typedef struct {
    double *A;
    int n;
} something_t;
which stores a pointer-to-double and an integer which, here, is to be interpreted as the memory space that A allocates if it is not null. The following function hard-copies the content stored under the input argument (double *)A into a new memory location. After this instantiation, the created structure occupies a completely different memory space.

something_t *something_new(const double *A, const int n){
    something_t *p = NULL;
    if ((p =  malloc(sizeof (something_t)))!=NULL)
    {
        p->n=n;
        if (A!=NULL && n>0){
            p->A=malloc(n*sizeof(double));
            if (p->A==NULL) return NULL;
            memcpy(p->A, A, n*sizeof(double));
        } else { p->A=NULL; }
    } else { return NULL; }
    return p;   
}
This type of constructor function resembles the new keyword of C++ and Java for the construction of new Objects. Notice that is malloc is used to allocate n*sizeof(double) bytes for the variable p (of type pointer-to-structure) and then memcpy is employed to copy the values pointed by A into p->A. The pointer A can be modified or freed freely without affecting the created instance of something_t. Here is a complete running example also available on ideone.com (click the little icon on the upper right corner of the frame to clone the code, modify it and compile it online!). This function comes however with the burden of allocating new memory space and is not the best option if you are concerned about good management of your resources, unless you really need to copy the data. 




The pointer-constructor

Let us now take a look at the following simple constructor function where A and p->A point exactly to the same memory position:

What we have done here is that we modified A[0] after we created the structure. However we didn't copy the corresponding values to new memory places. Guess what the program will print (or better run it to be sure...) Yes, you guessed right - A[0]=123.456, unlike the previous constructor. However, this constructor function is a much faster solution if you are concerned about performance and good management of your resources. However, you should use it with good understanding of its properties. Combination of these two constructor-function approaches is also possible, and, of course, there's no golden rule - everything depends on what you have to do...

No comments:

Post a Comment