Добрый день.
Столкнулся с проблемой.
Есть функция для сохранения if - структуры.
Код:
struct controlrecord
{
char* if_rec;
char** then_rec;
int num_then_rec;
char** else_rec;
int num_else_rec;
int check_status;
};
typedef struct controlrecord CtrlRec;
int record_rec(char** cmd, CtrlRec* rec)
{
#ifdef DEBUG
printf("Debug : record_rec() : begin\n");
#endif
int ret=0;
char* temp_cond;
temp_cond = malloc(0);
if (strcmp(cmd[0], "if") == 0)
{
#ifdef DEBUG
printf("Debug : record_rec() : if block\n");
#endif
ctrl_status = IF_REC;
c2toc1(temp_cond, cmd);
temp_cond+=3;
rec->if_rec = (char*)malloc(10*sizeof(char));
strcpy(rec->if_rec, temp_cond);
#ifdef DEBUG
printf("Debug : record_rec() : if block end\n");
#endif
}
else if(strcmp(cmd[0], "then") == 0)
{
#ifdef DEBUG
printf("Debug : record_rec() : then block\n");
#endif
ctrl_status = THEN_REC;
if((rec->then_rec = (char**)malloc(10*sizeof(char*))) == NULL)
{
printf("Error : alloc : then\n");
exit(1);
}
}
else if(strcmp(cmd[0], "else")==0)
{
#ifdef DEBUG
printf("Debug : record_rec() : else block\n");
#endif
ctrl_status = ELSE_REC;
rec->else_rec = malloc(10*sizeof(char*));
}
else if(strcmp(cmd[0], "fi") == 0)
{
#ifdef DEBUG
printf("Debug : record_rec() : fi block\n");
#endif
ctrl_status = NONE;
rec->check_status = NEED_CHECK;
showCtrlRec(rec);
}
else
{
if (ctrl_status == THEN_REC)
{
#ifdef DEBUG
printf("Debug : record_rec() : then_rec block\n");
#endif
c2toc1(temp_cond, cmd);
#ifdef DEBUG
printf("Debug : record_rec() : point 1 : strlen(temp_cond) = %d\n", strlen(temp_cond));
#endif
if((rec->then_rec[rec->num_then_rec] = (char*)malloc(strlen(temp_cond)*sizeof(char))) == NULL)
{
printf("Error: alloc : then_rec\n");
exit(1);
}
#ifdef DEBUG
printf("Debug : record_rec() : point 2\n");
#endif
strcpy(rec->then_rec[rec->num_then_rec], temp_cond);
#ifdef DEBUG
printf("Debug : record_rec() : point 3\n");
#endif
rec->num_then_rec++;
rec->then_rec++;
free(temp_cond);
#ifdef DEBUG
printf("Debug : record_rec() : point 4\n");
#endif
}
if (ctrl_status == ELSE_REC)
{
#ifdef DEBUG
printf("Debug : record_rec() : then_rec block\n");
#endif
c2toc1(temp_cond, cmd);
strcpy(rec->else_rec[rec->num_else_rec], temp_cond);
rec->num_else_rec++;
free(temp_cond);
}
}
#ifdef DEBUG
printf("Debug : record_rec() : end\n");
#endif
return ret;
}
Компилится. Но при обращении в rec->then_rec[rec->num_then_rec] "Ошибка сегментирования"
Код:
Program received signal SIGSEGV, Segmentation fault.
0x080491af in record_rec (cmd=0x87c0910, rec=0x87c29d8) at /home/vampire/code/unix_linux_sysprog/head9/e99/record.c:137
warning: Source file is more recent than executable.
137 if((rec->then_rec[rec->num_then_rec] = (char*)malloc(strlen(temp_cond)*sizeof(char))) == NULL)
(gdb)
Компилятор gcc-4.3.3