線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2871
推到 Plurk!
推到 Facebook!

請問要怎麼釋放記憶體?

尚未結案
happyzeus
一般會員


發表:1
回覆:3
積分:0
註冊:2002-10-20

發送簡訊給我
#1 引用回覆 回覆 發表時間:2002-10-20 11:57:31 IP:211.74.xxx.xxx 未訂閱
我寫了一個程式(使用c語言)~作用是讀入一個文字檔(全英文,長篇) 統計每個英文單字各出現幾次,並且秀出來 我寫完之後只能讀到前面大約10來個英文單字 之後的就好像消失了 老師說要做釋放記憶體的動作 請問要如何做到這個動作?? 問題是在哪部分的記憶體不夠用? 我的寫法是利用fgetc()獎文字檔裡的字元一個一個抓出來 利用一個暫存的char陣列放著~直到讀到的字元非(a~z或A~Z) 再用一個鏈結串列存出現的單字跟其出現次數 是從檔案讀入的地方記憶體不夠還是鏈結串列的地方還是其他?? 解決的方法? 麻煩各位前輩了^^
RaynorPao
版主


發表:139
回覆:3622
積分:7025
註冊:2002-08-12

發送簡訊給我
#2 引用回覆 回覆 發表時間:2002-10-20 12:04:56 IP:61.221.xxx.xxx 未訂閱
引言: 我寫了一個程式(使用c語言)~作用是讀入一個文字檔(全英文,長篇) 統計每個英文單字各出現幾次,並且秀出來 我寫完之後只能讀到前面大約10來個英文單字 之後的就好像消失了 老師說要做釋放記憶體的動作 請問要如何做到這個動作?? 問題是在哪部分的記憶體不夠用? 我的寫法是利用fgetc()獎文字檔裡的字元一個一個抓出來 利用一個暫存的char陣列放著~直到讀到的字元非(a~z或A~Z) 再用一個鏈結串列存出現的單字跟其出現次數 是從檔案讀入的地方記憶體不夠還是鏈結串列的地方還是其他?? 解決的方法? 麻煩各位前輩了^^
把你的 code 放上來吧
------
-- 若您已經得到滿意的答覆,請適時結案!! --
-- 欲知前世因,今生受者是;欲知來世果,今生做者是 --
-- 一切有為法,如夢幻泡影,如露亦如電,應作如是觀 --
happyzeus
一般會員


發表:1
回覆:3
積分:0
註冊:2002-10-20

發送簡訊給我
#3 引用回覆 回覆 發表時間:2002-10-20 12:15:12 IP:211.74.xxx.xxx 未訂閱
#include  #include #include typedef struct wordlist{ char input[32]; int count; struct wordlist *next; }node; node *head=NULL; node *new_node; node *ptr; void add_to_list (char tmp[]){ new_node=(node *)malloc(sizeof(node)); if(new_node==NULL){ printf("memory alloc error\n"); exit(1); } strcpy(new_node->input,tmp); new_node->count=1; new_node->next=NULL; ptr->next=new_node; } void print_result(){ for(ptr=head;ptr->next!=NULL;ptr=ptr->next) printf("%s\n%d\n",ptr->input,ptr->count); printf("%s\n%d\n",ptr->input,ptr->count); } void main(int argc, char *argv[]) { FILE *fpin; char c,tmp[32]=" "; int i=0,s=1; fpin = fopen("input.txt", "r"); if(!fpin){ printf("The file: %s is not found!\n", argv[1]); exit(1); } else printf("the file: %s is open!\n",argv[1]); while((c=fgetc(fpin))!=EOF){ if((c>'a' & c<'z') | (c>'A' & c<'z')){ tmp[i]=c; i ; } else{ if((tmp[0]>'a' & tmp[0]<'z') | (tmp[0]>'A' & tmp[0]<'z')){ if(head==NULL){ head=(node *)malloc(sizeof(node)); if(head==NULL){ printf("memory alloc error\n"); exit(1); } strcpy(head->input,tmp); head->count=1; head->next=NULL; } else{ for(ptr=head;ptr->next!=NULL;ptr=ptr->next){ if(strcmp(ptr->input,tmp)==0){ ptr->count=ptr->count 1; s=0; } } if(s) add_to_list (tmp); } } for(i=0;i<32;i ) tmp[i]=' '; i=0; } } print_result(); fclose(fpin); }
happyzeus
一般會員


發表:1
回覆:3
積分:0
註冊:2002-10-20

發送簡訊給我
#4 引用回覆 回覆 發表時間:2002-10-20 12:18:22 IP:211.74.xxx.xxx 未訂閱
麻煩前輩幫我看看^^ //input.txt this is a Big test. that is another test from the homework. Big apple is good but another is better
dllee
站務副站長


發表:321
回覆:2519
積分:1711
註冊:2002-04-15

發送簡訊給我
#5 引用回覆 回覆 發表時間:2002-10-20 14:15:21 IP:203.204.xxx.xxx 未訂閱
引言:
#include 
#include 
#include 
typedef struct wordlist{
        char input[32];
        int count;
        struct wordlist *next;
}node;
        node *head=NULL;
        node *new_node;
        node *ptr;
