C++ struct memory allocation -
this question has answer here:
- struct members memory layout 5 answers
in c++ if define structure this
struct complexfloat { float r; float i; };
and declare 2 variables this
struct complexfloat cf; float *f=(float*)cf;
can safely assume following condition true
(&(cf.r)==&(f[0]) && &(cf.i)==&(f[1]))
?
in other words, can safely assume in struct containing floats, elements occupy contiguous positions in memory , ordered according order in appear in definition of struct?
i tested , case gcc 4.8.2 on ubuntu, want make sure valid.
no cannot assume in right place unless control alignment of data in struct. there might alignment padding between r , i
Comments
Post a Comment