C++ struct memory allocation -


this question has answer here:

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

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -