1
EXPLANATION
I am studying about dynamic memory allocation and have made a simple program to solve the following situation:
The client wants to register a certain number of employees and, want to do the consultation soon after. The program makes the registration of employees using a Code (individual) and Name provided by the user (client).
main. c
#include <stdio.h>
#include <stdlib.h>
#include "cadastro.h"
#ifdef _WIN32 /*define o comando de limpar a tela com base no SO.*/
#define CLEAR "cls"
#else
#define CLEAR "clear"
#endif
int main() {
int qtd_prof, tam_nome, cod;
int stop=0;
char continue_signal;
Profissao *profissao;
system(CLEAR);
printf("Qual o número de profissoes a serem cadastradas?\n");
printf("Insira o valor:\n");
scanf("%d", &qtd_prof);
setbuf(stdin, NULL);
profissao = (Profissao *) malloc(qtd_prof * sizeof(Profissao));
for(int i=0; i<qtd_prof; i++) {
system(CLEAR);
printf("Qual o tamanho do %dº nome?\n", i+1);
printf("Insira o valor:\n");
scanf("%d", &tam_nome);
setbuf(stdin, NULL);
profissao[i].codigo = (int *) malloc(sizeof(int));
profissao[i].nome = (char *) malloc(tam_nome*sizeof(char));
printf("======================================\n");
printf("Qual o codigo do %dº cadastro:\n", i+1);
scanf("%d", &(*profissao[i].codigo));
setbuf(stdin, NULL);
printf("======================================\n");
printf("Qual o nome do %dº cadastro: (MAX: %d)\n", i+1, tam_nome);
scanf("%[^\n]s", profissao[i].nome);
setbuf(stdin, NULL);
/* if(feof(aux)) { //A ideia dessa parte era averiguar a qtd de bytes inseridos.
flag=1;
printf("ERRO! Falha ao tentar gravar o nome.\n");
getchar();
break;
} */
}
printf("======================================\n");
printf("Nomes gravados com sucesso!\n");
printf("Pressione ENTER para continuar!\n");
getchar();
while (!stop) {
system(CLEAR);
printf("Insira o codigo para consulta:\n");
scanf("%d", &cod);
setbuf(stdin, NULL);
for (int i = 0; i < qtd_prof; ++i) {
if(cod == *profissao[i].codigo) {
printf("-----------------------------\n");
printf("Nome: %s\n", profissao[i].nome);
}
}
do {
printf("======================================\n");
printf("Deseja fazer uma nova consulta? (S/N)\n");
scanf("%c", &continue_signal);
setbuf(stdin, NULL);
if (continue_signal == 'S') {
stop = 0;
} else if (continue_signal == 'N') {
stop = 1;
printf("O programa será finalizado!\n");
}
}while (continue_signal != 'S' && continue_signal != 'N');
}
for(int i=0; i<qtd_prof; i++) {
free(profissao[i].codigo);
free(profissao[i].nome);
free(profissao);
}
return 0;
}
register. h
#ifndef CADASTRO_H
#define CADASTRO_H
typedef struct{
int *codigo;
char *nome;
} Profissao;
#endif //CADASTRO_H
Initially, it only has the struct Professional, which has the structure of the register of each employee that will be implemented by the function main. c.
PROBLEM
The point is that the code compiles and performs perfectly using the gcc (linux). But when I try to compile from within the Clion (Jetbrains IDE) I come across the following problem:
The code doesn’t even show the first printf in the terminal.
Clion has detected GDB, Cmake and Valgrind correctly and all dependencies are installed.
I’m posting here, on Stackoverflow, in search of someone who has been through the same situation and can help me.
Put some breakpoint before launching the program in mode debug ?
– Isac
To test yes, but first I tried without any breakpoint and gave error even so. When I put the breakpoint, I put in the first malloc function because I thought the error could be in dynamic allocation. But even so, there are no printfs on the terminal and it says: "Process finished with Exit code 1".
– Eduardo Assis