'Access violation writing location 0x00000003

So, I'm writing a DQL Neural network. When I run the code through the debugger, it throws this exception

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

Here is the relevant code:

The neuron structure:

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;

The layer structure:

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

the main function:

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.*/

The relevant bit of the createArchitecture function:

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;
              
                ...
}

The exception occurs when I do this:

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

Why does this happen, and how should I fix it? And also, do say if you need to see more of the code.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source