Memory allocation in D (noob question)

Don Clugston dac at nospam.com.au
Sat Dec 1 00:55:07 PST 2007


mandel wrote:
> Robert Fraser Wrote:
> 
>> mandel wrote:
>>> It probably is a noob question,
>>> but aren't array lengths just hidden size_t values
>>> that are passed around?
>>> Why do we need to allocate space for them, too?
>>>
>>> voif foo()
>>> {
>>>   size_t length;
>>>   char* ptr; //allocated memory of 2^n
>>>   //.. the same as..?
>>>   char[] data;
>>> }
>> The extra space allocated isn't for the length (in fact, it's just a 
>> byte I think); it's to make checking for array bounds errors possible 
>> (since there's a byte of space that, if accessed, indicates an 
>> overflow). I tmight be used for something else, too.
> 
> Thanks, that answers my question.
> But I can't think how it could be used for array bounds errors checking right now.
> Well, I guess there some ng post about this, somewhere.
> But the page allocation overhead looks ugly for a language like D.
> 
> Anyway, good to have D arrays. Working with pointers in C was often
> ready for surprises in case of reduced attention. :>

An observation...
In my experience, most pointer bugs are actually uninitialised variables.
An uninitialised pointer is a truly horrible thing. But since D initialises 
variables, pointers in D aren't nearly as bad as in C.



More information about the Digitalmars-d mailing list