csegmentation-faultinitializationnested-loopsnull-pointer

For loop segmentation fault


I am attempting to create a DVR simulation in C. I know that I need a nested for loop, but whenever I compile and run the program it produces a segmentation fault after entering the number of routers in the system. The compiler is not producing any errors or warning. Any help would be greatly appreciated. Code below

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

typedef struct{
    int targetRouter[1024];
    int nextRouter[1024];
    int cost[1024];
}routers;

routers *routerSet[1024];


int main(){
    int numberOfRouters;
    char buffer[1024];
    
    printf("How many routers are in this network?\n");
    fgets(buffer, 1024, stdin);
    numberOfRouters = atoi(buffer);
    
    for(int i = 0; i < numberOfRouters; i++){
        for(int k=0; k<numberOfRouters; k++){
            if(i == k){
                routerSet[i]->targetRouter[k] = i;
                routerSet[i]->nextRouter[k] = i;
                routerSet[i]->cost[k] = 0;
            }
            else{ //different router
                printf("Is router number %d directly connected to router number %d (y/n)?", i, k);
                scanf("%s\n", buffer);
                
                if(strncmp(buffer, "y", 1) == 0){
                    bzero(buffer, sizeof(buffer));
                    routerSet[i]->targetRouter[k] = k;
                    routerSet[i]->nextRouter[k] = k;
                    printf("What is the delay between router number %d and router number %d?", i, k);
                    fgets(buffer, 1024, stdin);
                    routerSet[i]->cost[k] = atoi(buffer);
                }
                else if(strncmp(buffer, "n", 1) == 0){
                    routerSet[i]->targetRouter[k] = k;
                    routerSet[i]->nextRouter[k]=-1;
                    routerSet[i]->cost[k] = -1;
                }
                else{
                    printf("Invalid input. Ending program.");
                    exit(0);
                }
                bzero(buffer, sizeof(buffer));
            }
        }
    }
    
    for(int i = 0; i < numberOfRouters; i++){
        printf("Router table for router number %d", i);
        for(int k=0; k < numberOfRouters; k++){
            printf("%d | %d | %d\n", routerSet[i]->targetRouter[k], routerSet[i]->nextRouter[k], routerSet[i]->cost[k]);
        }
    }
}

Please note that this program is not finished. I just can't continue work on the rest of the program until this error is fixed.


Solution

  • You declared an array of null pointers

    routers *routerSet[1024];
    

    Note: the pointers in the array are initialized as null pointers because the array has static storage duration.

    So using these null pointers to access memory like in these statements

    routerSet[i]->targetRouter[k] = i;
    routerSet[i]->nextRouter[k] = i;
    routerSet[i]->cost[k] = 0;
    

    invokes undefined behavior.

    You need either declare an array of objects of the structure type instead of pointers

    routers routerSet[1024];
    

    or for each pointer in the array allocate dynamically an object of the structure type.