第一篇:EV3小狗程序轉(zhuǎn)換類C語言代碼
DN&小狗站立& {
A+D run by 10 until 1 秒 Wait 0.1 s Init A,D }
MNRH *小狗抬低頭* { Print “icon”(0,0)Light yellow flash(Case(p)P=4 C run by-100 P=5 C run by 100 Other C stot)until p2=1 C stop Init C Plight =green flash } IS(1)*顯示表情* { ISS=1 Case(ISS)(ISS=0 print Neutral(0,0)ISS=1 print Seepingl(0,0)ISS=2 print Tear(0,0)ISS=3 print Hurt(0,0)4 tired middle 5 tired right 7 tired left love)}
UP *站起來* {
Case(degree)-50)True(1(Init A
A run by-35 until degree of A 小于-25 A stop A run by-15 until degree of A 小于-65 A stop Wait 0.5s)2(Init B
B run by-35 until degree of A 小于-25 B stop B run by-15 until degree of A 小于-65 B stop Wait 0.5s)}
RST *P和F隨機數(shù)* {
P_T=Random(3,6)*4* F_T=Random(2,4);*3* P_C=F_C=1 Init T1,T2,T3;Sub_CS(0);*DB_S,NS 隨著表情變化* }
CS(0)* DB_S =0,NS=True * { I=0;IF(DB_S不等于I)*DB_S之前沒有初始化,不知何意*(DB_S=I;NS=True;)
}
MON*F_C,P_C=0 DB_S=1;NS=True * { CASE(T2 大于 10s)True(Init T2;P_C=P_C-1;*0* Case(P_C小于0)True P_C =0;)} Case(T1 大于 20s)(True(Init T1;F_C=F_C-1;*0* Case(F_C 小于 0)(F_C=0;))Case(T3大于30s)(Init T3;Sub_CS(1);* DB_S=1;NS=True *))DB_S 判斷小狗動作關(guān)鍵參數(shù),現(xiàn)在DB_S=1
以下全是DB_S產(chǎn)生的Case 1-IDL{
Case(NS)*CS賦值代表DB_S值改變* NS=Fals;*已改變* Sub_UP;*手動抬頭低頭* Sub_UIS*表情7* UPDB PTC FDC } SLP PLF NGR HNG PPP HPY 2-3-4-5-6-7-
UIS { Case(T5 大于 IBP)Init T5;Case(ISS=1)(ISS=6;IBP=Random(1,5);Sub_IS(ISS);*Tire right 表情*)} 讀取IAP*沒用* Case(T6大于5秒)(Case(ISS 不等于1)(Init T6;IAP=random(1,10);)
Case(ISS不等于7)ISS =7; Sub_IS(ISS);*表情*)
UPDB *調(diào)用CS,最終改變DB_S ,NS值* { Case(P_T=P_C and F_T=F_C)Sub_CS(6);Case(P_T大于P_C and F_T小于F_C)Sub_CS(3);Case(P_T小于P_C and F_T大于F_C)Sub_CS(5);Case(P_T=P_C and F_T大于F_C)Sub_CS(5);Case(F_C=0)Sub_CS(4);}*DB_S變動的根源* PTC *狗*sniff* {
OTC=TC TC= 0或1;來觸碰
Case(OTC不等于TC and TC)(P_C=P_C+1;Inti T3;Case(DB_S不等于4)(Sub_IS(2);表情 Sound sniff)
_C=true;)} FDC { _C=假 OCOL=COL COL=顏色傳感器
Case(OCOL不等于COL and COL不等于0)F_C=F_C+1 _C=true Init T3 Sub_IS(2)Sound cruning)} 輸出_C
第二篇:C語言課程設(shè)計代碼
#include “stdio.h” #include
void main(){ int n=0;struct course *head=NULL;void insert(struct course **head,struct course *cou);void Print(struct course **head,int *n);void Modify(struct course **head,int *n);void Require(struct course **head);void Creat(struct course **head,int *n);void Delete(struct course **head,int *n);void Fun(struct course **head,int *n);
Fun(&head,&n);}
void insert(struct course **head,struct course *cou){ struct course *p0,*p1,*p2;p2=p1=*head;p0=cou;if(*head){
while((p0->semester>p1->semester)&&(p1->next))
{
p2=p1;
p1=p1->next;
}
if(p0->semester
semester)
{
if(*head==p1)*head=p0;
else p2->next=p0;
p0->next=p1;}
else
{
if(p0->semester==p1->semester){ while((p0->cID>p1->cID)&&(p1->next)&&(p0->semester==p1->semester))
{
} if(p0->semester!=p1->semester){
} else {
if(p0->cID<=p1->cID){
if(*head==p1)*head=p0;else p2->next=p0;p2=p1;p1=p1->next;p2->next=p0;p0->next=p1;
p0->next=p1;
}
else
{p1->next=p0;p0->next=NULL;}
}
}
else
{p1->next=p0;p0->next=NULL;}
} } else
{
*head=p0;
p0->next=NULL;} }
void Print(struct course **head,int *n){ struct course *p;p=*head;if(*head){
if(*n==1)printf(“nThis %d record is:n”,*n);
else printf(“nThese %d records are:n”,*n);
printf(“semester cID
name
creditn”);
do
{ printf(“%-10d%-10d%-18s%-12.1f n”,p->semester,p->cID,p->name,p->credit);
p=p->next;
}while(p!=NULL);} else printf(“nList null!n”);}
void Modify(struct course **head,int *n){ struct course *p,*p2;int cID;if(*head){
Print(head,n);while(1){ printf(“nPlease input the cID which you want to modify:”);
scanf(“%d”,&cID);p2=p=*head;while(p->next&&(cID!=p->cID)){
p2=p;
p=p->next;} if(cID==p->cID){
printf(“Please input the new cID(1~60):”);
scanf(“%d”,&p->cID);
while(p->cID<0||p->cID>60)
{
printf(“nError!”);
printf(“nPlease input the new cID(1~60):”);
scanf(“%d”,&p->cID);
}
printf(“Please input the new semester(1~8):”);
scanf(“%d”,&p->semester);while(p->semester<0||p->semester>8)
{
printf(“nError!”);
printf(“nPlease input the new semester(1~8):”);
scanf(“%d”,&p->semester);
}
printf(“Please input the new credit:”);
scanf(“%f”,&p->credit);
printf(“Please input the new name:”);
scanf(“%s”,p->name);
if(p==*head)*head=p->next;
else p2->next=p->next;
insert(head,p);
break;
}
else printf(“%d not been found!n”,cID);
} } else {printf(“nList null!n”);} }
void Require(struct course **head){ struct course *p;float sum=0;int sem,i=0;printf(“nPlease input the semester which is required:”);
scanf(“%d”,&sem);p=*head;while(p){
if(sem==p->semester)
{
i++;if(i==1)printf(“nsemester cID
name
creditn”);printf(“%-10d%-10d%-18s%-12.1f n”,p->semester,p->cID,p->name,p->credit);
sum=sum+p->credit;
}
p=p->next;} printf(“The sum of credit in this term is:%.1fn”,sum);}
void Creat(struct course **head,int *n){ struct course *p1;while(1){
p1=(struct course *)malloc(LEN);
printf(“Please input the cID(1~60):”);
scanf(“%d”,&p1->cID);
while(p1->cID<0||p1->cID>60)
{
printf(“nError!”);
printf(“nPlease input the cID(1~60):”);
scanf(“%d”,&p1->cID);
}
if(p1->cID==0)break;
printf(“Please input the semester(1~8):”);
scanf(“%d”,&p1->semester);
while(p1->semester<0||p1->semester>8)
{
printf(“nError!”);
printf(“nPlease input the semester(1~8):”);scanf(“%d”,&p1->semester);
}
} } printf(“Please input the credit:”);scanf(“%f”,&p1->credit);printf(“Please input the name:”);scanf(“%s”,p1->name);insert(head,p1);*n=*n+1;printf(“nYou can continue until the cID is ”0“!n”);Print(head,n);void Delete(struct course **head,int *n){
struct course *p1,*p2;int cID;Print(head,n);if(*head){ printf(“Please input the cID of the course which you want to delete:”);scanf(“%d”,&cID);p1=*head;
while(cID!=p1->cID&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(cID==p1->cID)
{
if(p1==*head)*head=p1->next;
else p2->next=p1->next;
printf(“Have delete cID:%dn”,cID);
*n=*n-1;
}
else printf(“%d not been found!n”,cID);} }
void Fun(struct course **head,int *n){ char num;
while(1)
{
system(“cls”);
puts(“**************** Main Menu ******************”);
puts(“* 1.Add Records
2.Print Records
*”);
puts(“* 3.Delete Records
4.Modify Records *”);
puts(“* 5.Require Records 6.Exit
*”);
printf(“Please input your choice: ”);
scanf(“%d”,&num);
switch(num)
{
case 1:Creat(head,n);break;
case 2:Print(head,n);break;
case 3:Delete(head,n);break;
case 4:Modify(head,n);break;
case 5:Require(head);break;case 6:exit(0);break;
default: break;
}
printf(“nPress ”Enter“ to continue!”);getchar();getchar();
} }
第三篇:c語言實習程序
#include
course_name(int i)/*把科目變成數(shù)字函數(shù)*/ { switch(i)
{
case 1:printf(“英語 ”);
break;
case 2:printf(“數(shù)學”);
break;
case 3:printf(“C語言”);
break;
case 4:printf(“Java”);
break;
case 5:printf(“法律”);
break;
case 6:printf(“音樂”);
break;
case 7:printf(“語文”);
break;
case 8:printf(“體育”);
break;
case 9:printf(“英語”);
break;
case 10:printf(“美術(shù)”);
break;
} }
print2(int m)/*輸出學生考試科目函數(shù)*/ { int i,j;
printf(“學生考試科目:nnn”);
for(i=1;i<=m;i++)
{printf(“
【%d】”,i);
course_name(i);
}
printf(“nn”);}
stu_zhongfen(int(*p2)[100],int n,int m)/*計算學生總分函數(shù)*/ {
int sum,i,j;
for(i=0;i { sum=0; for(j=0;j sum=sum+*(*(p2+i)+j); *(*(p2+i)+m)=sum; } } input(long *p1,int(*p2)[100],int n,int m)/*輸入學號、以及各科成績函數(shù)*/ { int i,j; for(i=0;i { printf(“n請輸入第%d個學生的學號:”,i+1); scanf(“%ld”,p1+i); for(j=0;j { printf(“n請輸入第%d門課程的成績:”,j+1); scanf(“%d”,*(p2+i)+j); } } printf(“......輸入完畢!n”); } chaxun(long *p1,int(*p2)[100],int n,int m)/*查詢學生各科成績、總分、班排名(按總分)函數(shù)*/ { long xuehao; int counter=1,j=0,i,k; while(1) { printf(“n請輸入要查詢的學生的學號:”); scanf(“%ld”,&xuehao); for(i=0;i { if(*(p1+i)==xuehao) break; } if(i>=n)printf(“該學號不存在!請從新輸入!n”); else break; } stu_zhongfen(p2,n,m); while(j { if(j!=i) { if(*(*(p2+j)+m)>*(*(p2+i)+m)) counter++; j++; } else j++; } printf(“<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>n”); printf(“n 您要查詢的學生信息如下:n”); printf(“n”); printf(“ 課程名 分數(shù)n”); for(j=0;j { printf(“ ”); course_name(j+1); printf(“ %4dn”,*(*(p2+i)+j)); } printf(“n 總分為:%3dn”,*(*(p2+i)+m)); printf(“ 班級排名為:%3dn”,counter); printf(“<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>n”); } stu_average(long *p1,int(*p2)[100],int n,int m)/*計算每個學生的平均分*/ { int i,j,k; float average[100]; stu_zhongfen(p2,n,m); for(i=0;i average[i]=*(*(p2+i)+m)/(float)m; printf(“n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>”); printf(“n每個學生的平均分如下n”); printf(“ 學號 平均分n”); for(i=0;i printf(“ %4ld %fn”,*(p1+i),average[i]); printf(“n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>n”); } score_jiangxu(int(*p2)[100],int n,int m)/*把每個學生按平均分高到低排名函數(shù)*/ { int i,j,k,temp,a[100],z; float average[100]; stu_zhongfen(p2,n,m); printf(“n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>n”); for(i=0;i { a[i]=*(*(p2+i)+m); average[i]=*(*(p2+i)+m)/(float)m; } for(i=0;i { for(k=1;k if(a[i] { temp=a[i]; a[i]=a[k]; a[k]=temp; } } printf(“排名 平均分n”); for(i=0;i printf(“(%d) %fn”,i+1,average[i]); printf(“<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>n”); } print1()/*提示函數(shù)*/ { system(“cls”);/*清屏*/ printf(“1.查詢學生各科成績、總分、班排名(按總分)n”); printf(“2.計算每個學生的平均分 n”); printf(“3.把每個學生按平均分高到低排名n”); printf(“請選擇:”);} main(){ int m,n,i; long *p1,num[100]; int(*p2)[100],score[100][100]; printf(“n請輸入學生總數(shù):”); scanf(“%d”,&n); printf(“n請輸入課程總數(shù):”); scanf(“%d”,&m); p1=num; p2=score; printf(“n ★★現(xiàn)在請輸入學號和成績★★n”); print2(m); input(p1,p2,n,m); print1(); scanf(“%d”,&i); if(i<0||i>3) printf(“.....輸入錯誤!....”); else switch(i) { case 1: chaxun(p1,p2,n,m);break; case 2: stu_average(p1,p2,n,m);break; case 3: score_jiangxu(p2,n,m);break; } } #include //定義學生結(jié)構(gòu)體,st數(shù)組。 int score[5];float jqave;int rank;}st[27]; struct kecheng { int num;char kcname[20]; //定義課程結(jié)構(gòu)體,course數(shù)組.float xuefen;float biaozhuncha;int youxiu;int lianghao;int zhongdeng;int jige;int bujige; }course[5]; void readdata1(){ int i,j; char str1[100]; FILE *f1; f1=fopen(“c:xinji110213.txt”,“r”); fgets(str1,100,f1);// printf(“%-s”,str1); for(i=0;i<27;i++) { fscanf(f1,“%d%s”,&st[i].num,st[i].name); // printf(“%-4d%-10s”,st[i].num,st[i].name); for(j=0;j<5;j++) { fscanf(f1,“%d”,&st[i].score[j]);// printf(“%-9d”,st[i].score[j]);} // printf(“n”);} fclose(f1);} void readdata2(){ int k; char str2[100]; FILE *f2; f2=fopen(“c:xj02kc.txt”,“r”); fgets(str2,100,f2);// puts(str2); for(k=0;k<5;k++) { fscanf(f2,“%d%s%f”,&course[k].num,course[k].kcname,&course[k].xuefen); // printf(“%-8d%-10s%2.1fn”,course[k].num,course[k].kcname,course[k].xuefen); } fclose(f2);} void jqave(){ int i,j;float s=0,sum=0;for(i=0;i<27;i++) { for(j=0;j<5;j++) { s+=course[j].xuefen; sum+=st[i].score[j]*course[j].xuefen; } st[i].jqave =sum/s; } } void ranking() { int i,j; for(i=0;i<27;i++){ st[i].rank=1; for(j=0;j<27;j++) if(st[i].jqave st[i].rank=st[i].rank+1;} } void paiminghoudechengji(){ int i,j;FILE *fp;fp=fopen(“c:MC110213.txt”,“w”);printf(“ 名次 學號 姓名 數(shù)學分析 高等代數(shù) C語言 大學物理 大學藝術(shù) 加權(quán)平均分 n”);fprintf(fp,“ 名次 學號 姓名 數(shù)學分析 高等代數(shù) C語言 大學物理 大學藝術(shù) 加權(quán)平均分 n”); for(i=0;i<27;i++) { printf(“%3d%5d%8s”,st[i].rank,st[i].num,st[i].name); fprintf(fp,“%3d%5d%8s”,st[i].rank,st[i].num,st[i].name); for(j=0;j<5;j++) { printf(“%10d”,st[i].score[j]); fprintf(fp,“%10d”,st[i].score[j]); } printf(“%12.2fn”,st[i].jqave); fprintf(fp,“%12.2fn”,st[i].jqave);} fclose(fp);} void xiechengji(){ int j;FILE *fp;fp=fopen(“c:xj1110213.txt”,“w”);fprintf(fp,“ 編號 課程名稱 課程學分 標準差 優(yōu)秀 良好 中等 及格 不及格 n”);printf(“ 編號 課程名稱 課程學分 標準差 優(yōu)秀 良好 中等 及格 不及格 n”);for(j=0;j<5;j++) { printf(“%3d%15s%10.2f”,course[j].num,course[j].kcname,course[j].xuefen);fprintf(fp,“%3d%15s%10.2f”,course[j].num,course[j].kcname,course[j].xuefen); printf(“%10.1f%8d%8d%8d%8d%6dn”,course[j].biaozhuncha,course[j].youxiu,course[j].lianghao,course[j].zhongdeng,course[j].jige,course[j].bujige); fprintf(fp,“%10.1f%8d%8d%8d%8d%6dn”,course[j].biaozhuncha,course[j].youxiu,course[j].lianghao,course[j].zhongdeng,course[j].jige,course[j].bujige); } fclose(fp);} void pingjunfen(){ int i,j; for(j=0;j<5;j++) { float zongfen=0,pingjunfen=0; for(i=0;i<27;i++) zongfen+=st[i].score[j]; pingjunfen=zongfen/27; } } void biaozhuncha(){ int i,j; for(j=0;j<5;j++){ float zongfen=0,pingjunfen=0,x=0; for(i=0;i<27;i++) zongfen+=st[i].score[j]; pingjunfen=zongfen/27; for(i=0;i<27;i++) x+=(st[i].score[j]-pingjunfen)*(st[i].score[j]-pingjunfen); course[j].biaozhuncha=(float)sqrt((x/27)); } } void count(){ int i,j; for(i=0;i<5;i++) { course[i].youxiu=0,course[i].lianghao=0,course[i].zhongdeng=0,course[i].jige=0,course[i].bujige=0; for(j=0;j<27;j++) { if(st[j].score[i]<60) course[i].bujige++; else if(st[j].score[i]<=69) course[i].jige++; else if(st[j].score[i]<=79) course[i].zhongdeng++; else if(st[j].score[i]<=89) course[i].lianghao++; else course[i].youxiu++; } } } void chengjitiao(){ int i,j;FILE *fp;fp=fopen(“c:cjt110213.txt”,“w”);do { printf(“請輸入你要查詢的編號 退出請按零”); scanf(“%d”,&i); fprintf(fp,“ 編號 姓名 數(shù)學分析 高等代數(shù) C語言 大學物理 大學藝術(shù) 加權(quán)平均分 名次n”);printf(“ 編號 姓名 數(shù)學分析 高等代數(shù) C語言 大學物理 大學藝術(shù) 加權(quán)平均分 名次n”);if(i>=0&&i<=26)fprintf(fp,“%3d%10s”,st[i-1].num,st[i-1].name); printf(“%3d%10s”,st[i-1].num,st[i-1].name);for(j=0;j<5;j++){ printf(“%9d”,st[i-1].score[j]); fprintf(fp,“%9d”,st[i-1].score[j]);} printf(“%12.2f%8dn”,st[i-1].jqave,st[i-1].rank);fprintf(fp,“%12.2f%8dn”,st[i-1].jqave,st[i-1].rank);fclose(fp);} while(i>0&&i<28); } void bujigexueshengmingdan(){ int i,j; printf(“不及格學生名單如下:n”);printf(“ 學號 姓名 不及格課程 分數(shù) n”);for(i=0;i<27;i++){ for(j=0;j<5;j++) if(st[i].score[j]<60) {printf(“%3d%12s”,st[i].num,st[i].name); printf(“%15s%8dn”,course[j].kcname,st[i].score[j]); // printf(“ 平均分”); // printf(“%10.2f 名次%8dn”,st[i].jqave,st[i].rank); } } } void youdeng(){ int i,k;printf(“ 優(yōu)等生名單如下n”);printf(“編號 姓名 數(shù)學分析 高等代數(shù) C語言 大學物理 大學藝術(shù) 加權(quán)平均分 名次n”);for(i=0;i<27;i++){ // for(j=0;j<5;j++) if(st[i].jqave>=90||st[i].rank<=3) { printf(“%2d%10s”,st[i].num,st[i].name); for(k=0;k<5;k++) printf(“%9d”,st[i].score[k]); printf(“%10.2f%8dn”,st[i].jqave,st[i].rank); } else; } } void caidan() { loop: printf(“ &&簡易教學管理系統(tǒng)&&n”); printf(“press 1 for 排名后的成績表 !n”); printf(“press 2 for 各科成績情況!n”); printf(“press 3 for 成績條!n”); printf(“press 4 for 不及格學生名單!n”); printf(“press 5 for 優(yōu)等生名單!n”); printf(“press 0 for 退出n”); int t; scanf(“%d”,&t); switch(t) { case 1: paiminghoudechengji();break; case 2: xiechengji();break; case 3: chengjitiao();break; case 4: bujigexueshengmingdan();break; case 5: youdeng();break; case 0: exit(0); } goto loop; } void main(){ readdata1(); readdata2(); jqave(); ranking(); pingjunfen(); biaozhuncha();count(); caidan();} 提高C語言程序運行穩(wěn)定性的方法 一、前言 由于C語言的靈活性,用C語言開發(fā)出來的程序容易造成內(nèi)存泄漏、運行異常、運行結(jié)果不可預期等程序質(zhì)量問題,在用C語言開發(fā)程序的過程中,必須高度重視程序質(zhì)量問題,應當把提高程序穩(wěn)定性的方法加入到項目管理和開發(fā)過程中,最大限度地提高程序的穩(wěn)定性,保證項目的成功開發(fā)。在這里總結(jié)多年來的C語言開發(fā)經(jīng)驗,拿出來共享以期在這方面能夠得到更多的指教。 二、影響程序穩(wěn)定性的因素 1、內(nèi)存泄漏。造成內(nèi)存泄漏的原因有: 1)、程序有多個出口,但不能保證在每一個出口能夠完全釋放掉所有的動態(tài)內(nèi)存,如函數(shù)內(nèi)有多個“return”,但沒有在每一個“return”前釋放掉在原已申請但必須釋放的動態(tài)內(nèi)存; 2)、對于“struct”數(shù)據(jù)結(jié)構(gòu),沒有完全釋放掉每一個指向動態(tài)內(nèi)存的指針,如只釋放指向“struct”數(shù)據(jù)結(jié)構(gòu)指針沒有釋放“struct”體內(nèi)的指針或某些指針被漏釋放; 3)、對于用動態(tài)內(nèi)存建立的鏈表在釋放時沒有一個一個結(jié)點去釋放; 4)、一段動態(tài)內(nèi)存空間原來只被一個指針引用,但在這個指針引用另外一段內(nèi)存空間的時候,該段內(nèi)存沒有被釋放; 5)、對于在函數(shù)內(nèi)申請但必須在函數(shù)外釋放的動態(tài)內(nèi)存,在對該內(nèi)存使用后忽略該動態(tài)內(nèi)存的釋放; 6)、用戶強行退出程序,程序在退出前不能完全釋放掉所有的動態(tài)內(nèi)存; 7)、程序運行過程中發(fā)生了異常導致動態(tài)內(nèi)存未被釋放。 2、程序運行發(fā)生異常。造成異常產(chǎn)生的原因有: 1)、釋放指針時該指針為空或是一個已被釋放但釋放后未被置空的指針; 2)、對于C庫中的函數(shù),如字符串操作函數(shù),在調(diào)用該類函數(shù)時實參為空指針或者改指針沒有指向可用的內(nèi)存地址空間或者所指向的內(nèi)存空間大小不足以用來實現(xiàn)當前的字符串操作; 3)、對于指向一個“struct”數(shù)據(jù)結(jié)構(gòu)的指針,當指針為空時使用“struct”的分體數(shù)據(jù); 4)、數(shù)組或指針發(fā)生越界操作; 5)、指針指向一個已被釋放但釋放后未被置空的指針,如一個全局變量的指針,在一個地方被釋放后,但指針值未被置空,這時在另一個地方引用該指針的值時會發(fā)生異常; 6)、更改定義為常量的值; 7)、動態(tài)申請完一個內(nèi)存后,未檢查是否申請成功就調(diào)用了該指針; 8)、對于一塊連續(xù)的內(nèi)存塊和“struct”數(shù)據(jù)結(jié)構(gòu)在第一次使用時沒有做初始化操作。 9)、在用非ASCII(如中文字符、Unicode)編碼時,若使用char*來申請空間,在用C庫中的字符串操作函數(shù)來操作,會因無法判斷字符串結(jié)束位置而產(chǎn)生異常。 10)、指針類型強制轉(zhuǎn)換時,當強制轉(zhuǎn)換后指針指向的內(nèi)存空間大于原來指針指向的內(nèi)存空間時可能會出現(xiàn)異常(取決于堆或??臻g的結(jié)構(gòu)和大小),如把“INT12*”強制轉(zhuǎn)換成“INT32*”,應當盡量避免指針類型的強制轉(zhuǎn)換; 11)、更改了數(shù)據(jù)結(jié)構(gòu),但代碼沒有相應更新或整個工程中相關(guān)文件沒有做相應更新; 12)、申請的??臻g或堆空間超出了系統(tǒng)的容量限制; 13)、棧溢出,當函數(shù)中定義一個太大的數(shù)組時容易造成棧溢出,遞歸調(diào)用太深也容易造成棧舉出; 14)、全局變量使用混亂,造成程序錯亂; 16)、內(nèi)存碎片太多,造成內(nèi)存分配失敗而導致程序異常,如建立一個太長的鏈表容易造成大量內(nèi)存碎片; 17)、文件操作過于頻繁(特別是寫操作),系統(tǒng)應付不過來容易造成程序出現(xiàn)異常,這個在嵌入式系統(tǒng)中較常見。 三、內(nèi)存泄漏預防措施 1、在代碼審查時,檢查函數(shù)體內(nèi)的每一個“return”前是否有沒有釋放必須要釋放的指針; 2、設(shè)計“struct”數(shù)據(jù)結(jié)構(gòu)時,應當設(shè)計相應的釋放“struct”指針的函數(shù),并確保所有的“struct”體內(nèi)的指針都被釋放; 3、對于用動態(tài)內(nèi)存建立的鏈表在釋放時要一個一個結(jié)點去釋放, 對于每一個鏈表也要有相應的鏈表內(nèi)存管理函數(shù),如鏈表的釋放函數(shù); 4、當一個指針變量要指向另一個動態(tài)內(nèi)存地址時先檢查一下該指針是否有指向另一個動態(tài)內(nèi)存地址,如果有則應當考慮是否要先釋放掉原先的指向的動態(tài)內(nèi)存; 5、在調(diào)用一個函數(shù)時,對于函數(shù)的輸出值要確認值的內(nèi)存空間是否是在函數(shù)內(nèi)部動態(tài)申請,如果是則應當考慮是適當?shù)臅r候把它釋放掉; 6、減少程序的出口的數(shù)目,最好是一個出口,在出口處理函數(shù)中確保釋放所有的動態(tài)內(nèi)存; 7、當用戶強行退出時,要考慮在每一個退出點是否能夠釋放所有的動態(tài)內(nèi)存; 8、釋放掉一個指針所指的內(nèi)存空間后,就立即把改指針置為空; 9、少用動態(tài)申請內(nèi)存,能用數(shù)組代替的就用數(shù)組的形式; 10、盡量減少全局變量的使用,避免指針指向的混亂; 11、封裝動態(tài)內(nèi)存申請和釋放的底層函數(shù),便于檢查內(nèi)存泄漏問題; 12、把內(nèi)存泄漏的檢查方法放進設(shè)計代碼中,便于發(fā)現(xiàn)內(nèi)存泄漏。 四、程序運行異常預防措施 1、在釋放指針前先檢查指針是否為空; 2、當把指針作為參數(shù)傳入C庫函數(shù)中的參數(shù)時,先檢查指針是否為空; 3、在函數(shù)體內(nèi),當要調(diào)用指針參數(shù)時,先判斷該指針是否為空; 4、當要調(diào)用“struct”指針數(shù)據(jù)結(jié)構(gòu)中的分體時要先判斷該指針是否為空; 5、當做指針移動操作時要考慮指針是否會發(fā)生越界; 6、當一個函數(shù)體內(nèi)可能會改變參數(shù)中的值時,要避免傳入常量形式的值,在設(shè)計函數(shù)時要盡量避免試圖去改變參數(shù)中的值; 7、動態(tài)申請完一個內(nèi)存后要先檢查是否申請成功; 8、對于一塊連續(xù)的內(nèi)存塊和“struct”數(shù)據(jù)結(jié)構(gòu)在第一次使用時要做初始化操作,如申請完內(nèi)存后,記得用memset清空內(nèi)存; 9、備案所有的全局變量,考慮全局變量對程序可能產(chǎn)生的影響,盡量少用全局變量。對于全局變量的定義最好使用“static”來申明,不讓其它模塊直接訪問該全局變量,并且設(shè)計好相應的操作該全局變量的方法函數(shù),在定義全局變量時要充分考慮好全局變量的初始化方法和程序結(jié)束時的處理方法,對于整個工程中的全局變量要進行登記管理,登記內(nèi)容包括變量名、類型名、定義位置、使用范圍、使用目的、初始化方法、程序結(jié)束時的處理方法及其它注意事項。 10、在用非ASCII(如中文字符、Unicode)編碼時,要使用unsigned char*來申請空間,并記住申請空間大小,不要用C庫中的字符串操作函數(shù)來操作。 11、記得申請足夠的內(nèi)存,比如,儲存年份應該是5個空間而不是4個,記得保留‘
第四篇:C語言課程設(shè)計程序
第五篇:C語言程序穩(wěn)定性