I'm using the getopt function to parse out some command line arguments in the following format; ./a.out -b 16 -s 64 -n 2 < trace_file.trace. Currently, -b, -s work fine, but when I add the -n argument I get a seg fault. Through using Valgrind I get the following error information
==10472== Invalid read of size 1
==10472== at 0x517A517: ____strtol_l_internal (strtol_l.c:298)
==10472== by 0x5176F5F: atoi (atoi.c:27)
==10472== by 0x4008EA: main (in /home/ian/a.out)
==10472== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==10472==
==10472==
==10472== Process terminating with default action of signal 11 (SIGSEGV)
==10472== Access not within mapped region at address 0x0
==10472== at 0x517A517: ____strtol_l_internal (strtol_l.c:298)
==10472== by 0x5176F5F: atoi (atoi.c:27)
==10472== by 0x4008EA: main (in /home/ian/a.out)
And here is my code snippet where I believe the error occurs. -b, -s, -n refer to block_size, set_sum, association_num respectively and parameter is of struct type cache which just simply holds the int values of block_size, set_sum, association_num.
while((c = getopt(argc, argv, ":s:b:n:")) != -1){
switch(c){
case 'b':
parameter.block_size = atoi(optarg);
b_size = parameter.block_size;
break;
case 's':
parameter.set_num = atoi(optarg);
num_sets = parameter.set_num;
break;
case 'n':
parameter.associativity = atoi(optarg);
association_num = parameter.associativity;
break;
}
}
Aucun commentaire:
Enregistrer un commentaire