c - Value disappearing outside of while loop, even though saving to a malloc variable -
i've created struct called utype
contains different variables. if want t
call utype->t
.
now, i'm using fgets
retrieve data input file.
the code is:
#include "read_file.h" int read_file(int argc, char* argv[], utype* u_coarse){ int i; int num_length = 1024; file *input_file; char buffer[num_length]; char *end_of_file; char *lines = null; char *lines_temp; int num_time_segments=0; double testest=0; (i=0; i<num_length; i++){ buffer[i]=' '; } input_file = fopen(argv[1], "r"); end_of_file = fgets(buffer, num_length, input_file); while (end_of_file != null){ if (end_of_file[0]!= '\n' && end_of_file[0]!='%'){ lines = strtok(end_of_file, "="); if (lines[strlen(lines)-1] == ' '){ lines[strlen(lines)-1] = '\0'; } lines_temp = strtok(null, "="); if (lines_temp[0] == ' '){ (i=1; i<strlen(lines_temp); i++){ lines_temp[i-1] = lines_temp[i]; } lines_temp[strlen(lines_temp)-2] = '\0'; }else{ lines_temp[strlen(lines_temp)-1] = '\0'; } if (strcmp(lines, "t") == 0){ u_coarse->ultimatet = atof(lines_temp); } // printf("%g\n", testest); } } // printf("%g\n", testest); // u_coarse->burn_time = testest; fclose(input_file); return num_time_segments;
after call read_file
function, try print
u_coarse->ultimatet
but says
conditional jump or move depends on uninitialised value(s)
when use valgrind. i've been trying figure out why it's giving me memory error while now. appreciated.
edit may 31, 2015:
thank input. i'm attaching adjusted code did improvements. still same error before. valgrind says:
==6485== memcheck, memory error detector ==6485== copyright (c) 2002-2012, , gnu gpl'd, julian seward et al. ==6485== using valgrind-3.8.1 , libvex; rerun -h copyright info ==6485== command: ../bin/main.x ../ ../example_input.inp ==6485== ==6485== conditional jump or move depends on uninitialised value(s) ==6485== @ 0x3bfe249cf0: __printf_fp (in /lib64/libc-2.12.so) ==6485== 0x3bfe24589f: vfprintf (in /lib64/libc-2.12.so) ==6485== 0x3bfe24f189: printf (in /lib64/libc-2.12.so) ==6485== 0x406cf1: main (main.c:23)
where:
main.c:23
is i'm printing out :
u_coarse->ultimatet
edited code:
#include "read_file.h" int read_file(int argc, char* argv[], utype* u_coarse){ int i; int num_length = 1024; file *input_file; char buffer[num_length]; char *current_line; char *lines = null; char *lines_temp; int num_time_segments=0; size_t length; size_t length_temp; memset(buffer, ' ', sizeof(buffer) -1); buffer[sizeof(buffer) - 1] = '\0'; input_file = fopen(argv[1], "r"); if (input_file == null){ perror("error"); exit( exit_failure); }else{ while ( current_line = fgets(buffer, sizeof(buffer), input_file)){ if (current_line[0]!= '\n' && current_line[0]!='%'){ lines = strtok(current_line, "="); if (lines == null){ perror("error"); exit( exit_failure ); } length = strlen(lines); if (lines[length-1] == ' '){ lines[length-1] = '\0'; } lines_temp = strtok(null, "="); if (lines_temp == null){ perror("error"); exit( exit_failure ); } length_temp = strlen(lines_temp); if (lines_temp[0] == ' '){ memmove(lines_temp, lines_temp +1, length_temp); }else{ lines_temp[length_temp-1] = '\0'; } if (strcmp(lines, "t") == 0){ u_coarse->ultimatet = atof(lines_temp); } } } } return num_time_segments; }
my main.c file is:
int main(int argc, char *argv[]) { utype *u_coarse = (utype*)malloc(sizeof(utype)); int num_time_segments = read_file(argc, argv, u_coarse); printf("%g\n", u_coarse->ultimatet);
my struct is:
typedef struct { double ultimatet; }utype;
sorry, i've been trying not show since research.
your code has several problems
you never check if
strtok()
failed or not, can cause valgrind report reporting, or else since causes undefined behavior.you use
strlen()
wrong,strlen()
loops through string means in each iteration looping same number of times. must store value , use stored value, it's not more efficient, makes code prettier.the loop never ends, because don't reassign
end_of_file
way worse name variable, should thiswhile (current_line = fgets(buffer, sizeof(buffer), input_file)) ...
you filling
buffer
array manually written loop when can usememset()
memset(buffer, ' ', sizeof(buffer) - 1); buffer[sizeof(buffer) - 1] = '\0';
would better job, syntactically , more efficient, ,
nul
terminatebuffer
, didn't do.you never check if
fopen()
succeded, lead undefined behavior too, must check every function call worked expected, of them return special values or set special variables indicate when problem happens, failing check errors makes code unstable , if boss fire you. don't take wrong, saying because if follow advice write more robust code, , have way less problems.this
for (i = 1 ; < strlen(lines_temp) ; i++) { lines_temp[i - 1] = lines_temp[i]; } lines_temp[strlen(lines_temp) - 2] = '\0';
is bad several reasons
you should not loop yourself, use
memmove()
instead.size_t length = strlen(lines_temp); memmove(lines_temp, lines_temp + 1, length);
if write loop yourself, efficient way be
for (i = 1 ; lines_temp[i] != 0 ; i++) { lines_temp[i - 1] = lines_temp[i]; } lines_temp[i - 1] = '\0';
Comments
Post a Comment