c++ string 관련 질문입니다.

p952973의 이미지

힙을 구현하는 도중 메모리 오류가 발생해 문의 드립니다.

Push만 1000회 하고 난 후 다른 연산을 수행해보려 하는데 다음과 같이 반복해 삽입을 하는 도중 메모리가 죽습니다.

표준입력에서 한 라인을 읽어와 스트링에 담고, substr로 데이터만 담아오려는데

str.substr(2) 이걸 일정 횟수 반복하고 나면 메모리 폴트가 발생합니다.

스트링은 클래스라 동적할당을 따로 하는게 없는걸로 알고 있어서 계속 반복하는데 이상이 없어야 정상 아닌가요?

스트링을 사용 후 초기화시키는 것이 clear()함수로 알고 있어서 사용 후 clear()로 초기화를 하고 다시 사용하는데

에러가 발생합니다. 정상적으로 작동하다가 갑자기 같은걸 수행하려니 죽으니까 감을 잘 못잡겠습니다. c++이 이번에 배우는게 처음이라

기본적인 것을 모르는 것일수도 있는데 이 문제에 대한 답을 아신다면 알려주세요~~~!!ㅠㅠ

string str;
string temp;
 
while(1){							// until input is not exist
 
				getline(cin, str);			// receive data
				if(cin.eof())break;
				cout << "str : " << str << endl;
				cout << "str.size : " << str.size() << endl;
 
				if(str.length() > 1 ){
						if(str.at(0) == 'I'){
								cout << "in str.at(0) == 'I'" << endl;
								cout << "str.at(2) : " << str.at(2) << endl;
								cout << "str.substr(2) : " << str.substr(2) << endl;
								temp = str.substr(2);
								cout << "temp : " << temp << endl;
								buf = new char[temp.length() + 1];
								cout << "buf malloc successed " << endl;
								strcpy(buf, temp.c_str());
								cout << "strcpy successed" << endl;
								cout << "buf : " << buf << endl;
								heap->Push(atoi(buf));
								cout << "Push successed" << endl;
								cout << "done" << endl;
								str.clear();
								continue;
						}
				str.clear();				// init string
		}
익명 사용자의 이미지

new char 한 후에 할당한 크기만큼 값을 0으로 채우지 않아서 발생한 것으로 보입니다.
new char 아래에서 memset 으로 0을 채워보세요.

p952973의 이미지

그건 문제가 아닌것같습니다. 정확히 str.substr(2) 를 수행하는 순간 발생하거든요. 이걸 확인하기 위해서 buf를 만들어서 확인했던 것이라 정확한 문제는

string 클래스의 동적할당에 관련해 자세히 알아야 할 것 같습니다ㅠㅠ

klara의 이미지

입력이 세글자 이상인게 확실한가요?

p952973의 이미지

네 맞습니다. string에 "I 2039" 이런식의 데이터가 담기는것까지 확인하고

str.at(2)에 '2'가 나오는것까지 확인을 했는데 그 이후에 죽네요 ㅠㅠ

yukariko의 이미지

제 생각에는 heap에 데이터를 계속 추가하는 과정에서 메모리가 계속 쌓이다가

substr을 수행하는 과정에서 메모리 부족으로 프로그램이 중단된것 같습니다.

string 클래스의 동적할당하고는 관련없는 문제인것같네요.

p952973의 이미지

그말이 맞는것 같습니다. 데이터가 몇백개 입력되고 나서 죽으면 이해하겠는데 20개도 넣기 전에 죽어요... 그래서 제가 분명 실수한 부분이

있는것 같은데 못찾겠습니다... ㅠㅠ 이 while문을 메인에서 동작하게 하거든요. heap 클래스변수 하나 선언해 놓은 후에 거기에 접근해서 작업을

계속 수행하는데 이 방법이 잘못된건가요?

yukariko의 이미지

현재 메모리 상황이 어떻게 되는지,
정확하게 어떤 오류를 내고 정지되었는지,
이 소스와 관련된 다른 코드의 정보등
필요한 요소가 많아보입니다.

익명 사용자의 이미지

buf 를 delete 해줘야 할 것 같습니다.
heap->Push(atoi(buf));
cout << "Push successed" << endl;

-->

heap->Push(atoi(buf));
delete[] buf;
cout << "Push successed" << endl;

제 생각에는 string length 체크를 더 확실하게 2 보다 큰 경우로 하고 buf 를 없애고 atoi(str.substr(2).c_str()) 와 같이 하는게 좋아 보이네요...

twinwings의 이미지

string str;
string temp;
 
while(getline(cin, str)) // until input is not exist
{
	cout < < "str : " < < str < < endl;
	cout < < "str.size : " < < str.size() < < endl;
 
	if(str.at(0) == 'I'){
		/*
		 * some things...
		 */
 
 
		/* 이 부분 필요없습니다.
		 * str.clear();
		 * continue;
		 */
		} // if(str.at(0) == 'I')
 
	//str.clear();				// init string
} // while(getline(cin, str))

덫붙이자면, getline은 입력 패러미터 - istream -을 그대로 반환하며, istream은 bool type으로 implicit하게 type casting 됩니다.


그리고 heap 변수 타입이 안보이는데, heap의 변수타입-클래스-에서 버그 발생하는것 같은데요?

두 가지 단위 테스트 해보세요

1. 기존 코드와 동일하게 하고, heap 부분 주석 처리

//heap->Push(atoi(buf));

2.

int main()
{
    ITEMTYPE *heap = new ITEMTYPE;
    for(int i=0, i< 1000; i++)
        heap->Push(i)
 
    cout < < "test success\n";
 
    return 0;
}

제 생각에는 2번에서 죽을 것 같네요.

jick의 이미지

지금 현재 올려주신 코드는 컴파일이 되지 않습니다.
20줄 이내로 컴파일이 되고 동일한 문제점을 보이는 소스 코드를 만들어서 올려주세요.

댓글 달기

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