메모리 에러

pool007의 이미지

메모리 에러 좀 잡아주세요.

#include <iostream>
#include <vector>

using namespace std;

class correlation
{
  private:
    enum { NUM_ALPHABET = 20, K = 4 };
    typedef int (*t_cnt_alphabet)[NUM_ALPHABET];
    vector<t_cnt_alphabet> cnts;

    void init_cnt_alphabet();

  public:
    correlation();
    void operator() (vector<unsigned char> kmer, int, int);
    friend ostream& operator<< (ostream& os, correlation &c);
    ~correlation();
};

correlation::correlation() 
{
}

void correlation::init_cnt_alphabet()
{
  cerr << __PRETTY_FUNCTION__ << ": begin" << endl;

  try 
    {
      t_cnt_alphabet tmp = new int[K][NUM_ALPHABET];
      cnts.push_back(tmp);
    }

  catch(std::bad_alloc &a)
    {
      cerr << "Not enough memory" << endl;
      exit(EXIT_FAILURE);
    }


  cerr << __PRETTY_FUNCTION__ << ": new int[][]" << flush << endl;

  for (int k = 0 ; k < K ; k++)
    for (int i = 0 ; i < NUM_ALPHABET ; i++)
      {
        t_cnt_alphabet current = cnts.back();
        current[k][i] = 0;
      }

  cerr << __PRETTY_FUNCTION__<< ": end " << flush << endl;
}

correlation::~correlation()
{
  for (vector<t_cnt_alphabet>::iterator i = cnts.begin(); i != cnts.end(); i++)
  {
    t_cnt_alphabet current = *i;
    delete []current;
  }
}

void correlation::operator() (vector<unsigned char>kmer, int pos, int score)
{
  static int loc = -1;

  if (loc != pos)
    {
      cerr << __PRETTY_FUNCTION__ << ": new pos=" << pos << endl;
      loc = pos;

      init_cnt_alphabet();
      cerr << __PRETTY_FUNCTION__ << ": cnt_alphabet initialized." << endl;
    }

  t_cnt_alphabet current = cnts.back();
  
  current[0][kmer[0]]++;
  current[1][kmer[1]]++;
  current[2][kmer[2]]++;
  current[3][kmer[3]]++;
}

ostream& operator<< (ostream& os, correlation &c)
{
  for (vector<correlation::t_cnt_alphabet>::iterator i = c.cnts.begin()
       ; i != c.cnts.end()
       ; i++)
    {
      correlation::t_cnt_alphabet current = *i;
      for (int k = 0 ; k < 4 ; k++)
        {
          for (int i = 0 ; i < 20 ; i++)
            {
              os << current[k][i];
              os << " ";
            }
          os << endl;
        }
    }
  return os;
}

실행결과는 다음과 같습니다.

void correlation::operator() (vector<unsigned char, allocator<unsigned char> >, int, int): new pos=0
void correlation::init_cnt_alphabet (): begin
void correlation::init_cnt_alphabet (): new int[][]
void correlation::init_cnt_alphabet (): end
void correlation::operator() (vector<unsigned char, allocator<unsigned char> >, int, int): cnt_alphabet initialized.
void correlation::operator() (vector<unsigned char, allocator<unsigned char> >, int, int): new pos=1
void correlation::init_cnt_alphabet (): begin
Segmentation fault

외부에서 correlation 에 대한 참조를 갖고 있다가 operator() 를 호출하면 correlation에서는 각 문자 발생회수를 카운트 하는 것이 주요 흐름입니다.

그리고 메시지를 보면 아시겠지만, fault 가 나는 곳이

t_cnt_alphabet tmp = new int[K][NUM_ALPHABET];

입니다.

제 궁금증은 왜 int[][] 가 처음에는 성공하고
두번째는 실패하는가 하는 것입니다.

뭘 잘못한건가요..
하도 버그 잡으려고 이리저리 고치다보니 코드가 지저분합니다. 그점 죄송하구요..

p.s. __PRETTY_FUNCTION__과 유사한 출력을 가지는 C99의 표준 매크로는 없는건가요? __func__는 __FUNCTION__ 처럼만 나오던데요.

익명 사용자의 이미지

궁금한게 있습니다. 답변 아니고요. 그냥 C를 공부하는 사람인데요.
typedef int (*t_cnt_alphabet)[NUM_ALPHABET];이게 무슨 뜻인가요?
바쁘실텐데 죄송하네요. :oops:

pool007의 이미지

죄송합니다.. 문제를 해결했습니다.
전혀 다른쪽의 에러였습니다.
초보라서 괜히 엉뚱한데서 헤멨어요.

위에 손님님에 대한 답변으로요,
typedef를 말씀하시는건지 아니면 (*)[] 에 대해 물어보신건지
잘 모르겠지만,
t_cnt_alphabet이라고 하면 그것이 [NUM_ALPHABET]크기를 갖는
배열에 대한 포인터임을 선언한 것입니다.

--
Passion is like genius; a miracle.

댓글 달기

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