[問題] 程式記憶體區段錯誤
看板C_and_CPP (C/C++)作者fjf1980 (聽說 侯佩岑是豬頭)時間15年前 (2011/04/18 10:49)推噓1(1推 0噓 2→)留言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
04/18 10:54, 1F
※ 編輯: fjf1980 來自: 219.84.178.206 (04/18 11:02)
→
04/18 11:05, , 2F
04/18 11:05, 2F
→
04/18 11:45, , 3F
04/18 11:45, 3F
討論串 (同標題文章)
完整討論串 (本文為第 1 之 2 篇):
1
3
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章