2

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your comment. I switched the declaration and then accessed the struct in the form routerSet[i].targetRouter[k] and now it is working. On to the rest of the project!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.