digitalmars.D.learn - Pointer to string allows assigning int[]?
- cal (15/15) Nov 12 2012 Is the following a bug?
- simendsjo (8/23) Nov 12 2012 It's not a bug.
- cal (2/9) Nov 12 2012 Ah right of course, thank you.
- Andrej Mitrovic (2/3) Nov 12 2012 It's a bug, he's overwriting immutable data.
- Andrej Mitrovic (2/5) Nov 12 2012 Ahh sorry I completely misread the code. There's no bug here actually.
- Andrej Mitrovic (10/11) Nov 12 2012 Reduced:
- Andrej Mitrovic (2/4) Nov 12 2012 My bad, the string contents aren't being modified. Not a bug.
Is the following a bug? import std.c.string, core.memory; void* makeNew(T)() { T t = T.init; void* ptr = GC.malloc(T.sizeof); memcpy(ptr, &t, T.sizeof); return ptr; } void main() { alias string T; T* iptr = cast(T*)makeNew!(T); (*iptr) = [1,2,3]; // this is allowed }
Nov 12 2012
On Monday, 12 November 2012 at 21:03:51 UTC, cal wrote:Is the following a bug? import std.c.string, core.memory; void* makeNew(T)() { T t = T.init; void* ptr = GC.malloc(T.sizeof); memcpy(ptr, &t, T.sizeof); return ptr; } void main() { alias string T; T* iptr = cast(T*)makeNew!(T); (*iptr) = [1,2,3]; // this is allowed }It's not a bug. string is an alias for immutable(char)[]. ubyte can be implicitly converted to char. All your numbers are less than 256, so dmd is able to convert them to char. If you try this, it fails. string s = [256]; // 256 is > char.max
Nov 12 2012
On Monday, 12 November 2012 at 21:08:43 UTC, simendsjo wrote:It's not a bug. string is an alias for immutable(char)[]. ubyte can be implicitly converted to char. All your numbers are less than 256, so dmd is able to convert them to char. If you try this, it fails. string s = [256]; // 256 is > char.maxAh right of course, thank you.
Nov 12 2012
On 11/12/12, simendsjo <simendsjo gmail.com> wrote:It's not a bug.It's a bug, he's overwriting immutable data.
Nov 12 2012
On 11/12/12, Andrej Mitrovic <andrej.mitrovich gmail.com> wrote:On 11/12/12, simendsjo <simendsjo gmail.com> wrote:Ahh sorry I completely misread the code. There's no bug here actually.It's not a bug.It's a bug, he's overwriting immutable data.
Nov 12 2012
On 11/12/12, cal <callumenator gmail.com> wrote:Is the following a bug?Reduced: void main() { string x = "foo"; string* y = &x; *y = [1, 2, 3]; assert(x == "foo"); // fails } Yes this is a bug, especially since the string is typed as immutable(char)[].
Nov 12 2012
On 11/12/12, Andrej Mitrovic <andrej.mitrovich gmail.com> wrote:Yes this is a bug, especially since the string is typed as immutable(char)[].My bad, the string contents aren't being modified. Not a bug.
Nov 12 2012