www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - A new instance of a variable?

reply Ish <ratta1i cmich.edu> writes:
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
next sibling parent reply Alex Parrill <initrd.gz gmail.com> writes:
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
parent reply Ish <ratta1i cmich.edu> writes:
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:
 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.
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
next sibling parent reply Marc =?UTF-8?B?U2Now7x0eg==?= <schuetzm gmx.net> writes:
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:
 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.
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. }
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?
Nov 13 2015
parent Ish <ratta1i cmich.edu> writes:
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:
 On Friday, 13 November 2015 at 16:06:51 UTC, Alex Parrill 
 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. }
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?
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.
Nov 13 2015
prev sibling parent anonymous <anonymous example.com> writes:
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
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
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