Interdependent classes

M

MathStuf

I am making a neural network where a layer keeps track of its parent
and child through the ctor. However, I have hit a snag on how to
initialize them in the wrapper class. In the wrapper class, I have an
pointer to a pointer of Layers. Then I malloc space for each. How do I
assign the parent/child relationship from this? I am thinking
something like this:

layers = (Layer **)malloc(numLayers * sizeof(Layer *));
for (int i = 0; i < numLayers; ++i)
layers = (Layer *)malloc(sizeof(Layer));
layers[0] = &Layer(NULL, layers[1], numNodes[0]);
for (int i = 1; i < numLayers - 1; ++i)
layers = &Layer(layers[i - 1], layers[i + 1], numNodes);
layers = &Layer(layers[i - 1], NULL, numNodes);

Only, I don't think that this is legal due to the temporary instances
of Layer. Any ideas on how to get this to work?
 
G

Gianni Mariani

MathStuf said:
I am making a neural network where a layer keeps track of its parent
and child through the ctor. However, I have hit a snag on how to
initialize them in the wrapper class. In the wrapper class, I have an
pointer to a pointer of Layers. Then I malloc space for each. How do I
assign the parent/child relationship from this? I am thinking
something like this:

layers = (Layer **)malloc(numLayers * sizeof(Layer *));
for (int i = 0; i < numLayers; ++i)
layers = (Layer *)malloc(sizeof(Layer));
layers[0] = &Layer(NULL, layers[1], numNodes[0]);
for (int i = 1; i < numLayers - 1; ++i)
layers = &Layer(layers[i - 1], layers[i + 1], numNodes);
layers = &Layer(layers[i - 1], NULL, numNodes);

Only, I don't think that this is legal due to the temporary instances
of Layer. Any ideas on how to get this to work?



I can't tell what you're trying to do.

Maybe this:

Layer * layers = (Layer *) malloc(numLayers * sizeof(Layer));

new (layers +0) Layer(NULL, layers+0, numNodes[0]);
for (int i = 1; i < numLayers - 1; ++i) {
new (layers + i) Layer(layers+i-1, layers+i+1, numNodes);
}
new (layers + i) Layer(layers[i - 1], NULL, numNodes);

.... remember to destruct everything - in reverse.

layers[numLayers - 1].~Layer();
for (int i = numLayers - 1; i >= 0; --i) {
layers.~Layer();
}

free(layers);
 
M

Massimo

I am making a neural network where a layer keeps track of its parent
and child through the ctor. However, I have hit a snag on how to
initialize them in the wrapper class. In the wrapper class, I have an
pointer to a pointer of Layers. Then I malloc space for each. How do I
assign the parent/child relationship from this? I am thinking
something like this:

layers = (Layer **)malloc(numLayers * sizeof(Layer *));
for (int i = 0; i < numLayers; ++i)
layers = (Layer *)malloc(sizeof(Layer));
layers[0] = &Layer(NULL, layers[1], numNodes[0]);
for (int i = 1; i < numLayers - 1; ++i)
layers = &Layer(layers[i - 1], layers[i + 1], numNodes);
layers = &Layer(layers[i - 1], NULL, numNodes);

Only, I don't think that this is legal due to the temporary instances
of Layer. Any ideas on how to get this to work?


You can't execute the constructor when you want to... it has to be executed
when the class is instantiated; you can allocate the space for the class
without running it (what you're doing with malloc()), but then you have no
way to manually run the constructor. By the way, creating such unconstructed
classes is something you really should avoid.

If you need to initialize a class after its instantiation, add to it an
Init() method that you can call whenever you need it.

Anyway, why do you need to allocate space and run constructors at different
times?

Your code should be

layers = new Layer*[numLayers];
layers[0] = new Layer(NULL,layers[1],numNodes[0]);
for (int i = 1; i < numLayers - 1; ++i)
layers = new Layer(layers[i - 1],layers[i + 1],numNodes);
layers = new Layer(layers[i - 1],NULL,numNodes);



Massimo
 
M

MathStuf

"MathStuf" <[email protected]> ha scritto nel messaggio

I am making a neural network where a layer keeps track of its parent
and child through the ctor. However, I have hit a snag on how to
initialize them in the wrapper class. In the wrapper class, I have an
pointer to a pointer of Layers. Then I malloc space for each. How do I
assign the parent/child relationship from this? I am thinking
something like this:
layers = (Layer **)malloc(numLayers * sizeof(Layer *));
for (int i = 0; i < numLayers; ++i)
layers = (Layer *)malloc(sizeof(Layer));
layers[0] = &Layer(NULL, layers[1], numNodes[0]);
for (int i = 1; i < numLayers - 1; ++i)
layers = &Layer(layers[i - 1], layers[i + 1], numNodes);
layers = &Layer(layers[i - 1], NULL, numNodes);

Only, I don't think that this is legal due to the temporary instances
of Layer. Any ideas on how to get this to work?

You can't execute the constructor when you want to... it has to be executed
when the class is instantiated; you can allocate the space for the class
without running it (what you're doing with malloc()), but then you have no
way to manually run the constructor. By the way, creating such unconstructed
classes is something you really should avoid.

If you need to initialize a class after its instantiation, add to it an
Init() method that you can call whenever you need it.

Anyway, why do you need to allocate space and run constructors at different
times?

Your code should be

layers = new Layer*[numLayers];
layers[0] = new Layer(NULL,layers[1],numNodes[0]);
for (int i = 1; i < numLayers - 1; ++i)
layers = new Layer(layers[i - 1],layers[i + 1],numNodes);
layers = new Layer(layers[i - 1],NULL,numNodes);

Massimo


Ah...forgot about 'new' (I'm not new (no pun intended) to C++, but I
haven't needed it much so far). Thanks.

--MathStuf
 
G

Gianni Mariani

MathStuf said:
Ah...forgot about 'new' (I'm not new (no pun intended) to C++, but I
haven't needed it much so far). Thanks.

To say that you must be missing somthing. The new operator is an
important part of the language.
 
G

Gianni Mariani

Gianni Mariani wrote:
....
I can't tell what you're trying to do.

Maybe this:

Layer * layers = (Layer *) malloc(numLayers * sizeof(Layer));

new (layers +0) Layer(NULL, layers+0, numNodes[0]);
for (int i = 1; i < numLayers - 1; ++i) {
new (layers + i) Layer(layers+i-1, layers+i+1, numNodes);
}
new (layers + i) Layer(layers[i - 1], NULL, numNodes);


new (layers + i) Layer(layers+i-1, NULL, numNodes);
fixed - should be pointer arithmetic not array reference
... remember to destruct everything - in reverse.

layers[numLayers - 1].~Layer();
^^^^^^^^^^^^^^^^^^ forgot to remove this line ........

Save someone else from picking this errors up ....
for (int i = numLayers - 1; i >= 0; --i) {
layers.~Layer();
}

free(layers);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top