Skip to content

Instantly share code, notes, and snippets.

@isag
Created March 31, 2012 15:36
Show Gist options
  • Save isag/2266257 to your computer and use it in GitHub Desktop.
Save isag/2266257 to your computer and use it in GitHub Desktop.
EAM - Moving the data inside a struct to an array
// \param p0 The structure to copy in an array.
// \param param_sp An array of double with space for at least 6 elements (48 bytes).
inline double *tr_sp_array(ParamSP *p, double *param_sp)
{
param_sp[0] = (double)p->cycle_num;
param_sp[1] = (double)p->number;
param_sp[2] = (double)p->ab;
param_sp[3] = (double)p->pc;
param_sp[4] = p->input;
param_sp[5] = p->mass;
}
inline double *tr_array_sp(double *param_sp, ParamSP *p)
{
p->cycle_num = param_sp[0];
p->number = param_sp[1];
p->ab = param_sp[2];
p->pc = param_sp[3];
p->input = param_sp[4];
p->mass = param_sp[5];
}
/*
// Because the array is so small, you could store it on the stack (faster and no need to free), e.g.:
double x[6];
tr_sp_array(p0, x);
// You can also allocate it on the heap:
double *x = malloc(6 * sizeof(double));
tr_sp_array(p0, x);
// do stuff...
free(x);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment