Memory allocation in D (noob question)

Sean Kelly sean at f4.ca
Wed Dec 5 08:05:09 PST 2007


Regan Heath wrote:
> Steven Schveighoffer wrote:
>> import std.stdio;
>>
>> struct X
>> {
>>         char[5] myArray;
>>         int x;
>> }
>>
>> void main()
>> {
>>         X[] x = new X[2];
>>         x[0].myArray[] = "hello";
>>         char[] myslice = x[0].myArray[0..3];
>>         writefln("%x %x %x", &x[0].x, &x[0].myArray[0], &myslice[0]);
>>         myslice ~= "hithere";
>>         writefln("%x %x %x", &x[0].x, &x[0].myArray[0], &myslice[0]);
>>         writefln("%s %d", x[0].myArray, x[0].x);
>> }
>>
>> output:
>>
>> 868FE8 868FE0 868FE0
>> 868FE8 868FE0 868FE0
>> helhi 25970
> 
> This one worries me.
> 
> I believe the problem is caused by the memory address of myArray[0] 
> being the same as the memory address of the struct.  Is this what you 
> realised Sean... I may be a bit slow on the uptake here :)

Yes :-)


Sean



More information about the Digitalmars-d mailing list