c - Struct of arrays, arrays of structs and memory usage pattern -


i've been reading soa , wanted try implement in system building up.

i writing simple c struct tests bit confused, right have 3 different struct vec3. show them below , go further details question.

struct vec3 { size_t x, y, z; };  struct vec3_a { size_t pos[3]; };  struct vec3_b { size_t* x; size_t* y; size_t* z; };  struct vec3 vec3(size_t x, size_t y, size_t z) {     struct vec3 v;     v.x = x;     v.y = y;     v.z = z;     return v; }  struct vec3_a vec3_a(size_t x, size_t y, size_t z) {     struct vec3_a v;     v.pos[0] = x;     v.pos[1] = y;     v.pos[2] = z;     return v; }  struct vec3_b vec3_b(size_t x, size_t y, size_t z) {     struct vec3_b v;     v.x = (size_t*)malloc(sizeof(size_t));     v.y = (size_t*)malloc(sizeof(size_t));     v.z = (size_t*)malloc(sizeof(size_t));     *(v.x) = x;     *(v.y) = y;     *(v.z) = z;     return v; } 

that's declarations of 3 types of vec3.

struct vec3 v = vec3(10, 20, 30); struct vec3_a va = vec3_a(10, 20, 30); struct vec3_b vb = vec3_b(10, 20, 30); 

printing out addresses printf values these:

size of vec3      : 24 bytes size of vec3a     : 24 bytes size of vec3b     : 24 bytes size of size_t    : 8 bytes size of int       : 4 bytes size of 16 int    : 64 bytes vec3 x:10, y:20, z:30 vec3 x:0x7fff57f8e788, y:0x7fff57f8e790, z:0x7fff57f8e798 vec3a x:10, y:20, z:30 vec3a x:0x7fff57f8e768, y:0x7fff57f8e770, z:0x7fff57f8e778 vec3b x:10, y:20, z:30 vec3b x:0x7fbe514026a0, y:0x7fbe51402678, z:0x7fbe51402690 

one final thing did create array of 10 struct vec3_b , printed out addresses returned these values.

    struct vec3_b vb3[10];     for(int = 0; < 10; i++)     {         vb3[i] = vec3_b(i, i*2, i*4);     }  index:0 vec3b x:0x7fbe514031f0, y:0x7fbe51403208, z:0x7fbe51403420 index:1 vec3b x:0x7fbe51403420, y:0x7fbe51403438, z:0x7fbe51403590 index:2 vec3b x:0x7fbe51403590, y:0x7fbe514035a8, z:0x7fbe514035c0 index:3 vec3b x:0x7fbe514035c0, y:0x7fbe514035d8, z:0x7fbe514035f0 index:4 vec3b x:0x7fbe514035f0, y:0x7fbe51403608, z:0x7fbe51403680 index:5 vec3b x:0x7fbe51403680, y:0x7fbe51403698, z:0x7fbe514036b0 index:6 vec3b x:0x7fbe514036b0, y:0x7fbe514036c8, z:0x7fbe514036e0 index:7 vec3b x:0x7fbe514036e0, y:0x7fbe514036f8, z:0x7fbe51403710 index:8 vec3b x:0x7fbe51403710, y:0x7fbe51403728, z:0x7fbe51403740 index:9 vec3b x:0x7fbe51403740, y:0x7fbe51403758, z:0x7fbe51403770 

questions:

  1. is implementation of struct vec3_b proper way setup struct of array?

  2. since vec_3b structure 24 bytes large, fit 2 plus 12 additional bytes in 1 modern cpu's cache line?

  3. if vec3_b proper way soa setup, having trouble addressing, put 10 vec3_b together.

looking @ hex values , decimal representations cannot see pattern leads me believe setup incorrect.

      ---------------x-----------------|----------------y-----------------|----------------z-----------------|  0|    0x7fbe514031f0 : 140455383675376 | 0x7fbe51403208 : 140455383675400 | 0x7fbe51403420 : 140455383675936 1|    0x7fbe51403420 : 140455383675936 | 0x7fbe51403438 : 140455383675960 | 0x7fbe51403590 : 140455383676304 2|    0x7fbe51403590 : 140455383676304 | 0x7fbe514035a8 : 140455383676328 | 0x7fbe514035c0 : 140455383676352 

  1. i can't think of occasion when vec_3b idea.

  2. note have find space 24 bytes of data pointers point at, , won't contiguous structure itself, have reduced effective cache size factor of 2 compared vec3 or vec_3a. each malloc() has minimum size; on 64-bit machine, @ least 16 bytes. 3 separate allocations 3 pointed @ values in vec_3b structure needs @ least 48 other bytes supporting data (plus 24 structure itself). doesn't fit in single cache line; it's not guaranteed placed fits 2 cache lines.

  3. n/a — question predicated on false assumption.


Comments