c++ 간단한 질문입니다.

cjy1126의 이미지

c++ 책에 나오는 간단한 예제입니다.

밑의 소스의 생성자에서

p = new char[l];

이렇게 코딩하면 메모리에 어떤식으로 복사가되나요?

책에는 이렇게 나왔는데... strcpy함수에서 '\n'을 만날때까지 계속 복사하는건가요?

감이 잘안오네요.

#include <iostream>

using namespace std;

class strtype{
	char *p;
public:
	strtype(char *s);
	strtype(const strtype &a);
	~strtype() { delete []p; }
	char *get() { return p; }
};

strtype::strtype(char *s)
{
	int l;

	l = strlen(s);

	p = new char[l+1];
	if(!p){
		cout << "Allocation error\n";
		exit(1);
	}

	strcpy(p, s);

	cout << p[l-1] << endl;
}

strtype::strtype(const strtype &a)
{
	int l;

	l = strlen(a.p);

	p = new char[l+1];

	if(!p){
		cout << "Allocation error\n";
		exit(1);
	}

	strcpy(p, a.p);
}

void show(strtype x)
{
	char *s;

	s = x.get();
	cout << s << endl;
}

int main()
{
	strtype a("Hello"), b("There");

	show(a);
	show(b);

	getchar();
	return 0;
}
bugiii의 이미지

질문하신 내용은 다른 분들이 답글을 달아주시리라 생각하구요.

참고하시는 책이 아주 오래 전의 것 같습니다. 최신의 C++ 표준에 가까운 책을 구하시기 바랍니다.

코드 자체에 메모리 유출과 구형 C++ 동작을 하는 new 그리고 복사 생성자에서 지켜야할 사항이 빠져있고 이전 핸들값의 해제도 없고, 내부 핸들을 그대로 외부에 노출시키는 문제도 있습니다. 또, 복사 생성자가 있다면 대입연산자도 꼭 필요할 것입니다.

그럼, 이만...

mrchu의 이미지

'\n'이 아니고 '\0'아닌가요?
그리고, 그렇게 코딩 하신다면, '\0'이 들어갈 자리가 없겠군요.

cjy1126의 이미지

그렇게 알고있거든요.

winner의 이미지

줄단위 처리는 입출력 중 gets, fgets, puts, fputs 뿐입니다.

위 네개 함수에 대해서는 확인해 보시고 사용하세요.
file 처리를 하느냐와 표준입출력을 사용하느냐에 따라 '\n' 과 '\0' 에 미묘한 차이가 있으니까요.

댓글 달기

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