malloc分配的数组丢失了

时间:2015-12-13 18:15:51

标签: c arrays debugging malloc

我试图实现一个神经网络,但初始化不起作用,但我无法发现我的错误:

typedef struct{
    int numWeights;
    double* weights;
    double wBias;
}NeuronTanh;

typedef struct{
    int numNeurons;
    NeuronTanh* neurons;
}Layer;

typedef struct{
    int numLayers;
    Layer* layers;
}Network;

//--------------------------------

void initializeNetwork(Network* network){
    //malloc stuff
    network->numLayers = NUMBER_LAYERS;
    network->layers = malloc(NUMBER_LAYERS * sizeof(Layer));
    network->layers[0].numNeurons = 1
    network->layers[1].numNeurons = 4
    network->layers[2].numNeurons = 2

    for(int currentLayerIndex=0; currentLayerIndex<network->numLayers;++currentLayerIndex){
        Layer l = network->layers[currentLayerIndex];
        l.neurons = malloc(l.numNeurons * sizeof(NeuronTanh));
        for(int j=0; j<l.numNeurons; ++j){
            if(currentLayerIndex==0){
                l.neurons[j].numWeights = 2;
            }else{
                l.neurons[j].numWeights = network->layers[currentLayerIndex-1].numNeurons;
            }
            l.neurons[j].weights = malloc((1+l.neurons[j].numWeights) * sizeof(double));
            randomizeNeuron(&(l.neurons[j]));
        }
    }
 printNetwork(*network);
}

我现在的问题是,在最里面的for循环中,我可以在 randomizeNeuron(...)之后打印所有权重,但是如果我想在函数末尾打印所有权重或神经元,神经元数组是NULL,而层数组初始化很好。为什么神经元(和权重)数组是NULL?

修改

 printNetwork(Network network){
    fprintf(stderr, "Layers:%i\n",network.numLayers);
    for(int numLayer = 0; numLayer<network.numLayers; ++numLayer){
        fprintf(stderr, "Layer %i -------------------\n",numLayer);
        for(int numNeuron=0; numNeuron<network.layers[numLayer].numNeurons; ++numNeuron){
            fprintf(stderr, "Neuron %i: ", numNeuron);
            fprintf(stderr, "number of neurons: %i: ", network.layers[numLayer].numNeurons);
            if(network.layers[numLayer].neurons != NULL){
                for(int numWeight=0; numWeight<network.layers[numLayer].neurons[numNeuron].numWeights; ++numWeight){
                    fprintf(stderr, "%f ",network.layers[numLayer].neurons[numNeuron].weights[numWeight]);
                }
                fprintf(stderr, "%f\n", network.layers[numLayer].neurons[numNeuron].wBias);
            }
        }
    }
}

输出

Layers:3
Layer 0 -------------------
Neuron 0: number of weights: 2: Neuron 1: number of weights: 2: Layer 1 -------------------
Neuron 0: number of weights: 4: Neuron 1: number of weights: 4: Neuron 2: number of weights: 4: Neuron 3: number of weights: 4: Layer 2 -------------------
Neuron 0: number of weights: 1: 

1 个答案:

答案 0 :(得分:2)

问题在于以下两行:

Layer l = network->layers[currentLayerIndex];
l.neurons = malloc(l.numNeurons * sizeof(NeuronTanh));

malloc的行不会影响network->layers[currentLayerIndex].neurons

让我们假设network->layers[currentLayerIndex].neurons的地址 X 。之后

Layer l = network->layers[currentLayerIndex];

l.neurons的地址 X ,因为它来自network->layers[currentLayerIndex].neurons

现在malloc返回地址 Y ,分配给l.neurons,而network->layers[currentLayerIndex].neurons仍为 X

因此network->layers[currentLayerIndex]不受进一步初始化的影响。所有这些初始化都在l上执行。

修改

可能的解决方案是在完成初始化后将l.neurons分配回network->layers[currentLayerIndex].neurons