簡體   English   中英

不了解段錯誤

[英]Don't understand a seg fault

字符串是我自己的字符串類,而堆棧是我自己的堆棧類。 我正在嘗試使用以空格分隔的值將中綴更改為后綴。

下面的函數幾乎可以正常工作,但是還會返回“分段錯誤(核心已轉儲)”。

String postfix(String infix){
    std::vector<String> vec;
    vec = infix.split(' ');
    Stack<String> tmp;
    String right, left, op;

    int i = 0;
    while (vec[i] != ';'){
        if (vec[i] == ')'){
            right = tmp.pop();
            op = tmp.pop();
            left = tmp.pop();
            tmp.push(left + ' ' + right + ' ' + op);
        }else{
            if (vec[i] != '(' && vec[i] != ' ')
            tmp.push(vec[i]);
        }
        ++i;
    }

    String postfix = tmp.pop();
    return postfix;
}

我不明白為什么會這樣。 任何幫助,將不勝感激。

#ifndef STACK_HPP
#define STACK_HPP

template <typename T>
class Node{
public:
  T data;
  Node<T>* next;
  Node() {  data().next(0); } ;
  Node (const T& x) : data (x), next(0) {};
};


template <typename T>
class Stack{

public:
  Stack() : tos(0){};
  ~Stack();
  Stack(const Stack<T>&);
  void swap(Stack<T>& rhs);
  Stack<T>& operator= (Stack<T> rhs) { swap(rhs); return *this; };
  bool isEmpty() const { return tos == 0; };
  T pop();
  void push(const T&);

private:
  Node<T> *tos;
};

///////////////////////////////////////////////////////////////

template <typename T>
Stack<T>::~Stack(){
  while(tos != 0){
    Node<T> *tmp = tos;
    tos = tos -> next;
    delete tmp;
  }
}

template <typename T>
void Stack<T>::swap(Stack<T>& rhs){
  Node<T> *tmp = tos;
  tos = rhs.tos;
  rhs.tos = tmp;
}

template <typename T>
Stack<T>::Stack(const Stack<T>& actual){
  Node<T> *tmp = actual.tos, *bottom = 0;
  tos = 0;
  while (tmp != 0){
    if(tos == 0){
      tos = new Node<T>(tmp -> data);
      bottom = tos;
    }else{
      bottom -> next = new Node<T>(tmp -> data);
      bottom = bottom -> next;
    }
    tmp = tmp -> next;
  }
}

template<typename T>
T Stack<T>::pop(){
  T result = tos -> data;
  Node<T> *tmp = tos;
  tos = tos -> next;
  delete tmp;
  return result;
}

template <typename T>
void Stack<T>::push(const T& x){
  Node<T> *tmp = new Node<T> (x);
  tmp -> next = tos;
  tos = tmp;
}

#endif


//bruce pucci
//string
//cs23001
#include <iostream>
#include <cassert>
#include <vector>
#include <fstream>
#include "string.hpp"

///////////////////////////////string class friend functions////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////I/O/////////////////////////////////////////////
std::ostream& operator<<(std::ostream& out, const String& print){//std::cout operator
  int i = 0;
  while (print.arr[i] > 0){
    out << print.arr[i];
    ++i;
  }
  return out;
}
std::ifstream& operator>>(std::ifstream& in, String& rhs){//ifstream operator.
  char tmp[stringSize];//grabs word by word (chars serperated by whitespace).
  in >> tmp;
  rhs = String(tmp);
  return in;
}
////////////////////////////////string class public functions///////////////////////
////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////constructors///////////////////////////////////////
String::String(){//default constructor. default size
  arr = new char[stringSize];
  cap = stringSize;
  arr[0] = 0;
}
String::String(const char a){//char constructor. default size
  arr = new char[stringSize];
  cap = stringSize;
  arr[0] = a;
  arr[1] = 0;
}
String::String(const char a[]){//char array constructor. default size
  arr = new char[stringSize];
  cap = stringSize;
  int i = 0;
  while (a[i] > 0){
    arr[i] = a[i];
    ++i;
  }
  arr[i] = 0;
}
String::String(const int initSize, const char a[]){//char array constructor. size passed as a parameter
  arr = new char[initSize];
  cap = initSize;
  int i = 0;
  while (a[i] > 0){
    arr[i] = a[i];
    ++i;
  }
  arr[i] = 0;
}
String::String(int initSize){//like default constructor. size passed as parameter
  arr = new char[initSize];
  cap = initSize;
}
//////////////////////////////////dynamic stuff/////////////////////////////////////////////////
String::String(const String& rhs){//big three. copy constructor
  arr = new char[rhs.cap];
  cap = rhs.cap;
  for (int i = 0; i < cap; ++i)
    arr[i] = rhs.arr[i];
}
String::~String(){//big three. destuctor.
  delete [] arr;
}
String& String::operator=(String rhs){//big three. assignment operator.
  swap(rhs);
  return *this;
}
String String::swap(String& rhs){//swap the pointers on 2 char arrays.
  int tmpCap = rhs.cap;
  rhs.cap = cap;
  cap = tmpCap;
  char* tmp = rhs.arr;
  rhs.arr = arr;
  arr = tmp;
  return *this;
}

