Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > C/C++ программирование > Общие вопросы C/C++
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 25.09.2009, 14:32   #1
TheVampire
 
Регистрация: 25.09.2009
Сообщений: 5
По умолчанию SIEGSEGV в malloc()

Добрый день.
Столкнулся с проблемой.
Есть функция для сохранения 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

Последний раз редактировалось TheVampire; 25.09.2009 в 14:58.
TheVampire вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Проблема с malloc Обледеневший Общие вопросы C/C++ 7 14.09.2009 18:06
Своя реализация malloc и free Sazary Общие вопросы C/C++ 14 12.09.2009 18:32
malloc free Ошибка. BeNN Общие вопросы C/C++ 19 09.07.2009 12:46
Проблемы с выделением динамической памяти malloc (recalloc) slips Общие вопросы C/C++ 6 29.04.2009 19:27