c++ 소멸자 사용에서 잘못된 부분을 짚어주세요.

cucumbers816의 이미지

#include <iostream>
#include <fstream>
 
class KSS{
private:
	int *class1;
	int *class2;
	int *class3;
	int *class4;
public:
	KSS(std::ifstream &inStream, int k, int n);
	~KSS();
 
};
 
int main(){
	std::ifstream inStream;
	inStream.open("input.txt");
	if(inStream.fail())
	{
		std::cerr<<"input file opening failed\n";
		exit(1);
	}
 
	int testCasesNum;
	inStream >> testCasesNum;
 
	int k , n;
 
	for(int i = 0 ; i < testCasesNum ; i++){
		inStream >> k >> n ;
		KSS kss(inStream, k, n);
                kss.~KSS();
	}
 
 
	return 0;
}
 
KSS::KSS(std::ifstream &inStream, int k, int n){
	class1 = new int[sizeof(int)*n];
	class2 = new int[sizeof(int)*n];
	class3 = new int[sizeof(int)*n];
	class4 = new int[sizeof(int)*n];
 
	for(int i = 0 ; i < n ; i++){
		inStream >> class1[i];
		std::cout<<class1[i]<<std::endl;
	}
	for(int i = 0 ; i < n ; i++){
		inStream >> class2[i];
		std::cout<<class2[i]<<std::endl;
	}
	for(int i = 0 ; i < n ; i++){
		inStream >> class3[i];
                std::cout<<class3[i]<<std::endl;
        }
	for(int i = 0 ; i < n ; i++){
		inStream >> class4[i];
		std::cout<<class4[i]<<std::endl;
	}
 
}
 
KSS::~KSS(){
	delete[] class1;
	delete[] class2;
	delete[] class3;
	delete[] class4;
}

for(int i = 0 ; i < testCasesNum ; i++){
inStream >> k >> n ;
KSS kss(inStream, k, n);
kss.~KSS();
}

첫번째 테스트 케이스가 끝나고 두번째 테스트 케이스로 넘어가지 않네요. 제가 무엇을 잘못 한 걸까요?

yukariko의 이미지

kss 클래스 변수가 for문안에서 선언되기때문에
반복이 끝날때마다 자동으로 소멸자를 호출합니다.

그런데 kss.~KSS() 를 통해 소멸자를 명시적호출하게 되면,
클래스가 소멸되는것이 아닌, 그냥 소멸자 함수안의 내용을 수행하게 됩니다.
따라서 소멸자가 두번호출되는것이죠.

즉, 반복이 끝날때마다 소멸자를 두번 호출하기때문에
이미 해제가 끝난 변수를 또 해제하는것에서 오류가 발생하는듯 싶습니다.

그냥 소멸자 함수 호출부분을 삭제하면 될듯싶네요.

cucumbers816의 이미지

여쭤봤던 것 말고도 잘못된 부분이 있었는지도 몰랐는데.. 감사합니다 ㅠㅠ

jick의 이미지

new int[sizeof(int) * n]이라고 하면 정수 n개가 아니라 정수 (sizeof(int) * n) 개, 다시 말하면 (sizeof(int) * sizeof(int) * n) 바이트의 공간을 할당하게 됩니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.