///////////////////////////////////functions////////////////////////////////////////////////////////////////
void String::reallocate(int a){//changes the size of a dynamic String. size passed as parameter.
  String tmp;
  tmp.cap = a;
  tmp.arr = new char[a];//allocate space of size passed.
  int i = 0;
  while (arr[i] != 0){//copy elements to newly allocated tmp array
    tmp.arr[i] = arr[i];
    ++i;
  }
  tmp.arr[i] = 0;
  swap(tmp);//swap pointers of tmp and passed array
}
std::vector<String> String::split(char splitter) const{//splits a String into a vecotr of
  std::vector<String> vecOfStrings;//Strings besed on the delimited passed
  int start = 0, end = 0;//returns that vector
  bool doIt = false;
  for (int i = 0; i < cap; ++i){//look if the delimiter exists
    if (arr[i] == ' '){
      doIt = true;
      break;
    }
  }
  if (doIt){//if the delimiter exists in the string start looping
    for (int i = 0; i < cap; ++i){
      if (arr[i] == splitter){//when each occurance of the delimiter is found create a
    end = i;//node of String in the vector with the chars since the previous node
    vecOfStrings.push_back(substr(start, end - 1));
    start = (end + 1);
      }
      if (i == (cap - 1)){//do this until the no more delimiters are found
    end = i;
    vecOfStrings.push_back(substr(start, end));
      }
    }
  }
  return vecOfStrings;
}
int String::length() const{//returns the length of the String before the terminating char.
  int counter = 0;
  while (arr[counter] != 0)
    ++counter;
  return counter;
}
int String::capacity() const{//accessor to capacity.
  return cap;
}
String String::substr(int start, int end) const{//returns a subset string of string passed.
  if ((start < 0) || (end < 0))//parameters are starting and ending points of the substring
    return String();
  String result;
  int returnIndex = start;
  for (int i = start; i <= end; ++i)
    result[i - start] = arr[returnIndex++];
  result[end - start + 1] = 0;
  return result;
}
int String::findChar(const char target) const{//returns the position of the first occurance of the char
  for (int i = 0; i < length(); ++i)//being searched for. returns -1 if the char is not found.
    if (arr[i] == target)
      return i;
  std::cout << "The char was not found." << std::endl;
  return -1;
}
int String::findStr(const char target[]) const{//searches for a substring in the string. returns the
  String targetString(target);//position of the first char in the substring
  return findStr(targetString);//uses the String version of findStr. look there for more info.
}
int String::findStr(const String& target) const{//searches for a substring in the string. returns the
  int targetLength = target.length();//position of the first char in the substring
  String candidate;//candidate is the string that
  int candStart = 0, candEnd = candStart + (targetLength - 1), i = 0;//will be compared to other strings
  //of the same length
  while (i < (stringSize - targetLength)){//go through char by char and compare candidate to
    candidate = substr(candStart++, candEnd++);//strings of the same length within the full string.
    if (candidate == target)//ex String = "Hello World." looking for "llo"
      return i;//"Hel" == "llo" no "Hel" == "ell" no "llo == "llo" yes.
    i++;//return 2.
  }
  std::cout << "The string was not found." << std::endl;
  return -1;//if not found at all return -1
}
int String::stringToInt(){
  int result = 0, intTmp;
  char charTmp;
  for (int i = 0; i < length(); ++i){
    charTmp = arr[i];
    switch (charTmp){
    case '0' : intTmp = 0; break;
    case '1' : intTmp = 1; break;
    case '2' : intTmp = 2; break;
    case '3' : intTmp = 3; break;
    case '4' : intTmp = 4; break;
    case '5' : intTmp = 5; break;
    case '6' : intTmp = 6; break;
    case '7' : intTmp = 7; break;
    case '8' : intTmp = 8; break;
    case '9' : intTmp = 9; break;
    case '-' : intTmp = 0; break;
    }
    if (result > 0)
      result = result * 10;
    result = result + intTmp;
  }
  return result;
}

/////////////////////////////////////////////operators////////////////////////////////////////////////////////////////////
char String::operator[](int i) const{//subscript operator. returns char whated in char array. const version.
  return arr[i];//acts as an accessor to chars
}
char& String::operator[](int i){//subscript operator. returns char whated in char array. non const version.
  return arr[i];//acts as an accessor to chars
}
String String::operator+(const String& rhs) const{//concatenate
  String result(arr);
  int start = length(), rhsIndex = 0;
  while (rhs.arr[rhsIndex] != 0){
    result.arr[start] = rhs.arr[rhsIndex];
    start++;
    rhsIndex++;
  }
  result.arr[start] = 0;
  return result;
}
bool String::operator==(const String& rhs) const{
  if (length() != rhs.length())
    return false;
  for (int i = 0; i < length(); ++i)
    if (arr[i] != rhs.arr[i])
      return false;
  return true;
}
bool String::operator!=(const String& rhs) const{
  if (*this == rhs)
    return false;
  return true;
}

