Dear C programmers,
I am having some serious issues when trying to dynamically allocate memory for a complex (at least to me) struct I need to use in order to input experimental data into my program.
The data consists of a number of trajectory clusters, each with a number of trajectories, each one of these defined by an x,y,z point over a number of timesteps. In addition, I assign different rgb colors for each cluster and other small details. The problem is that I do not know the number of clusters, trajectories, or timesteps until I read in the data file. Hence the need for dynamic allocation.
The structs I came up with to handle my data are:
Code:
typedef struct _timestepType {
int id;
float x,y,z;
} timestepType;
typedef struct _trajectoryType {
timestepType *timestep;
} trajectoryType;
typedef struct _clusterType {
float r,g,b;
trajectoryType *trajectory;
} clusterType;
clusterType *Cluster;
I.e., I work with objects that look like the following
Code:
Cluster[i].r
Cluster[i].trajectory[j].timestep[k].x
I confess that, even after a good dose of online tutorials, I am still very confused as to how to handle the dynamic memory allocation of something like this. A search in this forum showed me some posts with somewhat similar issues, but I am afraid I am still quite in the dark.
Could anyone help me out with this?
Thanks very much,
mc61