Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
程序在VI中导致分段错误,但在emacs中工作正常_C - Fatal编程技术网

程序在VI中导致分段错误,但在emacs中工作正常

程序在VI中导致分段错误,但在emacs中工作正常,c,C,我不确定为什么我的程序不能在vi上正确编译。它只打印函数show(var)的第一次出现,然后退出并列出分段错误和核心转储,但是,它在emacs上编译时没有任何错误,并在快速排序后显示所有字符串 程序应该从我存储在同一目录中的文本文件中读取数据,并使用两个比较函数之一对其进行快速排序(不一定要有意义,它们只需要有功能),然后将其打印到屏幕上 提前谢谢 #include <stdio.h> #include <stddef.h> #include <stdlib.h&g

我不确定为什么我的程序不能在vi上正确编译。它只打印函数show(var)的第一次出现,然后退出并列出分段错误和核心转储,但是,它在emacs上编译时没有任何错误,并在快速排序后显示所有字符串

程序应该从我存储在同一目录中的文本文件中读取数据,并使用两个比较函数之一对其进行快速排序(不一定要有意义,它们只需要有功能),然后将其打印到屏幕上

提前谢谢

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>


void show(void *array[]){

    int i = 0;
    while(array[i]!=NULL){
       printf("String %d : %s\n",i, array[i]);

      i++;
    }
    printf("\n");
}

  void *readData(void * lineArray[]){

        static const char filename[] = "sampledata.txt";
        FILE *file = fopen ( filename, "r" );
        if ( file != NULL )
        {
            int i ;
            char line [ 128 ]; /* or other suitable maximum line size */

            void *lineadrs ;


            i = 0;

            lineadrs = malloc(sizeof(void) * 1024);
            while ( fgets ( lineadrs, sizeof line, file ) != NULL ) /* read a line */
            {
                lineArray[i] = lineadrs;

                lineadrs = malloc(sizeof(void) * 1024);

                i++;
            }
            fclose ( file );
        }


        else {
            perror ( filename );

            return 0;

        }
        return lineArray ;
    }



void swap(void *v[], int i, int j)
{
    void *temp;
    temp = v[i];
    v[i] = v[j];
    v[j]=temp;
}

//normal compare
int cmp1 (void *first_arg,  void *second_arg)
{

    if ( *(char*)first_arg <  *(char*)second_arg )
    {
        return -1;
    }
    if ( *(char*)first_arg == *(char*)second_arg )
    {
        return 0;
    }
    else    {
        return 1;
    }

}

//reverse the compare
int cmp2  (void * a, void * b)
{
    char *ia = (char *)a; // casting pointer types
    char *ib = (char *)b;
    return *ib  - *ia;
    //return ( *(int *)b + *(int *)a );
}


void QSort(void *v[],int left, int right, int (*compare)(void *first, void *second))
{
    int i, last;
    void swap (void *v[],int ,int);


    if(left >= right){
        return;
    }

    swap(v,left,(left+right)/2);
    last=left;
    for(i=left+1;i<=right; i++){
        if((*compare)(v[i],v[left])<0){
            swap(v,++last,i);
        }
    }

    swap(v,left,last);
    QSort(v,left,last-1,compare);
    QSort(v,last+1,right,compare);
}




int main(){
    void * var[6];
    readData(var);
    printf("Original String:\n");
    show(var);

    QSort(var,0,4,cmp1);
    printf("After cmp 1 which compares alphabetically.\n");
    show(var);

    QSort(var,0,4,cmp2);
    printf("After cmp 2 which compares reverse alphabetically.\n");
    show(var);


    return 0;



}
#包括
#包括
#包括
#包括
无效显示(无效*数组[]){
int i=0;
while(数组[i]!=NULL){
printf(“字符串%d:%s\n”,i,数组[i]);
i++;
}
printf(“\n”);
}
void*readData(void*lineArray[]{
静态常量字符文件名[]=“sampledata.txt”;
FILE*FILE=fopen(文件名,“r”);
如果(文件!=NULL)
{
int i;
字符行[128];/*或其他合适的最大行大小*/
无效*lineadrs;
i=0;
lineadrs=malloc(sizeof(void)*1024);
while(fgets(lineadrs,sizeof line,file)!=NULL)/*读取一行*/
{
线性阵列[i]=线性阵列;
lineadrs=malloc(sizeof(void)*1024);
i++;
}
fclose(文件);
}
否则{
perror(文件名);
返回0;
}
回归线性排列;
}
无效交换(无效*v[],整数i,整数j)
{
真空*温度;
温度=v[i];
v[i]=v[j];
v[j]=温度;
}
//正常比较
int cmp1(无效*第一个参数,无效*第二个参数)
{
如果(*(字符*)第一个字符<*(字符*)第二个字符)
{
返回-1;
}
如果(*(字符*)第一个字符==*(字符*)第二个字符)
{
返回0;
}
否则{
返回1;
}
}
//反向比较
int cmp2(无效*a,无效*b)
{
char*ia=(char*)a;//强制转换指针类型
char*ib=(char*)b;
返回*ib-*ia;
//返回(*(int*)b+*(int*)a);
}
void QSort(void*v[],int left,int right,int(*比较)(void*first,void*second))
{
int i,最后;
无效交换(无效*v[],整数,整数);
如果(左>=右){
返回;
}
交换(v,左,(左+右)/2);
最后=左;

对于(i=left+1;i来说,这段代码中错误的列表几乎数不胜数

  • 行数组是固定的。它应该是动态的。读取超过4行的文本将通过超过输入数组长度调用未定义的行为
  • 字符串内容的比较器错误
  • 内存泄漏很多
下面的代码,我相信,就是你想要做的。我真诚地希望你花点时间从中学习。仍然有一些事情需要做,但区别已经是日日夜夜了。我应该警告你,我是在网上写的,没有给它测试时间,但它应该是正确的。因为我没有你的示例数据,这是我所能做的。祝你好运

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// read data from a named file one line at a time, storing each
//  in a ever-expanding line array. The return result is the
//  number of lines allocated. The resulting line array is passed
//  as an output parameter
int readData(const char filename[], void ***results)
{
    // default answer: no lines, zero-length
    void **lines = NULL;
    int i=0;


    FILE *file = fopen ( filename, "r" );
    if ( file != NULL )
    {
        char line [ 128 ];
        while ( fgets ( line, sizeof line, file ) != NULL )
        {
            // trim the newline from line buffer
            size_t slen = strlen(line);
            if (slen > 0 && line[slen-1] == '\n')
                line[--slen] = 0;

            // resize lines array
            void **new_lines = realloc(lines, (i+1)*sizeof(*new_lines));
            if (new_lines == NULL)
            {
                perror("Failed to realloc lines array.");
                exit(EXIT_FAILURE);
            }

            // save new line entry, terminate with NULL;
            lines = new_lines;
            lines[i++] = strdup(line);
        }

        fclose ( file );
    }
    else
    {
        perror(filename);
        exit(EXIT_FAILURE);
    }

    // setup output result and return value
    *results = lines;
    return i;
}


// display an array of a specified length
void show(void *array[], int len)
{
    int i=0;
    for (; i<len; ++i)
        printf("String %d : %s\n", i, array[i]);
    printf("\n");
}

//normal compare
int cmp1 (void *first_arg,  void *second_arg)
{
    return strcmp((const char*)first_arg, (const char*)second_arg);
}

//reverse the compare
int cmp2 (void *first_arg, void *second_arg)
{
    return strcmp((const char*)second_arg, (const char*)first_arg);
}

// swap to void* by address
void swap(void **lhs, void **rhs)
{
    void *tmp = *lhs;
    *lhs = *rhs;
    *rhs = tmp;
}

// the simplest quicksort I can fathom
void QSort(void *v[], int len, int (*compare)(void*, void*))
{
    if (len < 2)
        return;

    // swap random element to last slot
    swap(v+(rand() % len), v+(len-1));

    // partition around the pivot value
    int pvt=0,i;
    for (i=0; i<len; ++i)
    {
        if (compare(v[i], v[len-1]) < 0)
            swap(v+i, v+pvt++);
    }

    // swap pivot into place
    swap(v+pvt, v+(len-1));

    // recurse. note the pivot slot is skipped.
    QSort(v, pvt++, compare);
    QSort(v+pvt, len-pvt, compare);
}


int main()
{
    static const char filename[] = "sampledata.txt";

    srand((unsigned)time(NULL));

    void **var = NULL;
    int len = readData(filename, &var);

    if (len > 0)
    {
        printf("Original String:\n");
        show(var, len);

        QSort(var, len, cmp1);
        printf("After cmp 1 which compares alphabetically.\n");
        show(var, len);

        QSort(var, len, cmp2);
        printf("After cmp 2 which compares reverse alphabetically.\n");
        show(var, len);

        // release lines when finished
        while (len-- != 0)
            free(var[len]);
        free(var);
    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
//从命名文件中一次读取一行数据,并存储每行数据
//在不断扩展的线数组中。返回结果为
//分配的行数。传递生成的行数组
//作为输出参数
int readData(常量字符文件名[],无效***结果)
{
//默认答案:无行,长度为零
空**行=空;
int i=0;
FILE*FILE=fopen(文件名,“r”);
如果(文件!=NULL)
{
字符行[128];
while(fgets(行、行大小、文件)!=NULL)
{
//从行缓冲区修剪换行符
尺寸=标准长度(直线);
如果(slen>0&&line[slen-1]='\n')
行[--slen]=0;
//调整线阵列的大小
void**new_line=realloc(line,(i+1)*sizeof(*new_line));
if(新行==NULL)
{
perror(“无法重新定位行数组”);
退出(退出失败);
}
//保存新行条目,以空终止;
线=新的线;
行[i++]=strdup(行);
}
fclose(文件);
}
其他的
{
perror(文件名);
退出(退出失败);
}
//设置输出结果和返回值
*结果=线;
返回i;
}
//显示指定长度的数组
无效显示(无效*数组[],整数长度)
{
int i=0;

例如(;如果从什么时候开始,编辑器开始影响程序的正确性?我不知道,但我也不知道为什么相同的代码在vi中不断出现,而在emacsI中它不会出现。我已经重新键入了两次,我想知道我是否遗漏了一些东西编译器的邪恶扩展允许
sizeof(void)
甚至编译?为什么要分配1024个“那些”,然后将
fgets()
的读取大小限制为完全不相关的变量(
)除了在要求它的大小时报告值
128
之外,它是未使用的?
vi
emacs
是编辑器,而不是编译器。你不能“在vi上编译它”等等。