clion關於指針使用不當發生的錯誤 -var-create: unable to create variable object


在實驗課上,在做散列表的我又一次遇到了“無法創建變量對象的問題”。

報錯如下:

Process finished with exit code -1073741819 (0xC0000005)

下面是修改后的完整代碼,出現問題的地方我會用注釋來解釋。

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int H(int k,int n){
 5     return k%n;
 6 }
 7 int HashSearch1(int k, int *&p,int n,int ht[]){//此處使用了&p來引用主函數中的指針p
 8     int i, j;
 9     j = H(k,n);                                
10     i = j;                                    
11     if (ht[i] == k) {//查找成功
12         *p = i;
13         return 1;
14     }
15     if(ht[i]==0){//插入成功
16         ht[i] = k;
17         *p = i;
18         return 0;
19     }
20 
21     i = (i + 1) % n;//向后探
22     while(i!=j){
23         if(ht[i]==k){//查找成功
24             *p = i;
25             return 1;
26         }
27         if(ht[i]==0){//插入成功
28             ht[i] = k;
29             *p = i;
30             return 0;
31         }else{
32             i = (i + 1) % n;
33         }
34     }
35     return -1;//表滿
36 }
37 
38 int main(){
39     int att,data,now=0;
40     int ht[100]={0},n=0;
41     int *p=&now;//又是你呀,指針大哥。由於之前 int *p;是野指針並沒有用變量來初始化,會導致它指向莫名其妙的地方,從而使程序崩潰。
42     cout<<"輸入閉散列表的大小"<<endl;
43     cin>>n;
44     cout<<"輸入查找值"<<endl;
45     cin>>data;
46     while(data!=0){
47         att=HashSearch1(data,p,n,ht);
48         if(att==1){
49             cout<<"查找成功"<<endl;
50         }
51         if(att==0){
52             cout<<"查找失敗,插入查找值"<<endl;
53         }
54         if(att==-1){
55             cout<<"溢出"<<endl;
56         }
57         cin>>data;
58     }
59 
60     return 0;
61 }

類似的見我的另一篇博客(2條消息) 關於函數傳參的再理解(含引用)_secant007的博客-CSDN博客

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM