메모리 에러
글쓴이: pool007 / 작성시간: 수, 2004/12/22 - 9:28오후
메모리 에러 좀 잡아주세요.
#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__ 처럼만 나오던데요.
Forums:
궁금한게 있습니다. 답변 아니고요. 그냥 C를 공부하는 사람인데요.
궁금한게 있습니다. 답변 아니고요. 그냥 C를 공부하는 사람인데요.
typedef int (*t_cnt_alphabet)[NUM_ALPHABET];
이게 무슨 뜻인가요?바쁘실텐데 죄송하네요. :oops:
죄송합니다.. 문제를 해결했습니다.전혀 다른쪽의 에러였습니다.초보
죄송합니다.. 문제를 해결했습니다.
전혀 다른쪽의 에러였습니다.
초보라서 괜히 엉뚱한데서 헤멨어요.
위에 손님님에 대한 답변으로요,
typedef를 말씀하시는건지 아니면 (*)[] 에 대해 물어보신건지
잘 모르겠지만,
t_cnt_alphabet이라고 하면 그것이 [NUM_ALPHABET]크기를 갖는
배열에 대한 포인터임을 선언한 것입니다.
--
Passion is like genius; a miracle.
댓글 달기