c array not assigning correctly

时间:2015-06-26 09:28:48

标签: c arrays pointers

I'm assigning an array to a pointer, but after the assignment, I print the values of both but they are different. Also, the pointer values change from execution to execution (and since the values come from an static file, that looks like the pointer is not assigned and it's printing address directions).

Here's the code:

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

struct machine {
    int neighborhood;
    int location;
    int* capacities;
    int* safety_capacities;
    int* moving_costs;
};
struct resource{
    int transient;
    int weight_load_cost;
};
struct variables{
    int resources_amount;
    int machines_amount;

    struct machine* machines;
    struct resource* resources;
};
struct variables var;

void read_resources(FILE *file){
    int resources_amount, i;
    char * line = NULL;
    size_t len = 0;
    ssize_t read;

    getline(&line, &len, file);
    //printf("resources: %s", line);
    resources_amount = atoi(line);

    struct resource resources[resources_amount];

    for(i = 0; i<resources_amount; i++){
        getline(&line, &len, file);
        getline(&line, &len, file);
        //printf("transient: %s\n", line);
        resources[i].transient = atoi(line);
        getline(&line, &len, file);
        //printf("load_cost: %s\n", line);
        resources[i].weight_load_cost = atoi(line);
    }
    var.resources = resources;
    var.resources_amount = resources_amount;
}
void read_machines(FILE *file){
    int machines_amount, i, j, num, length;
    char * line = NULL;
    size_t len = 0;
    ssize_t read;

    getline(&line, &len, file);
    //printf("machines: %d\n", atoi(line));
    machines_amount = atoi(line);

    struct machine machines[machines_amount];

    printf("Printing array values:\n");
    for(i=0; i<machines_amount; i++){
        //blank line
        getline(&line, &len, file);
        //getting neighborhood
        getline(&line, &len, file);
        machines[i].neighborhood = atoi(line);
        //getting location
        getline(&line, &len, file);
        machines[i].location = atoi(line);
        //getting capacities
        getline(&line, &len, file);
        int capacities[var.resources_amount];

        for(j=0; j < var.resources_amount; j++){
            sscanf( line, "%d%n", &num, &length);
            capacities[j]=num;
            printf("Machine %d, resource %d: %d\n", i, j, capacities[j]);
            line += length;
        }

        //HERE IS THE ERROR I GUESS!! <---
        machines[i].capacities = capacities;
        getline(&line, &len, file);
        int safety_capacities[var.resources_amount];
        for(j=0; j < var.resources_amount; j++){
            sscanf( line, "%d%n", &num, &length);
            safety_capacities[j]=num;
            //printf( "Number %d is %d\n", j, num );
            line += length;
        }
        machines[i].safety_capacities = safety_capacities;
        getline(&line, &len, file);
        int moving_costs[machines_amount];
        //printf("Moving Costs:\n");
        for(j=0; j < machines_amount; j++){
            //sscanf( line, "%d%n", &num, &length);
            moving_costs[j]=num;
            //printf( "Number %d is %d\n", j, num );
            line += length;
        }
        machines[i].moving_costs = moving_costs;
    }
    printf("Printing pointer values:\n");
    for(i=0; i < machines_amount; i++){
        for(j=0; j < var.resources_amount; j++){
            printf("Machine %d, resource %d: %d\n", i, j, machines[i].capacities[j]);
        }
    }

    var.machines = machines;
    var.machines_amount = machines_amount;
}
void read_file(char *filename){
    FILE *file;
    file = fopen(filename, "r");

    if ( file == 0 ){
        printf( "Could not open Settings File\n" );
    }
    else{
        read_resources(file);
        read_machines(file);
    }
}

int main( int argc, char *argv[] ){
    read_file(argv[4]);

    return 0;
}

The input file is:

2

1
10

0
100
4

0
0
30 400
16 80
0 1 4 5

0
0
10 250
8 160
1 0 3 4

1
1
15 100
12 80
4 3 0 2

1
2
10 100
8 80
5 4 2 0
2

2
0

1
1
0
3

0
12 10
1000

0
10 20
100

1
6 200
1
1
0 1 20
10
1
10
100

and the executing code is:

./mrp -t 1000 -p test_instance -i mrp_original_sol -o mrp_sol -s 10

1 个答案:

答案 0 :(得分:0)

You are storing a reference to the array machines, which is local to the function read_machines(). Once that function exits, the array ceases to exist and the stored pointer no longer points to anywhere valid. Dereferencing that pointer causes undefined behavior.