[arch-general] gcc: loop do not terminate
Daniel Micay
danielmicay at gmail.com
Tue May 14 19:10:30 EDT 2013
On Tue, May 14, 2013 at 11:47 AM, LANGLOIS Olivier PIS -EXT
<olivier.pis.langlois at 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.
More information about the arch-general
mailing list