C_and_CPP 板


LINE

这是部分程式码,不过我编译时,就是qsort()那一个东西发生问题 #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include <algorithm> char* fileName = "studat.txt"; void test( ); void sort( ); void print( ); void print1( ); void getscore(); int myCompare(const void*a,const void *b); typedef struct{}tmp; typedef struct{ long id; char name[9]; int s1,s2,s3,s4; double final; float sum; }Student; Student student[3388],stmp; int main( ) { long long tStart, tStop; double tRun; getscore(); test( ); // 读取档案到记忆体 // 算出每个人的学期总成绩 printf("Before sort...\n"); print( ); // 叫用 print( ) 印出部份资料 tStart = clock( ); sort( ); // 叫用 你的 sort 程式 tStop = clock( ); tRun = 1.0*(tStop - tStart)/CLOCKS_PER_SEC; printf("...Sorting done in %.5f seconds.\n", tRun); printf("After sort...\n"); print1( ); fprintf(stderr, "Hit ENTER key..."); getchar( ); return 0; } void print( ) { int i; printf("学号 姓名 s1 s2 s3 s4 平均成绩\n"); printf("1~10个:\n"); for(i=0;i<10;i++){ printf("%ld %2s %2d %2d %2d %2d %.2f\n",student[i].id,student[i].name,student[i].s1,student[i].s2,student[i].s3,student[i].s4,student[i].sum); } printf("3378~3388个:\n"); for(i=3377;i<3388;i++){ printf("%ld %s %2d %2d %2d %2d %.2f\n",student[i].id,student[i].name,student[i].s1,student[i].s2,student[i].s3,student[i].s4,student[i].sum); } } 以上不是重点,但是重点来了!!!!!! int myCompare(const void*a,const void*b){ Student *p1=(Student*)a; Student *p2=(Student*)b; if((*p1).sum < (*p2).sum){ return -1;} if((*p1).sum>(*p2).sum){ return 1;} return 0; } void sort(){ int student_size=(sizeof(student)/sizeof(student[0])); qsort(student,student_size,sizeof(student),myCompare); } 问题来了,就是我这样写compiler会给我过,但是程式跑到一半,他就会跑出一个对话 视窗,他说应用程式发生错误, "0x004016e1"指令参考的.......记忆体。该记忆体不能为"read"。 请按[确定]终止程式 请按[取消]进行程式侦错 但是神奇的就是如果我把student_size改成"1" 就是qsort(student,1,sizeof(student),myCompare); 这样就可以执行而且还是对的! 因此我就困惑了?? 我是那里写错呢?? --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.113.62.11
1F:推 snowlike:静态阵列宣告能那麽大?再来compare的return值是交换依据 04/06 00:56
2F:→ snowlike:没看到return 1,-1的部份  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄请忽略QQ 04/06 01:00
3F:推 sunneo:如果宣告/定义在全域变数的 会反应在档案大小 04/06 01:06
4F:→ wheredo:这样我要怎样修改勒?静态阵列好像没有那麽大吼~ 04/06 01:07
5F:→ wheredo:呃....snowlike大大你讲的後面那一句,我不懂欸? 04/06 01:10
6F:推 sunneo:因为sizeof(student) != sizeof(Student) 04/06 01:10
7F:→ sunneo:大小写错了 04/06 01:10
8F:→ sunneo:且1 * sizeof(student) == student_size * sizeof(Student) 04/06 01:11
9F:→ wheredo:不过Student他不是一个型别嘛?我上面有写一个struct 04/06 01:13
10F:→ wheredo:我写这样算个数应该不会有错吧,不过他执行就是会有错 04/06 01:13
11F:推 sunneo:当然是型别啊... qsort需要的参数是(阵列,个数,元素大小) 04/06 01:13
12F:→ sunneo:sizeof(Student) 才是单一个Student物件的大小 04/06 01:13
13F:→ sunneo:sizeof(student)得到的是 3328个Student物件的大小 04/06 01:14
14F:→ sunneo:如果你填student_size,sizeof(student) 04/06 01:14
15F:→ sunneo:那会是3328个 (3328个Student) 的Student物件大小 04/06 01:15
16F:→ sunneo:也就是3328 * 3328 * sizeof(Student) 04/06 01:15
17F:→ wheredo:谢谢!我懂了 04/06 01:15
18F:→ sunneo:与你原本想表达的"对3328个Student物件排序"的意思差多了 04/06 01:15
19F:→ sunneo:不过原来我刚刚一直看错啊...是3388 不是3328 04/06 01:16
20F:→ snowlike:呵感谢解答,後面那句是以为myCompare只有return 0的意思 04/06 01:22
21F:→ wheredo:只有return 0的意思? 04/06 01:26
22F:→ wheredo:不是有一堆的if嘛? 04/06 01:27
23F:→ snowlike:so 二楼才那样说 04/06 01:33
24F:→ wheredo:那我问一个和程式无关的问题,就是有时候我的pcman也会跑 04/06 12:19
25F:→ wheredo:出那一个对话窗,说是应用程式出问题,按下确定後 04/06 12:20
26F:→ wheredo:你的pcman就关掉了!! 04/06 12:20
27F:→ wheredo:这是我pcman本身的问题嘛? 04/06 12:21
28F:→ henry666666:还满灵异的 为什麽你可以把我的pcman关掉 04/06 13:26
29F:→ henry666666:没写好 用到不该用的记忆体? 04/06 13:28







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP