From: 643-408-1753@kylheku.com   
      
   On 2025-10-08, Michael S wrote:   
   > On Wed, 8 Oct 2025 15:23:09 -0000 (UTC)   
   > Kaz Kylheku <643-408-1753@kylheku.com> wrote:   
   >   
   >>   
   >> Ah well, you can lead an ass to water ...   
   >>   
   >   
   >   
   > IMHO, your sarcasm is unwarranted. Read the whole post of pozz.   
   > I seems to me that [in the first half of his post] pozz cares about   
   > things that are not worth carrying (few more or few less byte requested   
   > from malloc, where in practice malloc rounds requested size up at least   
   > to a multiple of 8, but more likely of 16), but it is obvious that he   
   > fully understood your earlier suggestion.   
      
   Fair enough.   
      
   My view is that applying sizeof to a structure with a flexible array   
   member is foolhardy, especially in the context when we want to allocate   
   space for that array and use it.   
      
   That is to say the only situation in which it makes sense is if   
   we need an instance of the structure which doesn't use the array.   
      
   Even then, it won't hurt us to only allocate offsetof(S, A),   
   since we don't touch A.   
      
   Only if we are allocating an array of such structures (which won't be   
   using the flexible arrays of any of them other than possibly the last   
   one) does the size come into play. The padding is then necessary for the   
   usual reason so that the members are correctly aligned in every element   
   of the array of structs.   
      
   The wording about the padding changed between the C99 draft, C99 and a   
   later standard. IIRC, C99 appeared to give the requirement that the   
   size of the struct had to be the offset of the array, and that was   
   backpedaled out.   
      
   The important thing is that the padding bears no relation to the   
   flexible array. Under no circumstances do you want to pretend   
   that &S + 1 is a suitable pointer for the base of the array,   
   rather S.A. &S + 1, which includes the padding, might not be   
   suitably aligned for the element type of A.   
      
   It is not necessary that offseotf(S, A[n]) calculation produce   
   a value that is at least as large as sizeof(S). If you need the array to   
   be [1] or [2] and that happens to fid into the padding, with padding   
   left over, that's okay.   
      
   You do not want to use sizeof(S) as the base for calculating   
   the necessary space.   
      
   If you allocate sizeof(S) + N * sizeof(T) where T is the element type,   
   but then correctly access P->A[0] through P->A[N-1], you have   
   over-allocated: your allocation includes unnecessary pading after   
   A[N-1].   
      
   I would like to see a compiler option which diagnoses when sizeof is   
   applied to an structure which ends in a flexible array member, or a   
   structure which has such a structure as its last member directly or,   
   recursively.   
      
   --   
   TXR Programming Language: http://nongnu.org/txr   
   Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal   
   Mastodon: @Kazinator@mstdn.ca   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|