c++ 구조체 char* 메모리 관련

jyh305의 이미지

현재 C++ 을 이용하여 dll을 만들고 있으며 c/c++ 예제 코드에서 동작해야합니다.

이전까지는 문제가 없었는데 구조체 문자열 관련해서 코멘트가 와서 어느 부분이 문제인지 고민중입니다.

기존에는

struct
{
   char* name
 
   생성자(const char* Name, ,)
   {
      this->name = new char[strlen(Name)+1]; 
      strcpy_s()/ strcpy()
   }
   복사생성자(const struct& copy)
   {
      this->name = new char[strlen(copy.name)+1]
      strcpy_s() / strcpy()
   }
   소멸자
   {
      if(name!=nullptr)
      {
          char* tmp = name;
          name = nullptr;
          delete[] tmp;
      }
   }
 
}

이런 형태로 작성되어있었습니다.

그런데 여기서 저 문자열 관련 코드에서 메모리 누수? 오버플로? 가 생각되는 오류가 있다고 합니다.
저도 전달 받은거라서 증상에 대해서 정확히 못들었습니다만,
(반복 작업 시에 자동적으로 delete 한다? 라고 들은 것 같습니다)

저런 구조체에서 char* 로 인해 메모리 관련 문제가 생길 수 있을까요?
예상 되어지는 오류가 있나요?

(참고 : c 예제코드에서 돌릴려고 하다보니 string 보다는 char* 을 사용했습니다.)

익명 사용자의 이미지

1. 코드에 검열관이 다녀갔나요? 여러 군데 지워져 있네요.

남에게 보이고 싶지 않은 게 있어서 지우는 거야 자유입니다만, 코드는 컴파일 가능한 형태로 올리는 게 좋습니다.

안 그러면 컴파일 가능한 형태의 답변을 받기 어렵거든요.

2. 답변은 짧게 요약하면, Copy assignment operator를 따로 만들어주지 않은 부분이 문제가 됩니다.

https://en.cppreference.com/w/cpp/language/copy_assignment

jyh305의 이미지

죄송합니다 코드가 불완전한건
퇴근하고 생각나는대로 적다보니 이렇게 되었습니다. 보충하겠습니다
copy assignment operator 가 복사생성자 아닌가요??

익명 사용자의 이미지

복사 생성자는 copy constructor고요.

https://en.cppreference.com/w/cpp/language/copy_constructor

그 둘을 잘 구분해 줘야 합니다. 대개의 경우 copy constructor와 copy assignment operator 중 하나를 explicit하게 구현할 필요가 있는 경우, 다른 하나도 구현해 줘야 할 필요가 있을 가능성이 높습니다.

(destructor와도 같은 관계가 있어서, 셋을 묶어서 "3의 법칙"이라고 부르기도 합니다. C++11부터는 두 개가 더 추가되어서 5의 법칙입니다.)

jyh305의 이미지

C++11 기준으로 작성중인데 5의 법칙까지 만족시켜야 하는군요
정말 감사합니다!

댓글 달기

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