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

Link List 釋放記憶體的問題

答題得分者是:syntax
istillloving
高階會員


發表:33
回覆:182
積分:183
註冊:2008-10-09

發送簡訊給我
#1 引用回覆 回覆 發表時間:2010-02-20 11:51:52 IP:140.127.xxx.xxx 訂閱
各位大大好:

這是我最近常遇到的問題
到底要用第一種的還是第二種的寫法呢?
我的程式用第二種的常常會發生記憶體讀取錯誤的問題呢




[code cpp]
// 1
while(first2 != NULL)
{
temp2=(*first2).next;
delete first2;
first2=temp2;
}
delete first2,op2,temp2;

[/code]



[code cpp]
// 2
while(first2 != NULL)
{
temp2=(*first2).next;
delete [] first2;
first2=temp2;
}
delete first2,op2,temp2;

[/code]



我的流程是這樣的

我有建一個class 名稱LPR


[code cpp]
class LPR{

void Merge (int *Nthreshold,double *Nbuffg)
{
// 上述的兩種釋放Link List 寫在這裡面
// 建立Link List的方式如下
/**** 區塊紀錄 ****/
Data *first2,*temp2,*op2,*Last2;
first2=temp2=op2=Last2= new Data;

(*temp2).Range = 0;
(*temp2).RangeW = 0;
(*temp2).RangeH = 0;
(*temp2).total = 0;
(*temp2).RangeL = 0;
(*temp2).RangeR = 0;
(*temp2).RangeT = 0;
(*temp2).RangeD = 0;
(*temp2).number = 0;

(*temp2).next=NULL;
(*temp2).last=NULL;


for(i=nthreshold[fleg*4];i {
for(j=nthreshold[fleg*4 1];j {
if( (i>=0)&&(i=0)&&(j {

MAX=0;
if(LPRTEMP[i w*j]!=0)
{
while(temp2 != NULL)
{
if(LPRTEMP[i w*j]!=(*temp2).number)
{
temp2 = (*temp2).next ;
MAX = LPRTEMP[i w*j];
}
else if(LPRTEMP[i w*j]==(*temp2).number)
{
MAX=0;
temp2 = NULL;
}
}//end-while
if( (MAX!=0)&&(op2!=first) ) // 正反向 link_list
{
(*op2).last = Last2 ;
(*op2).number = MAX ;
Last2 = op2;
op2 = (*op2).next = new Data;
(*op2).next = NULL ;
}
else if( (MAX!=0)&&(op2==first) )
{
(*op2).last=NULL;
(*op2).number = MAX;
op2 = (*op2).next = new Data;
(*op2).next = NULL ;
}

temp2=first2;
}//end-if

}
}
}
op2 = NULL ;
}

};

[/code]


Button裡面是這樣的寫法

LPR *run;
run = new LPR;
(*run).Merge(nthreshold,buffg);
delete [] run;

點擊Button多次後 就會發生錯誤 而且錯誤會指向 delete [] run;
到底是 (*run).Merge(nthreshold,buffg); 這行就出錯了
還是沒辦法刪除run呢

------
恩...
編輯記錄
istillloving 重新編輯於 2010-02-20 12:07:18, 註解 無‧
istillloving 重新編輯於 2010-02-20 12:07:56, 註解 無‧
syntax
尊榮會員


發表:26
回覆:1139
積分:1258
註冊:2002-04-23

發送簡訊給我
#2 引用回覆 回覆 發表時間:2010-02-21 09:46:35 IP:61.64.xxx.xxx 訂閱
最後一個 delete 內的 temp2 是多餘的
istillloving
高階會員


發表:33
回覆:182
積分:183
註冊:2008-10-09

發送簡訊給我
#3 引用回覆 回覆 發表時間:2010-02-21 10:32:48 IP:140.127.xxx.xxx 訂閱
您說的是這一行嗎

delete first2,op2,temp2;

===================引 用 syntax 文 章===================
最後一個 delete 內的 temp2 是多餘的
------
恩...
syntax
尊榮會員


發表:26
回覆:1139
積分:1258
註冊:2002-04-23

發送簡訊給我
#4 引用回覆 回覆 發表時間:2010-02-22 08:32:44 IP:59.125.xxx.xxx 訂閱
是,看起來是這樣 temp2 已經釋放過,不需要多做一次,但程式碼不完整,無法確定

===================引 用 istillloving 文 章===================
您說的是這一行嗎

deletefirst2,op2,temp2;

===================引 用 syntax 文 章===================
最後一個 delete 內的 temp2 是多餘的
istillloving
高階會員


發表:33
回覆:182
積分:183
註冊:2008-10-09

發送簡訊給我
#5 引用回覆 回覆 發表時間:2010-02-22 12:47:18 IP:140.127.xxx.xxx 訂閱

[code cpp]

temp2=first2=new Data;

int run=0;
while(run !=10)
{

temp2=(*temp2).next=new Data;

run ;
}

// 請問這兩種接地法 哪一個才能符合我釋放記憶體的寫法呢?
// 有時候程式在跑釋放的時候會有無窮回圈的情況
(*temp).next=NULL;
temp = NULL ;


while(first2 != NULL)
{
temp2=(*first2).next;
delete [] first2;
first2=temp2;
}

[/code]

再請問一下


[code cpp]
//這種寫法
I3temp=I3first;
while(I3first != NULL)
{
I3temp=(*I3first).next;
delete [] I3first;
I3first=I3temp;
}
//和這種有何不同
for(
I3temp=I3first;I3first != NULL;I3temp=(*I3first).next)
{
delete [] I3first;
I3first=I3temp;

}

[/code]

因為用for寫的在我的程式會出錯


------
恩...
編輯記錄
istillloving 重新編輯於 2010-02-22 12:48:31, 註解 無‧
istillloving 重新編輯於 2010-02-22 13:38:06, 註解 無‧
istillloving 重新編輯於 2010-02-22 14:11:30, 註解 無‧
istillloving 重新編輯於 2010-02-22 14:11:59, 註解 無‧
syntax
尊榮會員


發表:26
回覆:1139
積分:1258
註冊:2002-04-23

發送簡訊給我
#6 引用回覆 回覆 發表時間:2010-02-26 08:55:28 IP:59.125.xxx.xxx 訂閱
(*first2).next <<---- 這是一種很奇怪的用法
妳的指標觀念,可能要加強
通常,都會用 first2->next
使用通用的方式,可以避過一些可能看不見的盲點
當然 (*first2).next 也是可以用的,但是,這樣寫的意義與 first2->next 有所不同

delete object :「單一」釋放物件
delete [] object : 釋放整個物件陣列
for(;I3first!= NULL;I3temp=I3first->next)
{
delete I3first;
I3first=I3temp;

}

===================引 用 istillloving 文 章===================

[code cpp]

temp2=first2=new Data;

int run=0;
while(run !=10)
{

temp2=(*temp2).next=new Data;

run ;
}

// 請問這兩種接地法 哪一個才能符合我釋放記憶體的寫法呢?
// 有時候程式在跑釋放的時候會有無窮回圈的情況
(*temp).next=NULL;
temp = NULL ;


while(first2 != NULL)
{
temp2=(*first2).next;
delete [] first2;
first2=temp2;
}

[/code]

再請問一下


[code cpp]
//這種寫法
I3temp=I3first;
while(I3first != NULL)
{
I3temp=(*I3first).next;
delete [] I3first;
I3first=I3temp;
}
//和這種有何不同
for(
I3temp=I3first;I3first != NULL;I3temp=(*I3first).next)
{
delete [] I3first;
I3first=I3temp;

}

[/code]

因為用for寫的在我的程式會出錯


istillloving
高階會員


發表:33
回覆:182
積分:183
註冊:2008-10-09

發送簡訊給我
#7 引用回覆 回覆 發表時間:2010-02-26 14:29:23 IP:140.127.xxx.xxx 訂閱
您好:

請問一下(*first2).next 和 first2->next

這兩種寫法 切入點的差異在哪?

因為在學校被老師教導說這兩種都是一樣


------
恩...
系統時間:2024-04-19 21:31:49
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!