클래스 템플릿에서 타입인수로 메모리 동적할당

dltkddyd의 이미지

template <typename targ> DynamicArr<targ>::DynamicArr(size_t cnt) {
	targ trial='e';//test
	if(typeof(trial)==S_CHAR) {printf(".....  charrrr....%u\n",cnt);}//test
	(this->elements)=new targ[cnt];//trouble
	printf("In DynamicArr(size_t cnt)\n");//test
	this->length=cnt;
}

위의 코드 컴파일은 제대로 되는데, 실행시 계속 디버거가 실행됩니다. 여기서 DynamicArr은 클래스 템플릿입니다.

template DynamicArr {
.....
} ;

그리고 이 클래스 템플릿 안에 제일 위에 언급한 생성자가 포함되어 있습니다. 그런데

DynamicArr tmp((size_t)3);

이렇게 해서 객체를 만들면 위의 생성자가 호출되는데, 계속

(this->elements)=new targ[cnt];//trouble

코드 부분에서 디버거가 작동되면서 실행이 중지됩니다. 타입인수에 어떤 문제가 있는 건가요? 타입인수로 동적 메모리 할당이 되게끔 하는 방법좀 알려주세요.

익명 사용자의 이미지

가변길이 배열은 c99에서는 지원하는데 c++에서는 지원하지 않아요.

dltkddyd의 이미지

말씀하신 가변길이 배열이라는 것이 아래와 같은 메모리 동적할당이 아닌가요?

char* chs=new char[30];

그렇다면, 되어야 하지 않나요? C++에서 되던데요?

본인 맞습니다.
인증샷
우헤헤헤... 로 대신합니다.

dltkddyd의 이미지

int n=30;
char* chs=new char[n];

이런 식으로는 되는데, 가변인수에서 가변길이 선언이 될 때도 있고 안 될 때도 있더군요? 가변길이라서 그런 것은 아닌 것 같은데요.. 이거 정말 헷갈리네요. 그리고 c99라는 것이 C 표준 이전의 C라고 검색되네요. 이런 식의 동적배열은 최신 참고서적에서도 많이 언급되던데요..

본인 맞습니다.
인증샷
우헤헤헤... 로 대신합니다.

어허야의 이미지

char* chs=new char[n]; 는 올바른 사용법입니다.
런타임에서 문제가 생긴다면 코드의 다른 부분까지도 살펴봐야 할거 같네요.

사족을 달자면, c99는 1999년에 나온 C표준을 말하는것이고, 가변길이 배열이라 함은 char* chs=new char[n];가 아니라, char chs[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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.