digitalmars.D.learn - A new instance of a variable?
- Ish (7/7) Nov 13 2015 foreach (i; 0..5) {
- Alex Parrill (7/14) Nov 13 2015 Just like in C, you'll have to allocate storage for each object
- Ish (7/23) Nov 13 2015 immutable int* j = new immutable int(i);
- Marc =?UTF-8?B?U2Now7x0eg==?= (9/37) Nov 13 2015 This compiles for me with 2.068, 2.069 and latest Git (Linux
- Ish (4/25) Nov 13 2015 I am using gdc w/gcc.4.9.2 (backend). So, I should have mentioned
- anonymous (19/22) Nov 13 2015 Looks like your D version is rather old. I had to go back to dmd 2.065
- Steven Schveighoffer (10/16) Nov 13 2015 This is not safe to do. What you are doing is passing a scope-allocated
foreach (i; 0..5) { immutable int j = i; etc. } I want each j to be assigned separate memory so that it can be passed to a calle function and not overwritten by next value of i in foreach()??
Nov 13 2015
On Friday, 13 November 2015 at 15:49:01 UTC, Ish wrote:foreach (i; 0..5) { immutable int j = i; etc. } I want each j to be assigned separate memory so that it can be passed to a calle function and not overwritten by next value of i in foreach()??Just like in C, you'll have to allocate storage for each object you create. immutable int* j = new immutable int(i); Though if you use `new`, you can let the GC free them. But if the data is immutable, then there's no point in allocating separate objects for each number, since they can't be changed.
Nov 13 2015
On Friday, 13 November 2015 at 16:06:51 UTC, Alex Parrill wrote:On Friday, 13 November 2015 at 15:49:01 UTC, Ish wrote:immutable int* j = new immutable int(i); gives error: locks1.d:27: error: no constructor for immutable(int); I need to allocate separate objects as the value is passed to a separate thread as: void incrementer(immutable int* nterm, shared(Lock) lock) { etc. }foreach (i; 0..5) { immutable int j = i; etc. } I want each j to be assigned separate memory so that it can be passed to a calle function and not overwritten by next value of i in foreach()??Just like in C, you'll have to allocate storage for each object you create. immutable int* j = new immutable int(i); Though if you use `new`, you can let the GC free them. But if the data is immutable, then there's no point in allocating separate objects for each number, since they can't be changed.
Nov 13 2015
On Friday, 13 November 2015 at 17:44:31 UTC, Ish wrote:On Friday, 13 November 2015 at 16:06:51 UTC, Alex Parrill wrote:This compiles for me with 2.068, 2.069 and latest Git (Linux x86_64): void main() { foreach(i; 0 .. 10000) { immutable int* j = new immutable int(i); } } Can you post more of your actual code?On Friday, 13 November 2015 at 15:49:01 UTC, Ish wrote:immutable int* j = new immutable int(i); gives error: locks1.d:27: error: no constructor for immutable(int); I need to allocate separate objects as the value is passed to a separate thread as: void incrementer(immutable int* nterm, shared(Lock) lock) { etc. }foreach (i; 0..5) { immutable int j = i; etc. } I want each j to be assigned separate memory so that it can be passed to a calle function and not overwritten by next value of i in foreach()??Just like in C, you'll have to allocate storage for each object you create. immutable int* j = new immutable int(i); Though if you use `new`, you can let the GC free them. But if the data is immutable, then there's no point in allocating separate objects for each number, since they can't be changed.
Nov 13 2015
On Friday, 13 November 2015 at 18:10:38 UTC, Marc Schütz wrote:On Friday, 13 November 2015 at 17:44:31 UTC, Ish wrote:I am using gdc w/gcc.4.9.2 (backend). So, I should have mentioned it before making so much noise. Code provided also does not compile.On Friday, 13 November 2015 at 16:06:51 UTC, Alex Parrill wrote:This compiles for me with 2.068, 2.069 and latest Git (Linux x86_64): void main() { foreach(i; 0 .. 10000) { immutable int* j = new immutable int(i); } } Can you post more of your actual code?[...]immutable int* j = new immutable int(i); gives error: locks1.d:27: error: no constructor for immutable(int); I need to allocate separate objects as the value is passed to a separate thread as: void incrementer(immutable int* nterm, shared(Lock) lock) { etc. }
Nov 13 2015
On 13.11.2015 18:44, Ish wrote:immutable int* j = new immutable int(i); gives error: locks1.d:27: error: no constructor for immutable(int);Looks like your D version is rather old. I had to go back to dmd 2.065 to see that error. 2.065 is from February 2014. We're at 2.069 now. I'd recommend updating the compiler. If that's not an option, here are two variants that work with 2.065. Allocate mutable, assign value, cast to immutable: ---- int* jm = new int; *jm = i; immutable int* j = cast(immutable) jm; jm = null; /* So that you don't accidentally alter the data through jm. */ ---- Pretty straight-forward, but verbose and not fool-proof at all. Array literal: ---- immutable int imm = i; /* or declare i immutable to begin with */ immutable int* j = [imm].ptr; ---- Looks nicer, but allocates more space than necessary.
Nov 13 2015
On 11/13/15 10:48 AM, Ish wrote:foreach (i; 0..5) { immutable int j = i; etc. } I want each j to be assigned separate memory so that it can be passed to a calle function and not overwritten by next value of i in foreach()??This is not safe to do. What you are doing is passing a scope-allocated variable to a function, and then examining the variable after the scope has exited (and essentially deallocated the variable). The reason it "changes" is because your dangling pointer to that data is now pointing at the newly allocated variable in the new loop scope. I would expect this code to behave strangely, and possibly crash. If you want a variable to exist beyond it's declared scope, you must allocate on the heap as Alex said. -Steve
Nov 13 2015