gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:34:23 25 4
gpt4 key购买 nike

我不确定为什么我的程序无法在 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;



}

最佳答案

这段代码中的错误几乎不胜枚举

  • 线阵是固定的。它应该是动态的。读取超过 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;
}

关于c - 程序在 VI 中导致段错误,但在 emacs 中工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23307922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com