转换代码中的分段错误(核心转储)错误

时间:2014-08-02 12:22:41

标签: c coredump

我是C编程的新手,面对下面的代码段下面的分段错误。 sample.txt用作文件输入,下面是此文件的内容。

以下是sample.txt文件的内容 -

          1001:"field1":N
          1001:"field2":B
          1001:"field3":A
          1001:"field4":D
          1001:"field5":N
          1001:"field6":N

这在我的代码片段中 -

#include <string.h>
#include<stdio.h>
#include<stdint.h>
#include<stdlib.h>
#define MAX_OID_LEN 128
#define MAX_FILE_NAME_LEN 128
#define MAX_BUFFER_SIZE 80
#define MAXSTRINGSIZE 20
#define MAX_VALUE_LEN 128
int8_t* field[MAX_VALUE_LEN]={0,};     

int8_t* type[MAX_VALUE_LEN]={0,};
int8_t field_count=0;   
char * option(int8_t* type[MAX_VALUE_LEN], int8_t* field[MAX_VALUE_LEN])
{

int8_t i=0;
if(*type)
{
while(**type)
{
switch(**type)
{

case 'D':

printf("\nDateType");
printf("\nfield=%s",field[i]);
break;
case 'N':
printf("\nInteger");
printf("\nfield=%s",field[i]);
break;
case 'A':
printf("\nAscii");
printf("\nfield=%s",field[i]);

break;
case 'B':
printf("\nBinary");
printf("\nfield=%s",field[i]);

break;

default:
printf("\nDefault=%s",field[i]);
return *type;
//break;
}
*type++;
i++;
printf("\ni=%d",i);
}
printf("%s",field[i]);

}
return NULL;

//printf("%s",field[i]);
}


int main()
{
int8_t field_count=0;
        int8_t* data_field;
        int8_t* token;
        int8_t* ismp_field;
        int8_t *search = ":";
        FILE *fp;
        int8_t* filename = "sample.txt";
        fp = fopen ( filename, "r" );
        if( fp != NULL )
        {
                int8_t line [MAX_BUFFER_SIZE];
                while ( fgets ( line, sizeof (line), fp ) != NULL )
                {
                        token = strtok( line, search );
                        ismp_field = strtok( NULL, search );
                        data_field = strtok( NULL, "\n" );
                        if(ismp_field!=NULL)
                        {
                                field[field_count]=strdup(ismp_field);
                                printf("\nismp_field=%s\n", ismp_field);
                                type[field_count]=strdup(data_field);
                                printf("\ndata_value=%s\n",type[field_count]);
                                field_count++;
                        }
                }
                fclose ( fp );


}
char *val=option(type,field);
printf("%s",val);
return 1;
}

2 个答案:

答案 0 :(得分:0)

关于在type函数中取消引用option,这可能就是您的问题所在。因为你将它声明为一个指针数组,并且每个指针都被初始化为NULL,然后你取消引用这些NULL指针,这会导致undefined behavior和你的崩溃。

答案 1 :(得分:0)

需要更改选项功能。找到下面的修复

char * option(int8_t* type[MAX_VALUE_LEN], int8_t* field[MAX_VALUE_LEN])
{

int8_t i=0;
if(*type)
{
while(*type && **type) //Fix
{
switch(**type)
{

case 'D':

printf("\nDateType");
printf("\nfield=%s",field[i]);
break;
case 'N':
printf("\nInteger");
printf("\nfield=%s",field[i]);
break;
case 'A':
printf("\nAscii");
printf("\nfield=%s",field[i]);

break;
case 'B':
printf("\nBinary");
printf("\nfield=%s",field[i]);

break;

default:
printf("\nDefault=%s",field[i]);
return *type;
//break;
}
*type++;
i++;
printf("\ni=%d",i);
}
printf("%s",field[i]);

}

变量类型能够容纳128个指针,但在这种情况下都不使用。所以在使用指针之前检查NULL,这总是很好的做法