[arch-general] gcc: loop do not terminate

LANGLOIS Olivier PIS -EXT olivier.pis.langlois at transport.alstom.com
Tue May 14 11:47:08 EDT 2013


> 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.


________________________________
CONFIDENTIALITY : This e-mail and any attachments are confidential and may be privileged. If you are not a named recipient, please notify the sender immediately and do not disclose the contents to another person, use it for any purpose or store or copy the information in any medium.


More information about the arch-general mailing list