Created
March 31, 2012 15:17
-
-
Save isag/2266097 to your computer and use it in GitHub Desktop.
EAM - Moving the data inside a struct to an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// \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]; | |
} | |
// put the list pool into the matrix | |
//---------------------------------- | |
sllnode *node = ecosys.head; | |
for (int i = 0; node != NULL; i++, node = node->next) | |
{ | |
ParamSP sp = *(ParamSP*)(node->data); | |
double tempArray[21]; | |
tr_sp_array(&sp,tempArray); | |
memcpy((void*)m[i], (void*)tempArray,21*sizeof(double)); | |
} | |
// create an initial species list from a matrix pool | |
//-------------------------------------------------- | |
for(int i = 0 ; i < nrows_sub ; ++i) | |
{ | |
ParamSP *sp = (ParamSP*)malloc(sizeof(ParamSP)); | |
ParamSP tempSP; | |
tr_sp_struct(pool[positions[i]],&tempSP); | |
memcpy((void*)sp,(void*)&tempSP ,sizeof(ParamSP)); | |
sll_add_head(&ecosys,sp); | |
} | |
/* | |
// 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
Here is a way I use to do my transfers with memcpy. Do you think it could do a bug?