www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Address of an array

reply David <kraxli77 gmail.com> writes:
Hi

The pointer (.ptr) of an array is the address of the first array 
element. But what exactly is the address of the array? And how is 
it used?

   auto myArray = [5, 10, 15, 20, 25, 30, 35];
   writeln("myArray.ptr: ", myArray.ptr); // myArray.ptr: 
7FFA95F29000
   writeln("&myArray[0]: ", &myArray[0]); // &myArray[0]: 
7FFA95F29000
   writeln("&myArray: ", &myArray); // &myArray: 7FFE4B576B10
   writeln("*&myArray: ", *&myArray); // *&myArray: [5, 10, 15, 
20, 25, 30, 35]
Oct 27 2016
next sibling parent David <kraxli77 gmail.com> writes:
On Thursday, 27 October 2016 at 16:13:34 UTC, David wrote:
 Hi

 The pointer (.ptr) of an array is the address of the first 
 array element. But what exactly is the address of the array? 
 And how is it used?

   auto myArray = [5, 10, 15, 20, 25, 30, 35];
   writeln("myArray.ptr: ", myArray.ptr); // myArray.ptr: 
 7FFA95F29000
   writeln("&myArray[0]: ", &myArray[0]); // &myArray[0]: 
 7FFA95F29000
   writeln("&myArray: ", &myArray); // &myArray: 7FFE4B576B10
   writeln("*&myArray: ", *&myArray); // *&myArray: [5, 10, 15, 
 20, 25, 30, 35]
Sorry that went to quickly ;-) I observe for example that the even if the pointer is moved to another address the address of the (dynamic) array stays constant: auto shrink = myArray[0 .. $-1]; writeln("shrink.ptr: ", shrink.ptr); writeln("&shrink: ", &shrink); Note: myArray and shrink have the same ptr but a different address shrink ~= 100; writeln("shrink.ptr: ", shrink.ptr); writeln("&shrink: ", &shrink); Note: After appending, shrink changes its ptr but its address stays the same. Thanks for your help in advance.
Oct 27 2016
prev sibling parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Thursday, October 27, 2016 16:13:34 David via Digitalmars-d-learn wrote:
 Hi

 The pointer (.ptr) of an array is the address of the first array
 element. But what exactly is the address of the array? And how is
 it used?

    auto myArray = [5, 10, 15, 20, 25, 30, 35];
    writeln("myArray.ptr: ", myArray.ptr); // myArray.ptr:
 7FFA95F29000
    writeln("&myArray[0]: ", &myArray[0]); // &myArray[0]:
 7FFA95F29000
    writeln("&myArray: ", &myArray); // &myArray: 7FFE4B576B10
    writeln("*&myArray: ", *&myArray); // *&myArray: [5, 10, 15,
 20, 25, 30, 35]
A dynamic array looks something kind of like this: struct DynamicArray(T) { size_t length; T* ptr; } So, if you take the address of a dynamic array, you're basically taking the address of a struct on the stack, whereas the address in ptr is the address in memory where the data is (be it GC-allocated memory, malloc-ed memory, or a static array on the stack somewhere). Similarly, if you have a class reference, and you take its address, you're taking the address of the reference, not the class object that it points to. e.g. class MyClass { string foo; } MyClass mc; auto addr = &mc; addr is the address of mc on the stack, whereas mc itself is null. - Jonathan M Davis
Oct 27 2016
parent David <kraxli77 gmail.com> writes:
 A dynamic array looks something kind of like this:

 struct DynamicArray(T)
 {
     size_t length;
     T* ptr;
 }

 So, if you take the address of a dynamic array, you're 
 basically taking the address of a struct on the stack, whereas 
 the address in ptr is the address in memory where the data is 
 (be it GC-allocated memory, malloc-ed memory, or a static array 
 on the stack somewhere).

 Similarly, if you have a class reference, and you take its 
 address, you're taking the address of the reference, not the 
 class object that it points to. e.g.

 class MyClass
 {
     string foo;
 }

 MyClass mc;
 auto addr = &mc;

 addr is the address of mc on the stack, whereas mc itself is 
 null.

 - Jonathan M Davis
Many thanks for your speedy and clear answer Jonathan! That makes even sense to me :-)
Oct 27 2016