void add_to_list (char tmp[]){
        new_node=(node *)malloc(sizeof(node));
        if(new_node==NULL){
          printf("memory alloc error\n");
          exit(1);
        }
        strcpy(new_node->input,tmp);
        new_node->count=1;
        new_node->next=NULL;                        
        ptr->next=new_node;
}
void print_result(){
        for(ptr=head;ptr->next!=NULL;ptr=ptr->next)
          printf("%s\n%d\n",ptr->input,ptr->count);
        printf("%s\n%d\n",ptr->input,ptr->count);
}
void main(int argc, char *argv[])
{
        FILE *fpin;
        char c,tmp[32]="                               ";            int i=0,s=1;
        fpin = fopen("input.txt", "r");
        if(!fpin){
          printf("The file: %s is not found!\n", argv[1]);
          exit(1);
        }
        else
          printf("the file: %s is open!\n",argv[1]);
        while((c=fgetc(fpin))!=EOF){
        if((c>'a' & c<'z') | (c>'A' & c<'z')){
          tmp[i]=c;
          i++;
        }
        else{
                if((tmp[0]>'a' & tmp[0]<'z') | (tmp[0]>'A' & tmp[0]<'z')){
                  if(head==NULL){
                    head=(node *)malloc(sizeof(node));
                    if(head==NULL){
                         printf("memory alloc error\n");
                        exit(1);
                    }
                    strcpy(head->input,tmp);
                    head->count=1;
                    head->next=NULL;
                  }
                  else{
                    for(ptr=head;ptr->next!=NULL;ptr=ptr->next){
                      if(strcmp(ptr->input,tmp)==0){
                        ptr->count=ptr->count+1;
                        s=0;
                      }
                    }
                    if(s)
                    add_to_list (tmp);
                  }
                }
                for(i=0;i<32;i++)
                   tmp[i]=' ';
                i=0;
        }
        }
        print_result();
        fclose(fpin);
}
在 C/C++ 中的邏輯判斷是用 && 表示 AND || 表示 OR 而 & 表示「AND 位元運算」 | 表示「OR 位元運算」 此外,你的判斷式中都沒有包含到 'a' 'z' 'A' 'Z' (c>'a' & c<'z') | (c>'A' & c<'z') 應改成 (c>='a' && c<='z') || (c>='A' && c<='z') 試試看吧 沒空更新的網頁...
C及指標教學,計算機概論,資訊管理導論... http://coolsite.to/dllee 介紹Shells,LiteStep,GeoShell.... http://coolsite.to/ushells
------
http://www.ViewMove.com
happyzeus
一般會員


發表:1
回覆:3
積分:0
註冊:2002-10-20

發送簡訊給我
#6 引用回覆 回覆 發表時間:2002-10-20 14:52:21 IP:211.74.xxx.xxx 未訂閱
感謝你的提醒~~我把那部份改過了 但是無法讀完整份文件檔的問題還是沒解決^^|
ubong
一般會員


發表:2
回覆:42
積分:23
註冊:2002-10-02

發送簡訊給我
#7 引用回覆 回覆 發表時間:2002-10-22 01:24:47 IP:61.216.xxx.xxx 未訂閱
#include  #include #include #include #include typedef struct wordlist{ char input[32]; int count; struct wordlist *next; }node; node *head=NULL; node *new_node; node *ptr; void add_to_list (char tmp[]){ new_node=(node *)malloc(sizeof(node)); if(new_node==NULL){ printf("memory alloc error\n"); exit(1); } strcpy(new_node->input,tmp); new_node->count=1; new_node->next=NULL; ptr->next=new_node; } void print_result(){ int k=0; for(ptr=head;ptr->next!=NULL;ptr=ptr->next){ printf("= %s\n",ptr->count,ptr->input); k ; if(k && !(k )) getch(); } } void main() // 檔案由設計時指定,用此方法,不必用,argc,argv[] { FILE *fpin; char c,tmp[32]=" "; clrscr(); int i=0,s=1; fpin = fopen("input.txt", "r"); if(!fpin){ printf("The file: %s is not found!\n", "input.txt"); exit(1); } while((c=fgetc(fpin))!=EOF){ if(isalpha(c)) tmp[i ]=c; else{ tmp[i]='\0'; if(!tmp[0]) continue; i=0; s=1; if(head==NULL){ head=(node *)malloc(sizeof(node)); if(head==NULL){ printf("memory alloc error\n"); exit(1); } strcpy(head->input,tmp); head->count=1; head->next=NULL; } else{ for(ptr=head;ptr->next!=NULL;ptr=ptr->next){ if(strcmp(ptr->input,tmp)==0){ ptr->count=ptr->count 1; s=0; } } if(s) add_to_list (tmp); } tmp[i]='\0'; } } print_result(); fclose(fpin); } //照你的程式,如果記憶體不足時會出現提示,如上程式碼編輯後執行 並不會在10幾行就停止。只是有的字串會重複出現。你自己再查看那兒 有誤。(我用Turbo C ). ubong
------
ubong
Doulos
一般會員


發表:0
回覆:1
積分:0
註冊:2002-10-30

發送簡訊給我
#8 引用回覆 回覆 發表時間:2002-11-06 13:42:39 IP:211.23.xxx.xxx 未訂閱
釋放malloc的記憶體,不是用free() function嗎? 你是在問這個嗎? 如果是automatic variables當你執行完程式,會主動釋放,但是如果是用malloc() function 動態要的記憶體,是要用free()釋放的,不然會產生memory leak問題。
系統時間:2024-03-29 22:01:03
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!