www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to represent struct with trailing array member

reply Dibyendu Majumdar <d.majumdar gmail.com> writes:
Hi

I have C code where the struct has a trailing array member:

struct matrix {
   int rows;
   int cols;
   double data[1];
};

In C code this is allocated dynamically to be variable size. The 
array is used just as normal.

How should this be translated to D? Will D's array access allow 
data elements to be accessed beyond the size declared?

Thanks and Regards
Dibyendu
Jan 21 2016
next sibling parent reply Chris Wright <dhasenan gmail.com> writes:
On Thu, 21 Jan 2016 21:52:06 +0000, Dibyendu Majumdar wrote:

 Hi
 
 I have C code where the struct has a trailing array member:
 
 struct matrix {
    int rows;
    int cols;
    double data[1];
 };
 
 In C code this is allocated dynamically to be variable size. The array
 is used just as normal.
 
 How should this be translated to D? Will D's array access allow data
 elements to be accessed beyond the size declared?
 
 Thanks and Regards Dibyendu
D has bounds checking, which makes this awkward. You would be able to access the data using: double a = matrix.data.ptr[7]; The .ptr property is just a pointer to the first element of the array, and pointer indexing doesn't have bounds checking. A safer way of doing this is: struct matrix { int rows, cols; double[] data() { void* p = &this; p += this.sizeof; return (cast(double*)p)[0 .. rows * cols]; } } You must still take responsibility for allocating variables like this, but if you've done it in C, you're used to that. Furthermore, if you're using the GC, allocate memory as void[] if there's a possibility that the data will contain pointers. If you allocate, for instance, ubyte[] instead, the GC will assume no pointers. That means it can collect things that only have a pointer hidden in the ubyte[].
Jan 21 2016
parent reply Dibyendu Majumdar <d.majumdar gmail.com> writes:
On Friday, 22 January 2016 at 01:53:53 UTC, Chris Wright wrote:
 On Thu, 21 Jan 2016 21:52:06 +0000, Dibyendu Majumdar wrote:

 Hi
 
 I have C code where the struct has a trailing array member:
 
 struct matrix {
    int rows;
    int cols;
    double data[1];
 };
 
D has bounds checking, which makes this awkward. You would be able to access the data using: struct matrix { int rows, cols; double[] data() { void* p = &this; p += this.sizeof; return (cast(double*)p)[0 .. rows * cols]; } }
Right - I should use slices in other words. Thanks
Jan 22 2016
parent userABCabc123 <userABCabc123 skdhf.ri> writes:
On Friday, 22 January 2016 at 08:39:06 UTC, Dibyendu Majumdar 
wrote:
 On Friday, 22 January 2016 at 01:53:53 UTC, Chris Wright wrote:
 On Thu, 21 Jan 2016 21:52:06 +0000, Dibyendu Majumdar wrote:

 Hi
 
 I have C code where the struct has a trailing array member:
 
 struct matrix {
    int rows;
    int cols;
    double data[1];
 };
 
D has bounds checking, which makes this awkward. You would be able to access the data using: struct matrix { int rows, cols; double[] data() { void* p = &this; p += this.sizeof; return (cast(double*)p)[0 .. rows * cols]; } }
Right - I should use slices in other words. Thanks
The basic problem is that double data[1] in D is a static array, which is not a reference type so if you try to use its .ptr member you'll encounter many errors because as it is, your struct is totally POD and it fully resides on the stack. And the previous answer is erroneous because "p" is declared nowhere. struct matrix { int rows, cols; double[] data; } or struct matrix { int rows, cols; double* data; } are correct, although you still have to write the boring code to get the data in sync with rows and cols.
Jan 22 2016
prev sibling parent bearophile <bearophileHUGS lycos.com> writes:
Dibyendu Majumdar:

On Thursday, 21 January 2016 at 21:52:06 UTC, Dibyendu Majumdar 
wrote:
 How should this be translated to D? Will D's array access allow 
 data elements to be accessed beyond the size declared?
Take a look at the code I've written here: http://rosettacode.org/wiki/Sokoban#Faster_Version You can also add a constructor to such struct, for safety and disallow default construction. Bye, bearophile
Jan 21 2016