bool String::operator<(const String& rhs) const{
  int i = 0;
  while (arr[i] != 0 && rhs.arr[i] != 0){
    if ((arr[i] - rhs.arr[i]) < 0)
      return true;
    else if ((arr[i] - rhs.arr[i]) > 0)
      return false;
    i++;
  }
  if (length() < rhs.length())
    return true;
  return false;
}
bool String::operator>(const String& rhs) const{
  if (*this == rhs)
    return false;
  if (*this < rhs)
    return false;
  return true;
}
bool String::operator<=(const String& rhs) const{
  if (*this == rhs)
    return true;
  if (*this < rhs)
    return true;
  return false;
}
bool String::operator>=(const String& rhs) const{
  if (*this == rhs)
    return true;
  if (*this < rhs)
    return false;
  return true;
}

//////////////////////////////free functions////////////////////////////////
////////////////////////////////////////////////////////////////////////////
///////////////they use the public functions of the String class////////////
String operator+(const char lhs[], const String& rhs){
  String lhsString(lhs);
  String result = lhsString + rhs;
  return result;
}
String operator+(const char lhs, const String& rhs){
  String lhsString(lhs);
  String result = lhsString + rhs;
  return result;
}
bool operator==(const char lhs[], const String& rhs){
  String lhsString(lhs);
  return lhsString == rhs;
}
bool operator==(const char lhs, const String& rhs){
  String lhsString(lhs);
  return lhsString == rhs;
}
bool operator!=(const char lhs[], const String& rhs){
  String lhsString(lhs);
  return lhsString != rhs;
}
bool operator!=(const char lhs, const String& rhs){
  String lhsString(lhs);
  return lhsString != rhs;
}
bool operator<(const char lhs[], const String& rhs){
  String lhsString(lhs);
  return lhsString < rhs;
}
bool operator<(const char lhs, const String& rhs){
  String lhsString(lhs);
  return lhsString < rhs;
}
bool operator>(const char lhs[], const String& rhs){
  String lhsString(lhs);
  return lhsString > rhs;
}
bool operator>(const char lhs, const String& rhs){
  String lhsString(lhs);
  return lhsString > rhs;
}
bool operator<=(const char lhs[], const String& rhs){
  String lhsString(lhs);
  return lhsString <= rhs;
}
bool operator<=(const char lhs, const String& rhs){
  String lhsString(lhs);
  return lhsString <= rhs;
}
bool operator>=(const char lhs[], const String& rhs){
  String lhsString(lhs);
  return lhsString >= rhs;
}
bool operator>=(const char lhs, const String& rhs){
  String lhsString(lhs);
  return lhsString >= rhs;
}

我已將一些代碼添加到代碼中以進行調試。 6永遠不會出現。

String postfix(String infix){
  std::vector<String> vec;
  vec = infix.split(' ');
  Stack<String> tmp;
  String right, left, op;

  int i = 0;
  while (vec[i] != ';'){
    std::cout << 1 << std::endl;
    if (vec[i] == ')'){
      right = tmp.pop();
      op = tmp.pop();
      left = tmp.pop();
      std::cout << 2 << std::endl;
      tmp.push(left + ' ' + right + ' ' + op);
      std::cout << 3 << std::endl;
    }else{
      if (vec[i] != '(' && vec[i] != ' ')
        std::cout << 4 << std::endl;
        tmp.push(vec[i]);
        std::cout << 5 << std::endl;
    }
    ++i;
  }

  std::cout << 6 << std::endl;
  String postfix = tmp.pop();
  return postfix;
}

輸出是

-bash-4.1 $使測試g ++ -g -Wall -W -Wunused -Wuninitialized -Wshadow -iquote。 -iquote ../string -c test_data3.cpp g ++ -g -Wall -W -Wunused -Wuninitialized -Wshadow string.o test_data3.o -o test_data3 ./test_intStack一切看起來不錯。 完成了對int Stack的測試。 ./test_stringStack一切看起來不錯。 完成測試字符串堆棧。 ./test_data3(AX +(B * C)); 1 5 1 4 5 1 4 5 1 5 1 4 5 1 4 5 1 4 5 1 2 3 1 2 3 1 4 5使: * [測試]分段錯誤(核心已轉儲)rm test_data3.o

您幾乎肯定要從隔離問題開始。

為此,我將先做一些重寫,以使用您自己的代碼進行后綴轉換本身,但分別將std::stringstd::stack用作字符串和堆棧類型。

如果這樣可以使問題消失,請切換一個(但不能同時切換兩個)以使用您的課程。 如果仍然有效,請切換另一個以使用您的課程。

很有可能會很快告訴您問題出在后綴轉換,堆棧還是字符串中。 一旦您弄清楚了,我可能會為有問題的組件編寫一些不錯的單元測試。

可以只在上下文中調試它,但是這樣做的機會很公平(至少以我的經驗而言),這通常會留下許多類似的問題,因此,下次嘗試使用它時,您將遇到類似的問題。在稍微不同的情況下。 通過更系統地進行處理,您可以消除整個錯誤類別,而不必一次刪除一個錯誤類別。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM