分段错误在预定义元素g_int_hash中

时间:2017-01-18 07:32:26

标签: c segmentation-fault hashtable glib

我在程序中使用GHashTable来存储用户输入的文件中的字符串。 除了行

中的分段错误外,没有错误发生

GHashTable* HT = g_hash_table_new(g_int_hash, g_int_equal);

我从未在预定义元素g_int_hash中看到分段错误 错误:编程接收信号SIGSEGV,分段故障。 来自/lib64/libglib-2.0.so.0的g_int_hash()中的0x0000003f2ec74b70

我的主要文件

#include <cmdheader.h>
int main(int argc, char* argv[])
{

        int status = FAILURE;
        int i; //loop-counter
        char** safe_pointer = NULL;

        printf("before HashTable");
        GHashTable* HT = g_hash_table_new(g_int_hash, g_int_equal);

        if(argc < 2)
        {
                printf("\n <USAGE> : <Program_name> <file1> <file2> ...");
                return FAILURE;
        }
        // program needs to be scalable, all files should be read using variable arguments

        for(i = 1; i < argc; i++)
        {
                safe_pointer = &argv[i];
                status = read_CMD(safe_pointer, &HT);
                if(status == FAILURE)
                {
                        FILE_READ_ERROR; 
                }
        }
        status = write_CMD();
        if(status == FAILURE)
        {
                FILE_WRITE_ERROR;
        }
        return SUCCESS;
}

我的header_file

/************************************************************************************************************   
*   File Name   :   cmdheader.h
*
*   Description :   cmdheader.h contains all necessary headerfiles and macros 
*
* *********************************************************************************************************/

#ifndef __CMD_HEADER_H_
#define __CMD_HEADER_H_

/***********************************************************************************************************
 *                                      HEADER FILES 
 **********************************************************************************************************/

#include <stdio.h>
#include <string.h>
#include <glib.h>

/***********************************************************************************************************
 *                                         MACROS 
 **********************************************************************************************************/

#define SUCCESS 0
#define FAILURE 1
#define LINE_LEN 80
#define DELIMS 3

#define FILE_READ_ERROR printf("\n CANNOT READ THE FILE")
#define FILE_WRITE_ERROR printf("\n CANNOT WRITE TO THE FILE")

#endif

我的功能文件

#include <cmdheader.h>

int read_CMD(char** save_pointer, GHashTable** Hash);
int write_CMD();

int read_CMD(char** save_pointer, GHashTable **Hash)
{

        GHashTable *HT = *Hash;
        char buffer[LINE_LEN];
        memset(buffer, '\0', LINE_LEN);

        char* save_pointer_new = *save_pointer;
        int key = 1;
        int* key_pointer = &key;
        int parse_errors = 0;
        int parse_error_check = 1;
        char* token = NULL;

        const char delim[DELIMS] = {',', ':', ';'}; 
        FILE* read_file_pointer;
        read_file_pointer = fopen(save_pointer_new, "r");


        while(NULL != fgets(buffer, LINE_LEN, read_file_pointer))
        {
                // check for parse error
                token = strtok(buffer,delim);
                while(NULL != token)
                {
                        token = strtok(NULL, delim);
                        parse_error_check++;
                }

                // parse_error_check checks number of tokens in a command
                // 2 tokens is error, from 4, it is a valid command
                if(parse_error_check != 2 && 
                                ((parse_error_check % 2) ==0))
                {
                        g_hash_table_insert(HT,key_pointer, buffer);
                        key++;
                }

                else
                {
                        parse_errors++;
                }
        }

        printf("\n Element at key : 1 ", g_hash_table_lookup(HT,(gconstpointer)1));
        printf("\n Parse error count : %d", parse_errors);
        return SUCCESS;



}

int write_CMD()
{

        return SUCCESS;
}

0 个答案:

没有答案