【问题标题】:array allocated by malloc gets lostmalloc 分配的数组丢失
【发布时间】:2016-03-19 04:44:19
【问题描述】:

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

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: 

【问题讨论】:

  • 能否为这两种情况添加打印代码?

标签: c arrays debugging malloc


【解决方案1】:

问题出在这两行:

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

malloc 行不影响network-&gt;layers[currentLayerIndex].neurons

假设network-&gt;layers[currentLayerIndex].neurons 的地址为X。之后

Layer l = network->layers[currentLayerIndex];

l.neurons 的地址为 X,因为它是从 network-&gt;layers[currentLayerIndex].neurons 复制而来的。

现在malloc 返回地址Y,分配给l.neurons,而network-&gt;layers[currentLayerIndex].neurons 仍然是X

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

编辑:

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

【讨论】:

  • 所以我应该将行更改为Layer* l = &amp;network-&gt;layers[currentLayerIndex] 并使用此指针?
  • @Jonas 这也是一个选项。您还可以将指针分配回network-&gt;layers。请参阅我对答案的编辑。
  • 谢谢@Alex Lop,您发现了错误,现在它正在工作。
猜你喜欢
  • 2016-11-10
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
  • 2015-08-04
  • 1970-01-01
  • 2020-11-26
  • 1970-01-01
  • 2021-11-13
相关资源
最近更新 更多