访问冲突写入位置 0x00000003

Access violation writing location 0x00000003

所以,我正在编写一个 DQL 神经网络。当我通过调试器运行代码时,它抛出这个异常

Exception thrown at 0x00D05DFB in Deep Q-learning Neural
Network.exe: 0xC0000005: Access violation writing location 0x00000003.

相关代码如下:

神经元结构:

typedef struct neuron_t {

// not entirely relevant to this problem, but it's nicer to have the full picture

    float activation;
    float* outputWeights;
    float bias;
    float z;
    
    float dactv;
    float* dw;
    float dbias;
    float dz;
}Neuron;

图层结构:

typedef struct layer_t {
    int numberOfNeurons;
    Neuron* neu;
}Layer;

主要功能:

Layer* testTarget = NULL;
    Layer* test = NULL;
    int numberOfLayers = 3;

    int* neuronInlayer[] = { 2,3,4 };

    test = createPredictionArchitecture(test, testTarget, numberOfLayers, neuronInlayer); /* I plan to turn this into merely 
                                                                                          createPredictionArchitecture(test, testTarget, numberOfLayers, neuronInlayer) once this problem
                                                                                          is fixed.*/

createArchitecture函数的相关位:

int createArchitecture(Layer* predictionNetwork, Layer* targetNetwork, int numberOfLayers, int* neuronInEachLayer) {
    
    predictionNetwork = (Layer*)malloc(numberOfLayers * sizeof(Layer)); if (predictionNetwork == NULL) { fprintf(stderr, "Failed to allocate memory to 'predictionNetwork' in line %d\n ", __LINE__); exit(1); }
    else {
        printf("Memory successfully allocated to 'predictionNetwork'\n");
    }
    
    targetNetwork = (Layer*)malloc(numberOfLayers * sizeof(Layer)); if (targetNetwork == NULL) { fprintf(stderr, "Failed to allocate memory to 'predictionNetwork' in line %d\n ", __LINE__); exit(1); }

    else {
        printf("Memory successfully allocated to 'targetNetwork'\n");
    }
    Layer** targetNW = &targetNetwork;
              
                ...
}

我这样做时出现异常:

*targetNW[i] = createLayer(neuronInEachLayer[i]);

为什么会发生这种情况,我该如何解决? 另外,如果您需要查看更多代码,请务必说明。

嗯,我想通了;我没有为 targetNW 分配任何内存。