-3

Hello I am writing an algorithm for back propagation in c++, it consists of 1 input layer 1 hidden layer and 1 output layer. I am curious how to initialize my inputlayer, hiddenlayer, and outputlayer.

This is what i got:

struct Neuron {
double activation;
double bias;
double *incomingWeights;
};

struct Network {
double *targetLayer;
Neuron *inputLayer;
Neuron *hiddenLayer;
Neuron *outputLayer;
};

ann.inputLayer = malloc(din * sizeof * Neuron); //ERROR
for (int i = 0; i < din; i++) {
    ann.inputLayer[i].activation = 0;
    ann.inputLayer[i].bias = 0;
    ann.inputLayer[i].incomingWeights = NULL;
}

ann.outputLayer = malloc(dout * sizeof * Neuron); //ERROR
for (int i = 0; i < dout; i++) {
    ann.outputLayer[i].activation = 0;
    ann.outputLayer[i].bias = 0;
    ann.outputLayer[i].incomingWeights = new double[dhid];
}

din represents the number of neurons in my input layer. dhid represents the number of neurons in my hidden layer. dout repreesnts the number of neurons in my output layer. i didnt bother showing the initialization of my hidden layer cause its not important thank you

i wrote //ERROR on the lines where errors are shown according to my IDE(netbeans)

jamalsabs
  • 119
  • 3
  • What is the question? – David Heffernan Apr 14 '13 at 19:02
  • What do you think is wrong with the code you have written? It does have some issues but it helps if you explain yourself rather than we guess. – john Apr 14 '13 at 19:05
  • If you use `new` instead of `malloc`, the constructor method would be called after memory allocation. The constructor is where you should initialized the objects. – Thomas Matthews Apr 14 '13 at 19:05
  • main.cpp:138:50: error: expected primary-expression before ‘)’ token that is the error that i am getting it refers to the way i am using malloc, which makes me think i am not using malloc correctly – jamalsabs Apr 14 '13 at 19:06
  • Another method is to use `std::vector` and copy the items during the construction of the `vector`. – Thomas Matthews Apr 14 '13 at 19:06
  • Please edit your post to show `main.cpp:138:50`. – Thomas Matthews Apr 14 '13 at 19:07
  • The code posted is legal C. It shouldn't compile with a C++ compiler, however. Which leads to the question: should it be C or C++. If the intent is to write code in C, then the label should be C (and if the above code doesn't compile, it's probably because you're using a C++ compiler). If the intent is to write code in C++, then there should be no pointers and no dynamic allocation in the code, at least not for the parts we've been shown. – James Kanze Apr 14 '13 at 19:13
  • In general, we need the error message as well as the location; in this case, the errors are trivial. – Alex Chamberlain Apr 14 '13 at 19:15
  • Very similar to [this question](http://stackoverflow.com/q/15961835/1084416). – Peter Wood Apr 14 '13 at 19:43

3 Answers3

3

First, you never want to do default initializations in calling code, you can create a constructor for your struct and do it there:

struct Neuron {
   Neuron() : activation(0.0), bias(0.0), incomingWeights(0) {}
   double activation;
   double bias;
   double *incomingWeights;
};

Then (since this is C++)

for (int i = 0; i < din; i++) {
   ann.inputLayer[i] = new Neuron[din]; 

You should use new over malloc in C++, though if you ever did need malloc your statement would need to be fixed, to:

ann.inputLayer = (Neuron*)malloc(din * sizeof(Neuron)); 
Matt Phillips
  • 9,465
  • 8
  • 44
  • 75
1

Don't use malloc in C++.

As a minimal change to what you have, change this:

ann.inputLayer = malloc(din * sizeof * Neuron);

To this:

ann.inputLayer = new Neuron[din];

new here is more clear for readers of your code, performs construction when needed, and returns the correct pointer type.

(Same with the dout line)

Edit:

You would also likely find std::vector much easier to manage over pointers.

Community
  • 1
  • 1
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • No. Don't use `new[]` either. `inputLayer` should be an `std::vector`. (In fact, I don't think there should be any pointers in this code.) – James Kanze Apr 14 '13 at 19:10
  • @JamesKanze I completely agree with your reasoning, although your suggestion would require changes in unposted code. That would be a good lesson for another day. :) – Drew Dormann Apr 14 '13 at 19:13
0

Just for the record: the reason you're getting errors is because you are compiling C code with a C++ compiler. malloc returns a void*. In C, this converts implicitly to any other pointer type; in C++, you need an explicit conversion.

Except that, as others are saying as well, you should never use malloc in C++ (unless for reasons of compatibility with C—i.e. you are passing the pointer to a function written in C which will call free on it). In fact, in the little you've shown, there should be no pointers and no dynamic allocation whatsoever: define your types, add constructors, and when you need more than one, use std::vector.

James Kanze
  • 150,581
  • 18
  • 184
  • 329