Allocating a fixed-size array on the heap

Regan Heath regan at netmail.co.nz
Wed Jan 30 09:32:23 PST 2008


David Wilson wrote:
> As I understand it, a variable of the type float[4] will already be a
> reference pointing to the heap. The pointer syntax I do not
> understand, and it doesn't seem particularly well documented.
> 
> As I understand it,
> 
>    float[4] a = new float[4];
>    float[4]* b = &a;
> 
> b is a word-sized pointer to a, which is 2 words (ptr, length). The
> first word, ptr, is pointing to the heap.
>> Am I missing something or is this just yet another instance of how
>> fixed-size arrays aren't treated as first-class citizens?
>>
> 
> Either you're missing something or I am. I think the pointer support
> is purely for C compatibility (but I may be terribly wrong).

A static array like "float[4] a;" isn't the same as a dynamic one in 
that it's not a struct in the form (ptr, length).  Instead, as the 
compiler which knows the length at compile time replaces all instances 
of "a.length" with an actual constant value, you can see that from this 
example:

import std.stdio;

void main()
{
	float[4] a;
	uint* pl = &a.length; //Error: constant 4u is not an lvalue
}

R



More information about the Digitalmars-d mailing list