Skip to content
Extraits de code Groupes Projets
Valider ffcca1e2 rédigé par Hugo Clercq's avatar Hugo Clercq
Parcourir les fichiers

cppcheck fixes

parent b70d7d27
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
...@@ -77,6 +79,7 @@ queue_t* new_queue(){ ...@@ -77,6 +79,7 @@ queue_t* new_queue(){
if(result->buffer==NULL){// Test malloc failure if(result->buffer==NULL){// Test malloc failure
printf("Malloc failed in newArray() for result->first"); printf("Malloc failed in newArray() for result->first");
free(result);
return NULL; // Malloc fail return NULL; // Malloc fail
} }
...@@ -126,6 +129,7 @@ div_queue_t* new_div_queue(){ ...@@ -126,6 +129,7 @@ div_queue_t* new_div_queue(){
if(result->buffer==NULL){// Test malloc failure if(result->buffer==NULL){// Test malloc failure
printf("Malloc failed in newArray() for result->first"); printf("Malloc failed in newArray() for result->first");
free(result);
return NULL; // Malloc fail return NULL; // Malloc fail
} }
...@@ -158,17 +162,13 @@ void div_free_queue(div_queue_t *queue){//Free a queue ...@@ -158,17 +162,13 @@ void div_free_queue(div_queue_t *queue){//Free a queue
//Print help for option -h or --help //Print help for option -h or --help
void printHelp(){ void printHelp(){
printf("Usage: ./prime_divs [OPTION...]\n" printf("Usage: ./prime_divs [OPTION...] input_file output_file\n"
"\t-v, --verbose\tprint debug\n" "\t-v, --verbose\tprint debug\n"
"\t-q\t\tdo not print debug\n" "\t-N\t\tspecify number of threads\n");
"\t-r\t\tspecify input file\n"
"\t-r\t\tspecify output file\n"
"\t-N\t\tspecify number of threads");
} }
// Return number modulo i // Return number modulo i
int is_div(long long number,long long i){ int is_div(long long number,long long i){
//if (number == i) return false;
return (number % i) == 0; return (number % i) == 0;
} }
...@@ -244,14 +244,13 @@ void* produce(){ ...@@ -244,14 +244,13 @@ void* produce(){
char* line = NULL; char* line = NULL;
size_t len = 0; size_t len = 0;
ssize_t read;
char* eptr; char* eptr;
while ((read = getline(&line, &len, fdIn)) != -1){ while (getline(&line, &len, fdIn) != -1){
long long number = strtoll(line, &eptr,10); long long number = strtoll(line, &eptr,10);
if(number == 0){ if(number == 0){
printf("Conversion error string to ll\n"); printf("Conversion error string to ll\n");
} }
printf("%lld \n", number); //printf("%lld \n", number);
if(number < 2){ if(number < 2){
printf("Error number < 2 in file\n"); printf("Error number < 2 in file\n");
} }
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
int main(int argc, char** argv){ int main(int argc, char** argv){
Args.nThreads = 8; //default number of threads Args.nThreads = 8; //default number of threads
const char* strOpts = "hqvr:w:N:"; const char* strOpts = "hvN:";
const struct option longOpts[] = { const struct option longOpts[] = {
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h'},
{"verbose", no_argument, NULL, 'v'} {"verbose", no_argument, NULL, 'v'}
...@@ -20,23 +20,16 @@ int main(int argc, char** argv){ ...@@ -20,23 +20,16 @@ int main(int argc, char** argv){
case 'h': case 'h':
printHelp(); printHelp();
return EXIT_SUCCESS; return EXIT_SUCCESS;
case 'q':
Args.verbose = 0;
break;
case 'v': case 'v':
Args.verbose = 1; Args.verbose = 1;
break; break;
case 'r':
Args.input_file = optarg;
break;
case 'w':
Args.output_file = optarg;
break;
case 'N': case 'N':
Args.nThreads = atoi(optarg); Args.nThreads = atoi(optarg);
break; break;
} }
} }
Args.input_file = argv[argc-2];
Args.output_file = argv[argc-1];
FILE* LineCounter = fopen(Args.input_file,"r"); FILE* LineCounter = fopen(Args.input_file,"r");
if(LineCounter == NULL){ if(LineCounter == NULL){
...@@ -46,7 +39,6 @@ int main(int argc, char** argv){ ...@@ -46,7 +39,6 @@ int main(int argc, char** argv){
counter = 0; counter = 0;
char* line = NULL; char* line = NULL;
size_t len = 0; size_t len = 0;
char* eptr;
while (getline(&line, &len, LineCounter)!=-1){ while (getline(&line, &len, LineCounter)!=-1){
counter++; counter++;
} }
...@@ -61,12 +53,12 @@ int main(int argc, char** argv){ ...@@ -61,12 +53,12 @@ int main(int argc, char** argv){
done2 = Args.nThreads; done2 = Args.nThreads;
buffer = new_queue(1); buffer = new_queue();
pthread_mutex_init(&mutex, NULL); pthread_mutex_init(&mutex, NULL);
sem_init (&empty,0,(2* Args.nThreads) +1); sem_init (&empty,0,(2* Args.nThreads) +1);
sem_init(&full,0,0); sem_init(&full,0,0);
buffer2 = new_div_queue(0); buffer2 = new_div_queue();
pthread_mutex_init(&mutex2,NULL); pthread_mutex_init(&mutex2,NULL);
sem_init (&empty2,0,(2* Args.nThreads) +1); sem_init (&empty2,0,(2* Args.nThreads) +1);
sem_init(&full2,0,0); sem_init(&full2,0,0);
...@@ -76,6 +68,10 @@ int main(int argc, char** argv){ ...@@ -76,6 +68,10 @@ int main(int argc, char** argv){
pthread_t writer; pthread_t writer;
//pthread_t threads[Args.nThreads]; //pthread_t threads[Args.nThreads];
threads = malloc(sizeof(pthread_t) * Args.nThreads); threads = malloc(sizeof(pthread_t) * Args.nThreads);
if(threads == NULL){
printf("Malloc error for threads in main\n");
return EXIT_FAILURE;
}
finished = 0; finished = 0;
if (pthread_create(&reader, NULL, &produce, NULL) != 0){ if (pthread_create(&reader, NULL, &produce, NULL) != 0){
printf("Error while creating the reader thread\n"); printf("Error while creating the reader thread\n");
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter