0

I have written the following code:

#include <stdio.h>
#include <stdlib.h>


typedef struct _NeuralNetwork{

    int input_rows;
    int input_columns;
    double **inputs; 

}NeuralNetwork;



void main(){

    // structure variable
    NeuralNetwork *nn;

    int count;
    int i,j;

    nn->input_rows = 2;
    nn->input_columns = 3;

    // create the array of double pointers using # of rows
    nn->inputs = (double **)malloc(nn->input_rows * sizeof(double *));

    // each pointer gets an array of double values
    for (i=0; i<nn->input_rows; i++){
        nn->inputs[i] = (double *)malloc(nn->input_columns * sizeof(double));
    }

    // assign values 
    count = 0;
    for (i = 0; i < nn->input_rows  ; i++)
        for (j = 0; j < nn->input_columns; j++)
            nn->inputs[i][j] = ++count;  

    // print those values
    for (i = 0; i<nn->input_rows; i++)
        for (j = 0; j < nn->input_columns; j++)
            printf("%f ", nn->inputs[i][j]);


    /* Code for further processing and free the 
        dynamically allocated memory*/

    return;
}

When I compile this everything is okay. But after running it, I get a segmentation fault error:

Segmentation fault (core dumped)

I am not sure, where the mistake is. Can somebody help?

Note: When I use nn as structure variable instead of a structure, then everything is fine. But I want to use it as structure pointer and access the structure members via "->" and not via "." since I plan to pass nn as pointer to another function later.

Thank you in advance :)

ebeninki
  • 909
  • 1
  • 12
  • 34

2 Answers2

1

The variable nn is a pointer, but that pointer is never initialized. You subsequently read and dereference that pointer using an operation such as nn->input_rows = 2;. This invokes undefined behavior.

In this particular case, nn likely contains some garbage value. By dereferencing that pointer value, you are attempting to read from memory you probably aren't allowed to. This is what causes the crash.

By defining nn as an instance of a struct instead of a pointer, as you said you tried, you avoid this issue. You can still however pass a pointer to other functions by taking the address of this variable and passing that to the function, i.e.:

NeuralNetwork nn;
...
myfunction(&nn)
dbush
  • 205,898
  • 23
  • 218
  • 273
  • yeah, right. The "&" operator in combination with nn could also be a valid solution. Thanks for that, I will notice it. – ebeninki Feb 01 '18 at 13:51
0

First, do not use void main(), it's non-standard and would eventually cause problems. The right way is int main() or int main(int argc, char** argv). Remember to return a proper value at the end of the main function, possibly 0. Consult the reference here: main function

Second, if you use NeuralNetwork *nn; you must allocate some space for it in memory. It's a pointer to some memory address, if you don't allocate it who knows where it points. That's why you're getting the segfault. You must allocate memory for it in the following way:

NeuralNetwork *nn = malloc(sizeof(NeuralNetwork));

Then it should work properly.

SenselessCoder
  • 1,139
  • 12
  • 27
  • OOOOhhhhhhh right thx....I forget that, yes. I was so focused on the structure pointer that I ask myself if it could be a pointer dereferencing issue. Thank you also for the other advice :) – ebeninki Feb 01 '18 at 13:37
  • The rant about the format of `main()` is incorrect. `void main()` is a perfectly fine implementation-defined form of main() both on hosted and freestanding systems. In addition, since you wish to be pedantic, `int main()` is obsolete style, flagged for removal from the C language. The correct form is `int main (void)`. You would perhaps know this if you were reading a C reference instead of a C++ reference. More information [here](https://stackoverflow.com/a/31263079/584518). – Lundin Feb 01 '18 at 14:33
  • @Lundin that's good to know, thanks. – SenselessCoder Feb 01 '18 at 15:20