插入排序段错误

编程入门 行业动态 更新时间:2024-10-12 01:29:44
本文介绍了插入排序段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个全局变量 COUNT = 0; 和 SIGN = 0; 然后我的#define SIZE 100

我总是收到赛格故障在我的code,但我不太清楚的地方,如果你们能帮助脚下来了我,我会把AP preciate它。

它应该打印出去,而它插入数据一个txt文档阅读任务的总数。我不知道如果我理解的转让实际上是什么,但是这是我走出去了。

无效插入(INT X [],INT N){INT键,Y,Z;COUNT ++;    对于(Z = 1; z,其中,n-1个; Z ++){        键= X [Z]。        SIGN ++;        对于(Y =(Z-1); Y℃的关键|| GT&= X [Y]; Y - ){            X [Y + 1] = X [Y];            SIGN ++;        }        X [Y + 1] = KEY;    }诠释主(){    FILE * FP;    FP = FOPEN(data.txt中,R);    INT #TEMP;    INT I = 0;    为int *排序= NULL;    排序=(INT *)malloc的(SIZE *的sizeof(排序));    而(FP!= NULL){        的fscanf(FP,%D,温度);        排序[I] =(int)的温度;        我++;    }    插入(排序,SIZE);    的printf(分配数=%d个,SIGN);    返回0;}

解决方案

在你的下面code,你不要理会内存溢出。

而(FP!= NULL){        的fscanf(FP,%D,温度);        排序[I] =(int)的温度;        我++;    }

您分配的内存大小的 INT 的。如果文件较大(超过分配的内存大小),那么你的code将访问的内存超出了分配之一。因此,这可能会导致问题

I have two global Variables COUNT=0; and SIGN=0; Then I #define SIZE 100

I keep getting a seg fault in my code but I'm not too sure where, if you guys could help pin it down for me I'd appreciate it.

It is supposed to print out the total number of assignments that go on while it inserts data read in from a txt doc. I'm not sure if I am understanding what an assignment is actually but this is my go out of it.

void insert(int x[], int n){ int key,y,z; COUNT++; for(z=1; z<n-1; z++){ key=x[z]; SIGN++; for(y=(z-1); y<0||key>=x[y]; y--){ x[y+1]=x[y]; SIGN++; } x[y+1]=KEY; } int main(){ FILE *fp; fp=fopen("data.txt","r"); int #temp; int i=0; int *sort=NULL; sort=(int *)malloc(SIZE*sizeof(sort)); while(fp!=NULL){ fscanf(fp, "%d", temp); sort[i]=(int)temp; i++; } insert(sort, SIZE); printf("number of Assignments=%d", SIGN); return 0; }

解决方案

In your below code,you not bother about the memory overflow.

while(fp!=NULL){ fscanf(fp, "%d", temp); sort[i]=(int)temp; i++; }

Your allocated memory is SIZEint. If the file size is large(exceeds allocated memory size), then your code will access the memory beyond the allocated one. So this may cause the problem

更多推荐

插入排序段错误

本文发布于:2023-11-29 05:51:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645521.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!