Enviado em 09/07/2019 - 10:01h
É verdade que é considerado uma má prática de programação C alocar memória em uma função e desalocar em outra função?//trecho de código struct art_properties{ int art; int quote; int art_color; };
//trecho de código static void handler(int num){ clear(); endwin(); init_scr(); } int resize(int key){ struct sigaction new_action; struct sigaction old_action; new_action.sa_flags=0; new_action.sa_handler=handler; sigemptyset(&new_action.sa_mask); sigaction(SIGWINCH, &new_action, &old_action); return getmaxy(stdscr); } static size_t count_rows(const char *file_path){ size_t rows=0; FILE *file=fopen(file_path, "r"); if(file!=NULL){ while(!feof(file)){ if(fgetc(file)=='\n'){ rows++; } } fclose(file); } return rows; } struct art_properties *print_art(int y, struct art_properties *properties){ struct art_properties aux; FILE *art_file=NULL, *quote_file=NULL; size_t i=0; char row_content[1025], str_quote[1025]; const char *arts[]={"obj/arts/art_001.txt", "obj/arts/art_002.txt", "obj/arts/art_003.txt", "obj/arts/art_004.txt", "obj/arts/art_005.txt", "obj/arts/art_006.txt", "obj/arts/art_007.txt", "obj/arts/art_008.txt", "obj/arts/art_009.txt", "obj/arts/art_010.txt"}; if(properties==NULL){ srand(time(NULL)); aux.art=rand()%9; aux.art_color=rand()%7; aux.quote=1+(rand()%count_rows("obj/quotes.txt")); properties=malloc(sizeof(struct art_properties)); memcpy(properties, &aux, sizeof(struct art_properties)); }else{ memcpy(&aux, properties, sizeof(struct art_properties)); } attron(COLOR_PAIR(aux.art_color) | A_BOLD); art_file=fopen(arts[aux.art], "r"); if(art_file!=NULL){ for(i=0; i<count_rows(arts[aux.art]); i++){ fgets(row_content, 1025, art_file); mvprintw(y+i, 1, "%s", row_content); } fclose(art_file); } quote_file=fopen("obj/quotes.txt", "r"); if(quote_file!=NULL){ if(aux.quote!=1){ for(int i=0; i!=aux.quote; i++){ fgets(str_quote, 1025, quote_file); } mvprintw(y+2+i, 1, "[GANADO QUOTE] - %s", str_quote); } fclose(quote_file); } attroff(COLOR_PAIR(aux.art_color) | A_BOLD); return properties; } struct art_properties *free_art(struct art_properties *ptr){ free(ptr); return NULL; }
//trecho de código case '0': noecho(); do{ properties=print_art(1, properties); attron(COLOR_PAIR(YELLOW) | A_BOLD); mvprintw(y-1, 1, "[?] - Are you sure you want to quit Saddler [Y/n]?"); attroff(COLOR_PAIR(YELLOW) | A_BOLD); key=getch(); y=resize(key); clear(); switch(key){ case 'y': case 'Y': key='Y'; break; case 'n': case 'N': key='N'; break; default: break; } }while(key!='Y' && key!='N'); properties=free_art(properties); echo(); break;
Enviado em 10/07/2019 - 19:50h
OBS: Essa pergunta também foi postada em Portugal a ProgramarEnviado em 10/07/2019 - 22:36h
EDIT: Códico para testes:#include <time.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <ncurses.h> struct art_properties{ int art; int quote; int art_color; }; static void init_scr(void){ if(initscr()==NULL){ fprintf(stderr, "[X] - initscr() == NULL!\n"); exit(EXIT_FAILURE); } if(has_colors()!=TRUE){ endwin(); fprintf(stderr, "[X] - Your terminal does not suport colors!\n"); exit(EXIT_FAILURE); }else{ int bkg_color; signal(SIGINT, SIG_IGN); signal(SIGTSTP, SIG_IGN); cbreak(); noecho(); keypad(stdscr, TRUE); start_color(); if(use_default_colors()==OK){ bkg_color=-1; }else{ bkg_color=COLOR_BLACK; } init_pair(RED, COLOR_RED, bkg_color); init_pair(BLUE, COLOR_BLUE, bkg_color); init_pair(CYAN, COLOR_CYAN, bkg_color); init_pair(GREEN, COLOR_GREEN, bkg_color); init_pair(WHITE, COLOR_WHITE, bkg_color); init_pair(YELLOW, COLOR_YELLOW, bkg_color); init_pair(MAGENTA, COLOR_MAGENTA, bkg_color); refresh(); } } static void handler(int num){ clear(); endwin(); init_scr(); } static int resize(void){ struct sigaction new_action; struct sigaction old_action; new_action.sa_flags=0; new_action.sa_handler=handler; sigemptyset(&new_action.sa_mask); sigaction(SIGWINCH, &new_action, &old_action); return getmaxy(stdscr); } static size_t count_rows(const char *file_path){ ssize_t rows=0; FILE *file=fopen(file_path, "r"); if(file!=NULL){ while(!feof(file)){ if(fgetc(file)=='\n'){ rows++; } } fclose(file); }else{ rows=1; } return rows; } static struct art_properties *print_art(int y, struct art_properties *properties){ struct art_properties aux; FILE *art_file=NULL, *quote_file=NULL; size_t i=0; char row_content[1025], str_quote[1025]; const char *arts[]={"arts/art_001.txt", "arts/art_002.txt", "arts/art_003.txt", "arts/art_004.txt", "arts/art_005.txt", "arts/art_006.txt", "arts/art_007.txt", "arts/art_008.txt", "arts/art_009.txt", "arts/art_010.txt"}; if(properties==NULL){ srand(time(NULL)); aux.art=rand()%9; aux.art_color=1+(rand()%7); aux.quote=1+(rand()%count_rows("quotes.txt")); properties=malloc(sizeof(struct art_properties)); memcpy(properties, &aux, sizeof(struct art_properties)); }else{ memcpy(&aux, properties, sizeof(struct art_properties)); } attron(COLOR_PAIR(aux.art_color) | A_BOLD); art_file=fopen(arts[aux.art], "r"); if(art_file!=NULL){ for(i=0; i<count_rows(arts[aux.art]); i++){ fgets(row_content, 1025, art_file); mvprintw(y+i, 1, "%s", row_content); } fclose(art_file); } quote_file=fopen("quotes.txt", "r"); if(quote_file!=NULL){ if(aux.quote!=1){ for(int i=0; i!=aux.quote; i++){ fgets(str_quote, 1025, quote_file); } mvprintw(y+2+i, 1, "[GANADO QUOTE] - %s", str_quote); } fclose(quote_file); }else{ mvprintw(y+2+i, 1, "[GANADO QUOTE] - \"Te voy a matar!\""); } attroff(COLOR_PAIR(aux.art_color) | A_BOLD); return properties; } static struct art_properties *free_art(struct art_properties *ptr){ free(ptr); return NULL; } int main(void){ int key, y; struct art_properties *properties=NULL; init_scr(); y=getmaxy(stdscr); do{ properties=print_art(1, properties); attron(COLOR_PAIR(YELLOW) | A_BOLD); mvprintw(y-1, 1, "[i] - Press Enter key to quit"); attroff(COLOR_PAIR(YELLOW) | A_BOLD); key=getch(); y=resize(); }while(key!=10); properties=free_art(properties); endwin(); return EXIT_SUCCESS; }
Enviado em 11/07/2019 - 10:38h
//trecho de código struct art_properties{ int art; int quote; int art_color; };
//trecho de código static void handler(int num){ clear(); endwin(); init_scr(); } int resize(int key){
struct sigaction new_action; struct sigaction old_action; new_action.sa_flags=0; new_action.sa_handler=handler; sigemptyset(&new_action.sa_mask); sigaction(SIGWINCH, &new_action, &old_action); return getmaxy(stdscr); } static size_t count_rows(const char *file_path){ size_t rows=0; FILE *file=fopen(file_path, "r"); if(file!=NULL){ while(!feof(file)){ if(fgetc(file)=='\n'){ rows++; } } fclose(file); } return rows; } struct art_properties *print_art(int y, struct art_properties *properties){ struct art_properties aux; FILE *art_file=NULL, *quote_file=NULL; size_t i=0; char row_content[1025], str_quote[1025]; const char *arts[]={"obj/arts/art_001.txt", "obj/arts/art_002.txt", "obj/arts/art_003.txt", "obj/arts/art_004.txt", "obj/arts/art_005.txt", "obj/arts/art_006.txt", "obj/arts/art_007.txt", "obj/arts/art_008.txt", "obj/arts/art_009.txt", "obj/arts/art_010.txt"}; if(properties==NULL){ srand(time(NULL));
aux.art=rand()%9; aux.art_color=rand()%7;
int color=(int)(((double)rand()/(1.0+(double)RAND_MAX))*8.0);
aux.quote=1+(rand()%count_rows("obj/quotes.txt"));
void func(int arg1, char *arg2){ static int *p_quant_linhas=NULL; // Variável estática, inicializada apenas uma vez e com valor preservado ao longo de múltiplas chamadas a ‘func’. if(!p_quant_linhas){ p_quant_linhas=malloc(sizeof *p_quant_linhas); if(!p_quant_linhas){ perror("Erro de alocação de memória"); exit(1); } *p_quant_linhas=conta_linhas("arquivo"); } // A partir deste ponto, ‘*p_quant_linhas’ terá a quantidade de linhas do arquivo. // ... int aux=1+(int)(((double)rand()/(1.0+(double)RAND_MAX))*(double)(*p_quant_linhas)); // ... }
properties=malloc(sizeof(struct art_properties)); memcpy(properties, &aux, sizeof(struct art_properties));
properties=malloc(sizeof *properties); memcpy(properties, &aux, sizeof *properties);
}else{ memcpy(&aux, properties, sizeof(struct art_properties));
} attron(COLOR_PAIR(aux.art_color) | A_BOLD); art_file=fopen(arts[aux.art], "r"); if(art_file!=NULL){ for(i=0; i<count_rows(arts[aux.art]); i++){ fgets(row_content, 1025, art_file); mvprintw(y+i, 1, "%s", row_content); } fclose(art_file); } quote_file=fopen("obj/quotes.txt", "r"); if(quote_file!=NULL){ if(aux.quote!=1){ for(int i=0; i!=aux.quote; i++){ fgets(str_quote, 1025, quote_file); } mvprintw(y+2+i, 1, "[GANADO QUOTE] - %s", str_quote); } fclose(quote_file); } attroff(COLOR_PAIR(aux.art_color) | A_BOLD); return properties; } struct art_properties *free_art(struct art_properties *ptr){ free(ptr); return NULL;
void free_art(struct art_properties **ptr){ free(*ptr); *ptr=NULL; }
}
//trecho de código case '0': noecho(); do{ properties=print_art(1, properties); attron(COLOR_PAIR(YELLOW) | A_BOLD); mvprintw(y-1, 1, "[?] - Are you sure you want to quit Saddler [Y/n]?"); attroff(COLOR_PAIR(YELLOW) | A_BOLD); key=getch();
if(key>=0 && key<255){ unsigned char k=toupper(key); if(k=='Y' || k=='N') key=k; }
y=resize(key); clear(); switch(key){ case 'y': case 'Y': key='Y'; break; case 'n': case 'N': key='N'; break; default: break; } }while(key!='Y' && key!='N'); properties=free_art(properties); echo(); break;
Enviado em 13/07/2019 - 11:06h
//trecho de código struct art_properties{ int art; int quote; int art_color; };
//trecho de código static void handler(int num){ clear(); endwin(); init_scr(); } int resize(int key){
struct sigaction new_action; struct sigaction old_action; new_action.sa_flags=0; new_action.sa_handler=handler; sigemptyset(&new_action.sa_mask); sigaction(SIGWINCH, &new_action, &old_action); return getmaxy(stdscr); } static size_t count_rows(const char *file_path){ size_t rows=0; FILE *file=fopen(file_path, "r"); if(file!=NULL){ while(!feof(file)){ if(fgetc(file)=='\n'){ rows++; } } fclose(file); } return rows; } struct art_properties *print_art(int y, struct art_properties *properties){ struct art_properties aux; FILE *art_file=NULL, *quote_file=NULL; size_t i=0; char row_content[1025], str_quote[1025]; const char *arts[]={"obj/arts/art_001.txt", "obj/arts/art_002.txt", "obj/arts/art_003.txt", "obj/arts/art_004.txt", "obj/arts/art_005.txt", "obj/arts/art_006.txt", "obj/arts/art_007.txt", "obj/arts/art_008.txt", "obj/arts/art_009.txt", "obj/arts/art_010.txt"}; if(properties==NULL){ srand(time(NULL));
#include <time.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <ncurses.h> struct art_properties{ int art; int quote; int art_color; }; static void init_scr(void){ if(initscr()==NULL){ fprintf(stderr, "[X] - initscr() == NULL!\n"); exit(EXIT_FAILURE); } if(has_colors()!=TRUE){ endwin(); fprintf(stderr, "[X] - Your terminal does not suport colors!\n"); exit(EXIT_FAILURE); }else{ int bkg_color; signal(SIGINT, SIG_IGN); signal(SIGTSTP, SIG_IGN); cbreak(); noecho(); keypad(stdscr, TRUE); start_color(); if(use_default_colors()==OK){ bkg_color=-1; }else{ bkg_color=COLOR_BLACK; } init_pair(RED, COLOR_RED, bkg_color); init_pair(BLUE, COLOR_BLUE, bkg_color); init_pair(CYAN, COLOR_CYAN, bkg_color); init_pair(GREEN, COLOR_GREEN, bkg_color); init_pair(WHITE, COLOR_WHITE, bkg_color); init_pair(YELLOW, COLOR_YELLOW, bkg_color); init_pair(MAGENTA, COLOR_MAGENTA, bkg_color); refresh(); } } static void handler(int num){ clear(); endwin(); init_scr(); } static int resize(void){ struct sigaction new_action; struct sigaction old_action; new_action.sa_flags=0; new_action.sa_handler=handler; sigemptyset(&new_action.sa_mask); sigaction(SIGWINCH, &new_action, &old_action); return getmaxy(stdscr); } static size_t count_rows(const char *file_path){ ssize_t rows=0; FILE *file=fopen(file_path, "r"); if(file!=NULL){ while(!feof(file)){ if(fgetc(file)=='\n'){ rows++; } } fclose(file); }else{ rows=1; } return rows; } static struct art_properties *print_art(int y, struct art_properties *properties){ struct art_properties aux; FILE *art_file=NULL, *quote_file=NULL; size_t i=0; char row_content[1025], str_quote[1025]; const char *arts[]={"arts/art_001.txt", "arts/art_002.txt", "arts/art_003.txt", "arts/art_004.txt", "arts/art_005.txt", "arts/art_006.txt", "arts/art_007.txt", "arts/art_008.txt", "arts/art_009.txt", "arts/art_010.txt"}; if(properties==NULL){ srand(time(NULL)); aux.art=rand()%9; aux.art_color=1+(rand()%7); aux.quote=1+(rand()%count_rows("quotes.txt")); properties=malloc(sizeof(struct art_properties)); memcpy(properties, &aux, sizeof(struct art_properties)); }else{ memcpy(&aux, properties, sizeof(struct art_properties)); } attron(COLOR_PAIR(aux.art_color) | A_BOLD); art_file=fopen(arts[aux.art], "r"); if(art_file!=NULL){ for(i=0; i<count_rows(arts[aux.art]); i++){ fgets(row_content, 1025, art_file); mvprintw(y+i, 1, "%s", row_content); } fclose(art_file); } quote_file=fopen("quotes.txt", "r"); if(quote_file!=NULL){ for(int i=0; i!=aux.quote; i++){ fgets(str_quote, 1025, quote_file); } mvprintw(y+2+i, 1, "[GANADO QUOTE] - %s", str_quote); fclose(quote_file); }else{ mvprintw(y+2+i, 1, "[GANADO QUOTE] - \"Te voy a matar!\""); } attroff(COLOR_PAIR(aux.art_color) | A_BOLD); return properties; } static struct art_properties *free_art(struct art_properties *ptr){ free(ptr); return NULL; } int main(void){ int key, y; struct art_properties *properties=NULL; init_scr(); y=getmaxy(stdscr); do{ properties=print_art(1, properties); attron(COLOR_PAIR(YELLOW) | A_BOLD); mvprintw(y-1, 1, " - Press Enter key to quit"); attroff(COLOR_PAIR(YELLOW) | A_BOLD); key=getch(); y=resize(); }while(key!=10); properties=free_art(properties); endwin(); return EXIT_SUCCESS; }
if(properties==NULL){ srand(time(NULL)); aux.art=rand()%9; aux.art_color=rand()%7; aux.quote=1+(rand()%count_rows("quotes.txt")); properties=malloc(sizeof(struct art_properties)); memcpy(properties, &aux, sizeof(struct art_properties)); }else{ memcpy(&aux, properties, sizeof(struct art_properties)); }
int main(void){ int key, y; struct art_properties *properties=NULL; init_scr(); y=getmaxy(stdscr); do{ properties=print_art(1, properties); attron(COLOR_PAIR(YELLOW) | A_BOLD); mvprintw(y-1, 1, " - Press Enter key to quit"); attroff(COLOR_PAIR(YELLOW) | A_BOLD); key=getch(); y=resize(); }while(key!=10); properties=free_art(properties); endwin(); return EXIT_SUCCESS; }
//trecho de código case '0': noecho(); do{ properties=print_art(1, properties); attron(COLOR_PAIR(YELLOW) | A_BOLD); mvprintw(y-1, 1, "[?] - Are you sure you want to quit Saddler [Y/n]?"); attroff(COLOR_PAIR(YELLOW) | A_BOLD); key=getch();
int resize(int key){
if(key>=0 && key<255){ unsigned char k=toupper(key); if(k=='Y' || k=='N') key=k; }
Enviado em 15/07/2019 - 15:52h
int resize(int key){
Criar entrada (menuentry) ISO no Grub
Como gerar qualquer emoji ou símbolo unicode a partir do seu teclado
Instalar o VIM 9.1 no Debian 12
Como saber o range de um IP público?
Muitas dificuldades ao instalar distro Linux em Notebook Sony Vaio PCG-6131L (VPCEA24FM)
Slackpkg+ (Slackpkg Plus) está de volta!
Como dividir duas janelas igualmente e lado-a-lado na sua tela
Problemas com adaptadores wifi no Zorin (1)
Ocomon 6.0.1 - Problemas ao editar configurações estendidas (7)