Re: Get object address when creating it in for loop
On Monday, 5 May 2014 at 16:15:43 UTC, hardcoremore wrote: neurons[] = n; // here n always returns same adress You're taking the address of the pointer, which isn't changing. Just use plain n - when you new it, it is already a pointer so just add that value to your array.
Re: Get object address when creating it in for loop
On 5/5/2014 12:15 PM, hardcoremore wrote: How to get and address of newly created object and put it in pointer array? int maxNeurons = 100; Neuron*[] neurons = new Neuron*[](maxNeurons); Neuron n; for(int i = 0; i maxNeurons; i++) { n = new Neuron(); neurons[] = n; // here n always returns same adress } writefln(Thread func complete. Len: %s, neurons); This script above will print array with all the same address values, why is that? Thanks These sorts of questions should go in digitalmars.D.learn, but your problem is a simple typo here: neurons[] = n; That sets the *entire* array to n. You forgot the index: neurons[i] = n;
Re: Get object address when creating it in for loop
Hi Guys, Thanks so much for your reply. This fixes my problem like Adam D. Ruppe suggested: int maxNeurons = 100; Neuron[] neurons = new Neuron[](maxNeurons); Neuron n; for(int i = 0; i maxNeurons; i++) { n = new Neuron(); neurons[] = n; } But can you give me a more details so I can understand what is going on. What is the difference between Neuron[] neurons = new Neuron[](maxNeurons); and Neuron*[] neurons = new Neuron*[](maxNeurons); As I understand Neuron*[] should create array which elements are pointers? Is it possible to instantiate 100 objects in a for loop and get a address of each object instance and store it in array of pointers? Thanks
Re: Get object address when creating it in for loop
On Mon, 05 May 2014 16:15:42 + hardcoremore via Digitalmars-d digitalmars-d@puremagic.com wrote: How to get and address of newly created object and put it in pointer array? int maxNeurons = 100; Neuron*[] neurons = new Neuron*[](maxNeurons); Neuron n; for(int i = 0; i maxNeurons; i++) { n = new Neuron(); neurons[] = n; // here n always returns same adress } writefln(Thread func complete. Len: %s, neurons); This script above will print array with all the same address values, why is that? n gives you the address of the local variable n, not of the object on the heap that it points to. You don't normally get at the address of class objects in D. There's rarely any reason to. Classes always live on the heap, so they're already references. Neuron* is by definition a pointer to a class _reference_ not to an instance of Neuron. So, you'd normally do Neuron[] neurons; for your array. I very much doubt that you really want an array of Neuron*. IIRC, you _can_ get at an address of a class instance by casting its reference to void*, but I'm not sure, because I've never done it. And even then, you're then using void*, not Neuron*. Also FYI, questions like this belong in D.learn. The D newsgroup is for general discussions about D, not for questions related to learning D. - Jonathan M Davis
Re: Get object address when creating it in for loop
Hi Jonathan, Thanks for your reply. So actually I was getting the pointer of n itself. I understand now what was my problem. The problem was that I did not know that array support references of objects, so I thought that I must fill it with pointers of objects. But its great that I do not have to use pointers :) Thanks a lot.