c array not assigning correctly -
i'm assigning array pointer, after assignment, print values of both different. also, pointer values change execution execution (and since values come static file, looks pointer not assigned , it's printing address directions).
here's 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 error 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 %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 %d\n", j, num ); line += length; } machines[i].moving_costs = moving_costs; } printf("printing pointer values:\n"); for(i=0; < 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 executing code is:
./mrp -t 1000 -p test_instance -i mrp_original_sol -o mrp_sol -s 10
you storing reference array machines
, local function read_machines()
. once function exits, array ceases exist , stored pointer no longer points anywhere valid. dereferencing pointer causes undefined behavior.
Comments
Post a Comment