0

I'm working on converting a neural network simulator from Perl to C. I have it working, but I'm not happy with part of my code. I've defined a struct network (typedefed to NETWORK) which contains a pointer to an array of doubles and a pointer to an array of pointers to NEURONs, which is another struct. Here is how they are defined:

typedef struct neuron {
    double *inputweights;
    double *neuronweights;
    double value;
} NEURON; 

typedef struct network {
    NEURON **neurons;
    double *outputs;
} NETWORK;

Initially, I tried to initialize these like this:

NEURON* myvariable;

But of course that didn't work because no memory was actually assigned. I know that I can initialize it like this:

NEURON myvariable;
NEURON* ptr = &myvariable;

But when I tried to do that into a loop, and stored the pointers in an array, it seemed like the previous pointers were lost or reset each iteration, and I was getting all sorts of errors. I was doing something like this:

NETWORK mynetwork;
for (i = 0; i < NEURON_COUNT; i++) {
    NEURON myneuron;
    reset_neuron(&myneuron); // Basically a zero fill of the arrays
    mynetwork->neurons[i]=&myneuron;
    myneuron->inputweights[0] = 1; // Sets the current neuron, first input
    printf("First neuron, first input is %f\n", mynetwork->neurons[0]->inputweights[0]);
    // The printf gives 1 on the first iteration, and 0 on every following iteration.
}

This gives me the impression that myneuron is always the /same/ memory location, even though I'm still keeping a pointer to the last one, so I keep resetting the same place. Of course I can also use malloc to force each neuron to be different:

NEURON* myvariable = malloc(sizeof(NEURON));

And that works, but that seems a bit like a kludge. Should I be bothering to use pointers to my structs at all? Is there a way to initialize a struct pointer without resorting to the low-level malloc and sizeof?

2
  • i know you want 'NEURON myneuron;' to be some form of declaration - it is, but after loop ends, your object disappears aswell. that's why we have malloc
    – fazo
    Oct 17, 2011 at 20:50
  • Cool, a random ninja-downvote to a two year old question.
    – Dan
    Sep 20, 2013 at 14:09

1 Answer 1

3

That's exactly what malloc is for. When you need a varying number of objects of a particular type and need to control where they are allocated, you use malloc. Why does it seem like a kludge?

1
  • I suppose it's just that I'm not used to the low level memory management, it seems like allocating memory myself is something that should only be done in unusual situations, but if you say that's how I should do it that's OK then. I just obviously need to make sure that all of the pointers in the struct are also initialized with malloc(). Any comments on how the structures were laid out? I'm wondering if they could be designed better, or if there's a reason to put a pointer to an array/struct in the struct rather than the array/struct itself?
    – Dan
    Oct 17, 2011 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.