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,091 of 33,346    |
|    Ulrich Eckhardt to All    |
|    Re: allocate memory 'inside' POD    |
|    04 Apr 12 11:46:48    |
   
   From: ulrich.eckhardt@dominolaser.com   
      
   Am 03.04.2012 20:33, schrieb Thomas Mang:   
   > Consider the need for a POD-struct which can store a given number of   
   > double values, where this number is determined at run-time (though then   
   > fixed). The struct itself must not use a pointer type. Access to the   
   > double values shall be possible through members of the POD; moreover,   
   > each POD instance and its associated double values shall cover   
   > contiguous memory; if there is an array of these PODs then the whole   
   > array shall also be in contiguous memory[..].   
      
   So not only is the type itself of variable size, but you also want to   
   build arrays of this type. Since C[++] assumes fixed-size objects in   
   arrays, you can already say goodby to the normal array[i] indexing.   
   Building a singly-linked list with jump offset, like you did, should be   
   possible though.   
      
      
   > I could treat the bytes at vals and beyond as an array of doubles, but   
   > according to 3.8/2 it seems that a single memory region can only be of   
   > one type at a time; if it's the vals member of a foo object than I can   
   > access it but can't increment the pointer to reach all elements; if it's   
   > interpreted as an array of doubles then technically there would be no   
   > foo object any more and I would not be allowed to access it through the   
   > foo interface.   
   >   
   > Any possibility to making it fully C++ compliant while sticking to above   
   > constraints?   
      
   Looking at your code, I'd say it will work in pretty much any   
   environment you are likely to encounter. For me, that would be enough in   
   most cases.   
      
      
   Anyhow, what you can do is this:   
      
   union element   
   {   
    size_t length;   
    double value;   
   };   
   element array[5];   
      
   array[0].length = 2;   
   array[1].value = 1.0;   
   array[2].value = 3.2;   
   array[3].length = 1;   
   array[4].value = 42.1593;   
      
   The only problem you have is that double (or whatever type you have   
   there) might be smaller than size_t. In that case, you could get away   
   with storing multiple values in one array element.   
      
   Uli   
      
      
   --   
    [ 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