Re: static array of pointers to dynamic arrays of ints problem...

2018-04-23 Thread Steven Schveighoffer via Digitalmars-d-learn
On 4/22/18 2:00 AM, WhatMeForget wrote: Surely a stupid mistake on my part, but why is the first array repeated? In addition to what Neia said, you shouldn't use pointers to dynamic arrays. Such things are really hard to allocate on the heap, since new int[] just makes an array,

Re: static array of pointers to dynamic arrays of ints problem...

2018-04-21 Thread Neia Neutuladh via Digitalmars-d-learn
On Sunday, 22 April 2018 at 06:00:15 UTC, WhatMeForget wrote: foreach(i, elem; a) { int[] temp = new int[](5); .. a[i] = &temp; } You're taking the address of a local variable and persisting it beyond the variable's scope. This is not safe in general; compilers

static array of pointers to dynamic arrays of ints problem...

2018-04-21 Thread WhatMeForget via Digitalmars-d-learn
Surely a stupid mistake on my part, but why is the first array repeated? import std.stdio; void main() { int[]*[2] a; // a static arrray holding pointers to dynamic arrays static int unique = 0; foreach(i, elem; a) { int[] temp = new int[](5); foreach(ref

Re: Pointers to Dynamic Arrays

2015-08-17 Thread Adam D. Ruppe via Digitalmars-d-learn
On Monday, 17 August 2015 at 13:27:19 UTC, Brandon Ragland wrote: If that is true, than passing it as _char[] file_ makes the most sense to me. A pointer copy doesn't hurt as bad as an array copy, of say, 100Kibibytes... Right. Knowing this helps to explain a lot btw: char[] foo; void func(

Re: Pointers to Dynamic Arrays

2015-08-17 Thread Brandon Ragland via Digitalmars-d-learn
On Monday, 17 August 2015 at 03:07:26 UTC, Adam D. Ruppe wrote: On Monday, 17 August 2015 at 02:45:22 UTC, Brandon Ragland wrote: [...] Short answer: pointers to slices are usually a mistake, you probably don't actually want it, but rather should be using a regular slice instead. [...]

Re: Pointers to Dynamic Arrays

2015-08-16 Thread Adam D. Ruppe via Digitalmars-d-learn
On Monday, 17 August 2015 at 02:45:22 UTC, Brandon Ragland wrote: Since Dynamic Arrays / Slices are a D feature, using pointers to these has me a bit confused... Short answer: pointers to slices are usually a mistake, you probably don't actually want it, but rather should be using a regular s

Re: Pointers to Dynamic Arrays

2015-08-16 Thread Nicholas Wilson via Digitalmars-d-learn
On Monday, 17 August 2015 at 02:45:22 UTC, Brandon Ragland wrote: Howdy, Since Dynamic Arrays / Slices are a D feature, using pointers to these has me a bit confused... Consider: Now what is especially confusing about this, is that the above seems to works fine, while this does not: if(

Re: Pointers to Dynamic Arrays

2015-08-16 Thread Freddy via Digitalmars-d-learn
On Monday, 17 August 2015 at 02:45:22 UTC, Brandon Ragland wrote: if(file[(*pos + i)] == '}'){ *pos += i; return; } That code doesn't do what you want it do. file is a ((char[])*) you are indexing the pointer(accessing invalid memory) and getting a char[].

Pointers to Dynamic Arrays

2015-08-16 Thread Brandon Ragland via Digitalmars-d-learn
Howdy, Since Dynamic Arrays / Slices are a D feature, using pointers to these has me a bit confused... Consider: string c2s(int* pos, char[]* file, int l){ char[] s; for(int i = 0; i < l; i++){ s ~= file[(*pos + i)]; } return s.dup; } Now what