[問題] 程式記憶體區段錯誤

看板C_and_CPP (C/C++)作者 (聽說 侯佩岑是豬頭)時間15年前 (2011/04/18 10:49), 編輯推噓1(102)
留言3則, 3人參與, 最新討論串1/2 (看更多)
下面程式跑了以後會有 程式記憶體區段錯誤 請問我這是哪裡出錯了呢,謝謝 #include <iostream> #include <stdio.h> #include <string.h> using namespace std; class Caaa { private: int total, *ptr; public: Caaa(int num):total(num) { ptr = new int[num]; for(int i=0;i<num;i++) ptr[i]=i; } void show() { for(int j=0;j<this->total;j++) cout << this->ptr[j] <<" " ; cout << endl; } Caaa(const Caaa &c) { int *ptr1 = new int[c.total]; for(int k=0;k<c.total;k++) ptr1[k]=c.ptr[k]; } ~Caaa() { delete [] ptr;} }; int main() { Caaa a(10); Caaa b(a); a.show(); b.show(); } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 219.84.178.206

04/18 10:54, , 1F
ptr = new int[c.total];
04/18 10:54, 1F
※ 編輯: fjf1980 來自: 219.84.178.206 (04/18 11:02)

04/18 11:05, , 2F
照1F修改後 還是會 為什麼
04/18 11:05, 2F

04/18 11:45, , 3F
Caaa(const Caaa &c) 的時候total 沒有跟著改
04/18 11:45, 3F
文章代碼(AID): #1DgwQ-2_ (C_and_CPP)
文章代碼(AID): #1DgwQ-2_ (C_and_CPP)