[질문]이차원 동적생성 문제

zhangyuan의 이미지

#include "stdio.h"

int main(int argc, char* argv[])
{
	char source[]={'a','b','c'};
	char **dest;

	dest=new char*[10];
	*dest=new char[5];

	for(int i=0;i<10;i++)
		for(int k=0;k<5;k++){
			dest[i][k]='\0';
		}

	dest[0][0]=source[0];
	dest[0][1]='\0';
	dest[1][0]=source[0];
	dest[1][1]='\0';
	dest[2][0]=source[0];
	dest[2][1]='\0';


	printf("%s %s %s\n",dest[0],dest[1],dest[2]);


	return 0;
}

위와같이 일반 char 형을 string형태로 고치기 위해서 사용을 했는데
메모리참조 에러가 발생하네요..
어디가 잘못된것인지 잘 모르겠습니다..
도움부탁드립니다.

xx[0] , xx[1] 이런식으로 안하고 memcpy나 strcpy를 사용했는데도
마찬가지던데....

mykldp의 이미지

   char source[]={'a','b','c'}; 
   char **dest; 

   dest=new char*[10]; 
/************************/
   for(int i = 0; i < 10; ++i)
   {	dest[i] = new char[5]; 
   }
/************************/

   for(int i=0;i<10;i++) 
      for(int k=0;k<5;k++){ 
         dest[i][k]='\0'; 
      } 

   dest[0][0]=source[0]; 
   dest[0][1]='\0'; 
   dest[1][0]=source[1]; 
   dest[1][1]='\0'; 
   dest[2][0]=source[2]; 
   dest[2][1]='\0'; 
zhangyuan의 이미지

아...맞다..

그랬던거군요...

넘 어이없이 시간만 엄청낭비했습니다..ㅠ.ㅠ

감솨감솨또감솨~!

삶의 이탈을 꿈꾸며..

winner의 이미지

다른 이야기입니다만 이런 code 를 많이 보게 되면서 항상 생각되는 것이 memory 동적할당하고, 해제하는게 영 불편하다는 거죠.

위의 code 에서 2차원을 행렬로 볼때 각각의 행에 정해진 열은 5이므로 차라리

char (*dest)[5] = new char[10][5];

이 차라리 낫다고 생각되네요.

어?...!!!

그런데 이경우 memory 해제를 어떻게 하죠?

C 에서는 그냥 free(dest) 하면 그만인데....
C++ 에서는 delete 인가요? 아니면 delete[] 인가요?

delete[][] 은 안되는군요...

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.