www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Getting a reference to an immutable string

reply David Zhang <straivers98 gmail.com> writes:
Hi,

I've got an immutable string declared in module scope, and I 
attempted to get a pointer to its characters through both &str[0] 
and str.ptr. However, it appears to me that the string is 
behaving like a manifest constant, in that the pointer is null.

The language reference indicates that it has a location in memory 
and thus has a pointer.

So, my question is thus: Is this a bug in DMD, or is this just 
something I missed?

Thanks

David
Feb 10
parent reply ag0aep6g <anonymous example.com> writes:
On 02/10/2018 11:26 PM, David Zhang wrote:
 I've got an immutable string declared in module scope, and I attempted 
 to get a pointer to its characters through both &str[0] and str.ptr. 
 However, it appears to me that the string is behaving like a manifest 
 constant, in that the pointer is null.
 
 The language reference indicates that it has a location in memory and 
 thus has a pointer.
 
 So, my question is thus: Is this a bug in DMD, or is this just something 
 I missed?
The pointer should not be null, even when `str` is a manifest constant. But without code it's impossible to say if you're hitting a compiler bug or if you're doing something wrong.
Feb 10
parent reply David Zhang <straivers98 gmail.com> writes:
On Saturday, 10 February 2018 at 22:36:41 UTC, ag0aep6g wrote:
 On 02/10/2018 11:26 PM, David Zhang wrote:
 I've got an immutable string declared in module scope, and I 
 attempted to get a pointer to its characters through both 
 &str[0] and str.ptr. However, it appears to me that the string 
 is behaving like a manifest constant, in that the pointer is 
 null.
 
 The language reference indicates that it has a location in 
 memory and thus has a pointer.
 
 So, my question is thus: Is this a bug in DMD, or is this just 
 something I missed?
The pointer should not be null, even when `str` is a manifest constant. But without code it's impossible to say if you're hitting a compiler bug or if you're doing something wrong.
Ah, yeah. It appears to occur only when compiled in x86 mode. This is what I'm talking about: void createWindow( ... ) { assert( wndclassName.ptr ); //This fails HWND hwnd = CreateWindowW( wndclassName.ptr, //This too null, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, null, null, null, null ); } wstring wndclassName = "wndclass_name"w;
Feb 10
next sibling parent David Zhang <straivers98 gmail.com> writes:
Building with Visual Studio seems to be fine. This isn't an 
OptLink issue, is it?
Feb 10
prev sibling parent reply ag0aep6g <anonymous example.com> writes:
On 02/10/2018 11:46 PM, David Zhang wrote:
 This is what I'm talking about:
 
 void createWindow( ... ) {
 
      assert( wndclassName.ptr ); //This fails
 
      HWND hwnd = CreateWindowW(
          wndclassName.ptr, //This too
          null,
          0,
          CW_USEDEFAULT, CW_USEDEFAULT,
          CW_USEDEFAULT, CW_USEDEFAULT,
          null,
          null,
          null,
          null
      );
 }
 
 wstring wndclassName = "wndclass_name"w;
That's not enough code to reproduce the issue. This works as far as I see: ---- import core.sys.windows.windef: HWND; import core.sys.windows.winuser: CreateWindowW, CW_USEDEFAULT; void main() { assert( wndclassName.ptr ); HWND hwnd = CreateWindowW( wndclassName.ptr, null, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, null, null, null, null ); } wstring wndclassName = "wndclass_name"w; ---- But there is a recent regression on Windows that might be related. Do you also have a static constructor (`static this`) that uses `wndclassName`? If so, you might be hitting issue 18412. https://issues.dlang.org/show_bug.cgi?id=18412 If you don't have a static constructor, it might be a different bug. In that case, please provide complete code so that we can get to the bottom of it.
Feb 10
parent David Zhang <straivers98 gmail.com> writes:
On Saturday, 10 February 2018 at 22:59:18 UTC, ag0aep6g wrote:
 But there is a recent regression on Windows that might be 
 related. Do you also have a static constructor (`static this`) 
 that uses `wndclassName`? If so, you might be hitting issue 
 18412.

 https://issues.dlang.org/show_bug.cgi?id=18412

 If you don't have a static constructor, it might be a different 
 bug. In that case, please provide complete code so that we can 
 get to the bottom of it.
I have a static constructor that uses wndclassName to register the window class... at the top of the file. I think that's the bug.
Feb 10