2016-08-28 2 views
-1

CRT가 응용 프로그램이 힙 버퍼의 끝 뒤에 메모리에 쓴 것을 감지했습니다.힙 손상 감지 : 정상 차단 (# 153) 후

그러나 나는 어디에서 힙을 쓰고 있는지 잘 모르겠습니다. delete [] 데이터를 호출하자마자 오류가 발생합니다.

데이터는 두 배의 배열에 대한 포인터입니다. 불필요한 코드를 제거하려고했습니다.

소스 코드 :

#include "sequence2.h" 
using namespace main_savitch_4; 

// CONSTRUCTORS and DESTRUCTORS 

// The default sequence constructor 
// Purpose: To create an instance of a sequence 
// Parameters: (int) initial_capacity 
// Returns: None 
sequence::sequence(int initial_capacity) 
{ 
    // Allocate memory for the new sequence 
    data = new value_type[capacity]; 
    // Set the capacity to the passed in capacity 
    capacity = initial_capacity; 
    // Set used to 0 because there are currently no objects in the sequence 
    used = 0; 
    // Set the current index to the 0 for the first item 
    current_index = 0; 
} 

// The sequence copy constructor 
// Purpose: To create a deep copy of a sequence 
// Parameters: A source sequence 
// Returns: None 
sequence::sequence(const sequence& source) 
{ 
    // Allocate memory to the new sequence using the capacity of the passed sequence 
    data = new value_type[source.capacity]; 
    // Set the capacity to the passed capacity 
    capacity = source.capacity; 
    // Set used to the number of used indexes in the previous sequence 
    used = source.used; 
    // Set the current_index to the index of the passed sequence 
    current_index = source.current_index; 

    // Copy all data from the passed sequence into the new sequence 
    for (int i = 0; i < used; i++) 
    { 
     data[i] = source.data[i]; 
    } 
} 

// The default sequence destructor 
// Purpose: To de-allocate memory for a sequence 
// Parameters: None 
// Returns: None 
sequence::~sequence() 
{ 
    // De-allocate dynamic memory 
    delete[] data; 
} 

// MODIFICATION MEMBER FUNCTIONS 

// The overload assignment operator function 
// Purpose: To assign a sequence with the data from another sequence 
// Parameters: A source sequence 
// Returns: None 
void sequence::operator=(const sequence& source) 
{ 
    value_type *new_data; 

    // Check for possible self-assignment 
    if (this == &source) 
     return; 

    // If the capacity of the source is not equal to the original 
    if (capacity != source.capacity) 
    { 
     // Create a new sequence using the source capacity 
     new_data = new value_type[source.capacity]; 
     // Delete the old sequence 
     delete[] data; 
     // Assign data pointer to the new sequence 
     data = new_data; 
     // Set capacity to the new source capacity 
     capacity = source.capacity; 
    } 

    // Set used equal to the source "used" 
    used = source.used; 

    // Copy data from the source sequence into the new sequence 
    for (int i = 0; i < used; i++) 
    { 
     data[i] = source.data[i]; 
    } 
} 

// The resize function 
// Purpose: To grow a sequence when necessary 
// Parameters: (int) new_capacity 
// Returns: None 
void sequence::resize(int new_capacity) 
{ 
    // Declare a larger array 
    value_type *larger_array; 

    if (new_capacity == capacity) 
     return; // The allocated memory is already the right size 

    if (new_capacity < used) 
     new_capacity = used; // Can't allocate less than we are using 

    // Dynamically allocate memory for the larger array with the new capacity 
    larger_array = new value_type[new_capacity]; 

    // Copy the data from the old array to the larger array 
    for (int i = 0; i < used; i++) 
    { 
     larger_array[i] = data[i]; 
    } 
    // Delete the old array 
    delete[] data; 
    // Set the pointer to the larger array 
    data = larger_array; 
    // Set the capacity to the new capacity 
    capacity = new_capacity; 
} 

// The insert function 
// Purpose: To insert an item before the current index 
// Parameters: (const value_type&) entry 
// Returns: None 
void sequence::insert(const value_type& entry) 
{ 
    // If used and capacity are the same, resize the sequence 
    if (used == capacity) 
     resize(used + 1); 

    // If the current index is an item... 
    if (is_item()) 
    { 
     // Block of code that moves objects ahead of insert forward 
     for (int i = used; i > current_index; i--) 
     { 
      data[i] = data[i-1]; 
     } 
    } 

    // Set the current index to the new entry 
    data[current_index] = entry; 
    // Increment used because we've added an item 
    ++used; 
} 

// The attach function 
// Purpose: To attach an item after the current index 
// Parameters: (const value_type&) entry 
// Returns: None 
void sequence::attach(const value_type& entry) 
{ 
    // If used and capacity are the same, resize the sequence 
    if (used == capacity) 
     resize(used + 1); 

    // If the current index is an item... 
    if (is_item()) 
    { 
     // Block of code that moves objects ahead of attach forward 
     for (int i = used; i > current_index; i--) 
     { 
      data[i] = data[i-1]; 
     } 

     // Set the current index to the recently attached item 
     ++current_index; 
     // Store the entry in the new current index 
     data[current_index] = entry; 
    } 
    else 
    { 
     // Set the current index to the end of the sequence 
     current_index = used; 
     // Put that entry into that new index 
     data[current_index] = entry; 
    } 

    // Increment used because we've added an item 
    ++used; 
} 

// CONSTANT MEMBER FUNCTIONS 

// The is_item function 
// Purpose: To check if the current index contains an item 
// Parameters: None 
// Returns: None 
bool sequence::is_item() const 
{ 
    // If the current index is greater than used, or used is equal to 0 
    // there is no current item 
    if (current_index >= used || used == 0) 
     return false; 
    else // otherwise there is a current item 
     return true; 
} 
+0

입니다 . 문제가 될 수 있습니까? –

+0

필자는 삽입 및 삽입 기능에서 동적 순서에 데이터를 쓸 때 문제가 있다고 믿는다는 것을 분명히해야합니다. –

+0

또한 문제를 일으키는 가장 짧고 간단한 예제를 작성한 다음 디버거를 사용하여 코드를 한 행씩 단계별로 실행하여 범위를 벗어나지 않도록하십시오. –

답변

0

LOL, 여기 당신이`current_index` 멤버 변수를 복사하지 않는 복사 할당 연산자의 솔루션

sequence::sequence(int initial_capacity) 
{ 
    // Allocate memory for the new sequence 
    data = new value_type[**initial_capacity**]; 
    // Set the capacity to the passed in capacity 
    capacity = initial_capacity; 
    // Set used to 0 because there are currently no objects in the sequence 
    used = 0; 
    // Set the current index to the 0 for the first item 
    current_index = 0; 

} 우선 들어

+0

좋은 컴파일러는 아마도 추가 경고가 활성화되어있어 초기화되지 않은 변수를 언제 사용하는지 알려줄 수 있습니다. –