D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7280 - Can't get address of array `.length` or `.ptr` properties
Summary: Can't get address of array `.length` or `.ptr` properties
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Windows
: P2 minor
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-01-12 09:06 UTC by Denis Shelomovskii
Modified: 2012-01-12 10:17 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Denis Shelomovskii 2012-01-12 09:06:56 UTC
---
void f() {
    void[] arr;
    size_t* _length = &arr.length; // Error: arr.length is not an lvalue
    void** _ptr = &arr.ptr;        // Error: cast(void*)arr is not an lvalue
}
---
What is the case not to behave like a struct of two elements?

Workaround:
---
size_t* arrayLengthRef(T)(ref T[] arr) {
    return (cast(size_t*)&arr);
}

T** arrayPtrRef(T)(ref T[] arr) {
    return (cast(T**)&arr) + 1;
}
---
Comment 1 Steven Schveighoffer 2012-01-12 10:17:01 UTC
arr.length is a read/write property.  Writing length is not a simple field set, it calls a runtime function.
arr.ptr is a read only property.

Setting these must be done in tandem, and is done via the slicing interface or via the length set operation.  You can circumvent as you say, but it should not be easy, since it is very dangerous.