Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 空析构函数崩溃程序:C++;_C++_Destructor - Fatal编程技术网

C++ 空析构函数崩溃程序:C++;

C++ 空析构函数崩溃程序:C++;,c++,destructor,C++,Destructor,下面的程序计算真正大的数(例如600851475143)的所有素数。到目前为止,一切都正常,除了当我输入大量数据时,析构函数正在使应用程序崩溃。有人看到我的申请有什么问题吗? 重新检查我的答案后,答案是错误的,但问题仍然有效 #include <iostream> #include <iterator> #include <algorithm> #include <vector> #include <cmath> #include &l

下面的程序计算真正大的数(例如600851475143)的所有素数。到目前为止,一切都正常,除了当我输入大量数据时,析构函数正在使应用程序崩溃。有人看到我的申请有什么问题吗? 重新检查我的答案后,答案是错误的,但问题仍然有效

#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <cmath>
#include <stdexcept>
#include <climits>

typedef std::vector<unsigned long long>::const_iterator prime_it;

#define MAX_COL 900000

struct large_vector
{
public:
  large_vector(unsigned long long size, unsigned int row) :
    m_Row(row)
  {
    m_RowVector.reserve(size);
  }
  std::vector<bool> m_RowVector;
  unsigned int m_Row;
};

struct prime_factor
{
public:
  prime_factor(unsigned long long N);
  ~prime_factor() {}
  void print_primes();
private:
  std::vector<bool> m_Primes;
  std::vector<large_vector>m_Vect_Primes;
  unsigned long long m_N;
};

prime_factor::prime_factor(unsigned long long N) :
  m_N(N)
{
  // If number is odd then we need the cieling of N/2 / MAX_COL
  int number_of_vectors = (m_N % MAX_COL == 0) ? (m_N / MAX_COL) : ((m_N / MAX_COL) + 1);
  std::cout << "There will be " << number_of_vectors << " rows";
  if (number_of_vectors != 0) {
    for (int x = 0; x < number_of_vectors; ++x) {
      m_Vect_Primes.push_back(large_vector(MAX_COL, x));
    }

    m_Vect_Primes[0].m_RowVector[0] = false;
    m_Vect_Primes[0].m_RowVector[1] = false;
    unsigned long long increment = 2;
    unsigned long long index = 0;
    while (index < m_N) {
      for (index = 2*increment; index < m_N; index += increment) {
        unsigned long long row = index/MAX_COL;
        unsigned long long col = index%MAX_COL;
        m_Vect_Primes[row].m_RowVector[col] = true;
      }
      while (m_Vect_Primes[increment/MAX_COL].m_RowVector[increment%MAX_COL]) {
        increment++;
      }
    }
  }
}

void prime_factor::print_primes()
{
  for (int index = 0; index < m_N; ++index) {
    if (m_Vect_Primes[index/MAX_COL].m_RowVector[index%MAX_COL] == false) {
      std::cout << index << " ";
    }
  }
}

/*!
 * Driver
 */
int main(int argc, char *argv[])
{
  static const unsigned long long N = 600851475143;
  prime_factor pf(N);
  pf.print_primes();
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
typedef std::vector::const_iterator prime_it;
#定义最大值900000
结构大向量
{
公众:
大向量(无符号长向量,无符号整行):
m_Row(Row)
{
m_行向量保留(大小);
}
std::向量m_RowVector;
无符号整数m_行;
};
结构素数因子
{
公众:
素数因子(无符号长N);
~prime_因子(){}
无效打印素数();
私人:
向量m_素数;
向量素数;
无符号长m_N;
};
素数因子::素数因子(无符号长N):
m_N(N)
{
//如果数字是奇数,那么我们需要N/2/MAX\u COL
向量的整数=(m_N%MAX_COL==0)?(m_N/MAX_COL):((m_N/MAX_COL)+1);

std::cout你对储备的使用是不正确的

m_RowVector.reserve(size);
此处
m_RowVector
保留了空间,以便向量可以在不重新分配的情况下增长。但是
m_RowVector
的大小仍然是
0
,因此访问任何元素仍然是未定义的。必须使用
resize()
push_back()更改数组的大小
将元素放入向量


我看不出有任何错误,但我确信除了向量末尾问题之外,还有其他索引问题。我会将运算符[]的用法更改为方法at(),这将在访问向量末尾的元素时引发异常,并为您提供错误实际位置的线索。

您对保留的使用不正确

m_RowVector.reserve(size);
此处
m_RowVector
保留了空间,以便向量可以在不重新分配的情况下增长。但是
m_RowVector
的大小仍然是
0
,因此访问任何元素仍然是未定义的。必须使用
resize()
push_back()更改数组的大小
将元素放入向量


我看不出有什么错误,但我确信除了向量结束问题之外还有其他索引。我会将运算符[]的用法改为()的方法当您访问向量末尾的元素时,这将引发异常,并为您提供错误实际位置的线索。

更有可能帮助您。通常,当您看到空的析构函数崩溃时,这意味着在调用析构函数之前有其他东西损坏了堆。您能举一个例子吗“大数”?是否有任何标志可以在命令行中传递?或者只运行“valgrind--leak check=full”更有可能帮助您。通常,当您看到空的析构函数崩溃时,这意味着在调用析构函数之前有其他东西损坏了堆。您能举一个“大数”的例子吗“?您是否需要在命令行中传递任何标志?或者只运行“valgrind--leak check=full”您的建议解决了问题,使用at()是一个很好的方法。在修复错误后,我将在更新中重新发布代码。您的建议修复了问题并使用at()这是一个很好的观点。一旦我修复了错误,我将在更新中重新发布代码。