Skip to content

Commit 53d7ac0

Browse files
committed
fix shared mem misalignment error, close #118
1 parent 7f8a2ac commit 53d7ac0

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/mcx_utils.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,19 +1696,22 @@ int mcx_loadjson(cJSON *root, Config *cfg){
16961696

16971697
val=FIND_JSON_OBJ("InverseCDF","Domain.InverseCDF",Domain);
16981698
if(val){
1699-
cfg->nphase=cJSON_GetArraySize(val)+2; /*left-/right-ends are excluded, so added 2*/
1699+
int nphase=cJSON_GetArraySize(val);
1700+
cfg->nphase=nphase+2; /*left-/right-ends are excluded, so added 2*/
1701+
cfg->nphase+=(cfg->nphase & 0x1); /* make cfg.nphase even number */
17001702
if(cfg->invcdf)
17011703
free(cfg->invcdf);
17021704
cfg->invcdf=(float*)calloc(cfg->nphase,sizeof(float));
17031705
cfg->invcdf[0]=-1.f; /*left end is always -1.f,right-end is always 1.f*/
17041706
vv=val->child;
1705-
for(i=1;i<cfg->nphase-1;i++){
1707+
for(i=1;i<=nphase;i++){
17061708
cfg->invcdf[i]=vv->valuedouble;
17071709
vv=vv->next;
17081710
if(cfg->invcdf[i]<cfg->invcdf[i-1] || (cfg->invcdf[i]>1.f || cfg->invcdf[i]<-1.f))
17091711
MCX_ERROR(-1,"Domain.InverseCDF contains invalid data; it must be a monotonically increasing vector with all values between -1 and 1");
17101712
}
1711-
cfg->invcdf[cfg->nphase-1]=1.f; /*left end is always -1.f,right-end is always 1.f*/
1713+
cfg->invcdf[nphase+1]=1.f; /*left end is always -1.f,right-end is always 1.f*/
1714+
cfg->invcdf[cfg->nphase-1]=1.f;
17121715
}
17131716

17141717
val=FIND_JSON_OBJ("VoxelSize","Domain.VoxelSize",Domain);

src/mcxlab.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@ void mcx_set_field(const mxArray *root,const mxArray *item,int idx, Config *cfg)
721721
double *val=mxGetPr(item);
722722
if(cfg->invcdf) free(cfg->invcdf);
723723
cfg->nphase=(unsigned int)nphase+2;
724+
cfg->nphase+=(cfg->nphase & 0x1); // make cfg.nphase even number
724725
cfg->invcdf=(float*)calloc(cfg->nphase,sizeof(float));
725726
for(i=0;i<nphase;i++){
726727
cfg->invcdf[i+1]=val[i];
@@ -729,7 +730,8 @@ void mcx_set_field(const mxArray *root,const mxArray *item,int idx, Config *cfg)
729730
}
730731
cfg->invcdf[0]=-1.f;
731732
cfg->invcdf[nphase+1]=1.f;
732-
printf("mcx.invcdf=[%ld];\n",nphase);
733+
cfg->invcdf[cfg->nphase-1]=1.f;
734+
printf("mcx.invcdf=[%ld];\n",cfg->nphase);
733735
}else if(strcmp(name,"shapes")==0){
734736
int len=mxGetNumberOfElements(item);
735737
if(!mxIsChar(item) || len==0)

0 commit comments

Comments
 (0)