14 May
2013
14 May
'13
11:10 p.m.
On Tue, May 14, 2013 at 11:47 AM, LANGLOIS Olivier PIS -EXT <olivier.pis.langlois@transport.alstom.com> wrote:
void testing(void) { #define NR_STRUCT_A (4) struct B* ptr = malloc(sizeof(struct B) + NR_STRUCT_A*sizeof(struct A));
ptr->numelem = NR_STRUCT_A; ptr->item = (struct A*)(ptr+1);
ptr->item[0].a = 0; ptr->item[1].a = 1; ptr->item[2].a = 2; ptr->item[3].a = 3; ptr->item[4].a = 100; /* Wrong, violate memory accessing here! */ #undef NR_STRUCT_A }
IMO, this is a viable alternative. The only disadvantage is that it is taking an extra sizeof(struct A *) bytes of memory, if memory space is a concern.
You don't need an alternative, `array[]` is standard and does what you want.