home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 241,261 of 243,242   
   Michael S to pozz   
   Re: Nice way of allocating flexible stru   
   08 Oct 25 18:52:17   
   
   From: already5chosen@yahoo.com   
      
   On Wed, 8 Oct 2025 09:09:40 +0200   
   pozz  wrote:   
   >   
   > And another question. Suppose I need an array of struct S. All   
   > elements have 7-bytes array[] member. How to allocate this array and   
   > access each element?   
   >   
   > I think I can't use the first malloc (17), neither the second (14).   
   > Both aren't a multiple of alignment length.   
   >   
      
   That's very good question.   
   IMHO, the best practical answer is: don't!   
      
   I mean, don't use structures with flexible array members except as   
   individual objects or as *last* field of other structure.   
      
   If you feel that the data structure that looks like array of structures   
   with FAM is the best fit for your requirements or if it happens to be   
   the structure that matches an external layout that you have to deal   
   with then handle the situation in imperative rather than declarative   
   manner. Manually impose headers and data arrays on array of char.   
      
   If you feel that alignment can cause troubles then don't hesitate to   
   memcpy into and out of local variable. Don't fall into trap of   
   premature optimization.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca