0

Hi I'm trying to read variables from a file into a int array so I can store them in a Struct array. The numbers get stored correctly into the curLinks array but when I try pass the curLinks array into curNodes.nodes, it doesn't work and when i try print it out (to test) it prints rubbish numbers. Any help would be great.

struct nodeInfo* getTopology(FILE *file){
    int totLinks=0;
    fscanf(file, "%d", &nodeCount);
    struct nodeInfo netTopo[nodeCount];

    // How many links does node have
    for (int id=0; id<nodeCount; id++){
        struct nodeInfo curNode;
        curNode.n=id;
        fscanf(file, "%d", &totLinks);
        int curLinks[totLinks];
        for(int i=0; i<totLinks; i++){
            int digit=0;
            fscanf(file, "%d", &digit);
            curLinks[i] = digit;
        }
        curNode.nodes = curLinks;

        netTopo[id] = curNode;
    }
    for (int id=0; id<nodeCount; id++){
        for (int j=0; j<3; j++){
            printf("%d ", netTopo[id].nodes[j]);
        }
    }

    return netTopo;
}

2 Answers 2

1

You define curLinks multiple time in the first for-loop

int curLinks[totLinks];

And after you fill that you try to set that in your nodeinfo however as soon as the next iteration in the for-loop is entered and curLinks is filled again, the memory of your previous curLinks is out of scope and the memory where you think your read in values should reside can be actually filled with anything - Undefined Behaviour

If you tell me the way you define your structs nodeInfo I might be able to show you how to do it properly.

e.g.: Assuming you define

struct nodeinfo {
    int *nodes;
};

Then

struct nodeInfo* getTopology(FILE *file)
{
    int id, digit=0, totLinks=0;
    fscanf(file, "%d", &nodeCount);
    struct nodeInfo netTopo[nodeCount];

    // How many links does node have
    for (id=0; id<nodeCount; id++){

        fscanf(file, "%d", &totLinks);

        netTopo[id].nodes = malloc(totLinks * sizeof(int));
        if (netTopo[id].nodes==NULL)
            printf("Error allocating memory\n");

        for(i=0; i<totLinks; i++) {
            fscanf(file, "%d", &digit);
            netTopo[id].nodes[i] = digit;
        }
    }

    // Free the allocate memory
    for (id=0; id<nodeCount; id++){
         free(netTopo[id].nodes);
    }

    return netTopo;
}
Sign up to request clarification or add additional context in comments.

1 Comment

The double for loop is where I'm trying to read what's in the Struct. The input file I'm using to test has max 3 int's hence the j<3 in the loop. it just prints a bunch of randon numbers 1347480480 32767 258941594 1347480480 32767 258941594 1347480480 32767 258941594 1347480480 32767 258941594
0

I think you should use pointers and "malloc" to allocate memory from the heap in this case.

1 Comment

Thanks i changed to the line: int curLinks = (int)calloc(totLinks,sizeof(int)); and it worked :D

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.