home bbs files messages ]

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

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

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

   Message 32,249 of 33,346   
   Johannes Schaub to All   
   Re: Most-derived objects objects shall h   
   05 May 12 06:19:50   
   
   ec995c3a   
   From: schaub.johannes@googlemail.com   
      
   Am 05.05.2012 08:39, schrieb Gene Bushuyev:   
   > On May 3, 2:25 pm, Johannes Schaub   
   > wrote:   
   >> The C++11 spec says at 1.8p5   
   >>   
   >> "Unless it is a bit-field (9.6), a most derived object shall have a   
   >> non-zero size and shall occupy one or more bytes of storage. Base   
   >> class subobjects may have zero size."   
   >>   
   >> The following is supposed to be valid   
   >>   
   >>      int *x = new int[0];   
   >>   
   >> The object created by "new int[0]" has zero size (int[0] == 0 *   
   >> sizeof(int)), but that contradicts the above quote.   
   >>   
   >> How is this to be interpreted?   
   >   
   > In my view the word "array" is overloaded in the standard, the rules   
   > are different for statically and dynamically allocated arrays though   
   > both are just called arrays. One meaning of the "array" is an object   
   > defined in the form of "T array[n1][n2]...", to which the   
   > non-negative compile-time const requirements are applied as well as   
   > sizeof(array) is defined as n*sizeof(T) in 5.3.3/2. A different   
   > meaning of the "array" is an object which is the result of an array   
   > form of new expression calling "operator new[]", which isn't   
   > actually the type of an array, for creation of which the   
   > non-negative integral expression is required and "sizeof(*new T[n])"   
   > is always positive as described in 5.3.4/7.   
   >   
      
   Does that mean that the following is undefined behavior, because the   
   pointer is only allowed to point to an object of the type which it   
   compounds? (aliasing-violation, c.f. 3.10/15)   
      
       int x[3] = {};   
       int(*p)[3] = &x;   
       p[0][0] = 42;   
        // valid so far   
      
       int n = 3;   
       p = (int(*)[3])new int[n];   
       p[0][0] = 42;   
         // undefined behavior?   
      
   According to you, if I understood you correctly, the object created by   
   the new-expression doesn't have the type "int[3]". What type is it?   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- 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