多项式程序中的 C++ 动态数组和指针导致退出错误 255

     2023-02-22     296

关键词:

【中文标题】多项式程序中的 C++ 动态数组和指针导致退出错误 255【英文标题】:C++ Dynamic Arrays and Pointers in Polynomial program causes exit error 255 【发布时间】:2015-11-01 01:29:28 【问题描述】:

我目前正在做一个程序来读取度数、系数并创建一个多项式结构。程序可以将多项式相加和相乘,然后输出和或乘积。程序运行,输出正确答案,然后得到一个windows错误:

poly.exe 已停止工作 一个问题导致程序停止正常工作。如果有可用的解决方案,Windows 将关闭该程序并通知您。

然后 scite 显示退出代码:255

我认为这可能是我用于乘法多边形的双 for 循环中的某些东西,或者是指向系数数组的指针的初始化。但我不知道是什么。我使用的可能如下:

#include <iostream>
#include <stdio.h>
using namespace std;

struct Poly 
   int degree;
   int *coeff; //array of coefficients from lowest degree to highest degree
;

//Reads the coefficients of a polynomial from standard input
//Creates a poly struct and returns a pointer to the poly
Poly* readPoly();

//Outputs a polynomial to standard output with the variable x
void outputPoly(const Poly* p, char x);

//Computes the sum of two polynomials and returns
//a pointer to the new poly struct which is their sum
Poly* addPoly(const Poly* a, const Poly* b);

//Computes the product of two polynomials and returns
//a pointer to the new poly struct which is their product
Poly* multPoly(const Poly* a, const Poly* b);

//Returns to the heap the memory allocated for the polynomial
//and sets p to the nullptr
void deletePoly(Poly* &p);

Poly* readPoly() 
   int deg;
   //Read the highest degree
   cout << "Input the degree: ";
   cin >> deg;

   //Handles when the degree is == 0
   if(deg == 0) 
      int *C = new int[deg+1];
      Poly *p;
      p = new Poly;
      p->degree = deg;
      p->coeff = C;
      return p;
   

   int *C = new int[deg+1];
   //Read the coefficients
   cout << "Input the coefficients: ";
   for(int i = 0; i <= deg; i++) 
      cin >> C[i];
   

   //Create a new poly structure, assign its fields 
   //and retun a pointer to the new structure
   Poly *p;
   p = new Poly;
   p->degree = deg;
   p->coeff = C;

   return p;


void outputPoly(const Poly* p, char x) 
   //Set the degree and cooefficients to be used in the loop
   int d = p->degree;
   int *C = p->coeff;

   //Output the polynomial, depending on the degree/coeff
   for(int i = 0; i <= d; i++) 

      //if the coeff is zero, and the degree is > 1
      if(C[i] == 0 && i > 0)
         continue; //Skip the +

      //if the degree is 0, and the coeff is 0
      else if(i == 0 && C[i] == 0) 
         cout << C[i];
         continue; //Skip the +
      

      //if the degree is 0, and the coeff is not 0
      else if(i == 0 && C[i] != 0)
         cout << C[i];

      //if the degree is 1, and the coeff is 0
      else if(C[i] == 0 && i == 1)
         cout << x;

      //if the degree is 1, and the coeff is 1
      else if(C[i] == 1 && i == 1)
         cout << x;

      //if the degree is 0, and the coeff is > 0
      else if(C[i] > 0 && i == 0)
         cout << C[i] << "*" << x;

      //if the coefficient is 1
      else if(C[i] == 1)
         cout << x << "^" << i;

      //if the degree is 1
      else if(i == 1)
         cout << C[i] << "*" << x;

      //any other circumstance
      else
         cout << C[i] << "*" << x << "^" << i;

      //Print a +, as long as it's not the last term
      if(i != d)
         cout << " + ";
   


void deletePoly(Poly* &p) 
   delete[] p->coeff; //Delete the array first
   delete p; 
   p = nullptr;


const Poly* getLargest(const Poly* a, const Poly* b) 
//Helper function to get the larger polynomial, given two
   if(a->degree > b->degree)
      return a;
   else
      return b;


const Poly* getSmallest(const Poly* a, const Poly* b) 
//Helper function to get the smaller polynomial, given two
   if(a->degree < b->degree)
      return a;
   else
      return b;


Poly* addPoly(const Poly* a, const Poly* b)
   int i, j;

   int *polyOneC = a->coeff;
   int *polyTwoC = b->coeff;

   //The new polynomials degree is the size of the polynomial that is the largest
   int polyThreeD = getLargest(a, b)->degree;
   int *polyThreeC = new int[polyThreeD];

   for(i = 0, j = 0; j <= polyThreeD; i++, j++) 
      //If the polynomials are of different size, 
      //then any coefficent term over the size 
      //of the smaller polynomial degree stays the same
      if(i > getSmallest(a, b)->degree)
         polyThreeC[i] = getLargest(a, b)->coeff[i];
      else
         //Otherwise, just add them
         polyThreeC[i] = polyOneC[j] + polyTwoC[j];
         //"Shifts" if it's equal to zero
         if(polyThreeC[i] == 0)
            i--;
   

   //Ensures the remaining terms have a coefficient value(0)
   while(i <= polyThreeD) 
      polyThreeC[i] = 0;
      i++;
   

   Poly *sumPoly;
   sumPoly = new Poly;
   sumPoly->degree = polyThreeD;
   sumPoly->coeff = polyThreeC;

   return sumPoly;


Poly* multPoly(const Poly* a, const Poly* b) 

   //Get the degrees and arrays of coefficients
   int polyOneD = a->degree;
   int polyTwoD = b->degree; 

   int *polyOneC = a-> coeff;
   int *polyTwoC = b-> coeff;

   int polyThreeD = polyOneD + polyTwoD; 
   int *polyThreeC = new int[polyThreeD + 1];

   //Initialize the array of coefficients
   for(int i = 0; i <= polyThreeD; i++)
     polyThreeC[i] = 0;

   //Multiple the coeffients and add to the position of the degree
   for(int i = 0; i <= polyOneD; i++) 
      for(int j = 0; j <= polyTwoD; j++) 
            polyThreeC[i + j] += (polyOneC[i] * polyTwoC[j]);
      
   

   //Create a new polynomial pointer and return it
   Poly *productPoly;
   productPoly = new Poly;
   productPoly->degree = polyThreeD;
   productPoly->coeff = polyThreeC;
   return productPoly;   


int main() 
   Poly *x = readPoly();
   Poly *y = readPoly();

   //Test the add poly function
   cout << "(";
   outputPoly(x, 'x');
   cout << ")";


   cout << " + ";

   cout << "(";
   outputPoly(y, 'x');
   cout << ")";
   cout << endl;

   //Call addPoly
   Poly *z = addPoly(x, y);

   cout << "= ";
   cout << "(";
   outputPoly(z, 'x');
   cout << ")";

   cout << endl;
   cout << endl;

   //Test the multiply poly function
   cout << "(";
   outputPoly(x, 'x');
   cout << ")";


   cout << " * ";

   cout << "(";
   outputPoly(y, 'x');
   cout << ")";
   cout << endl;

   //Call multPoly
   z = multPoly(x, y);

   cout << "= ";
   cout << "(";
   outputPoly(z, 'x');
   cout << ")";

   cout << endl;


   //Delete the polynomials now that we are done with them
   deletePoly(x);
   deletePoly(y);
   deletePoly(z);

另一件事是每次运行时都不会发生错误,到目前为止,当我输入一个次数为 4 的多项式时,我已经看到了它。与次数为 2 的多项式相反,它可以工作很好。

谁能帮帮我!

【问题讨论】:

使用容器应为std::vector,并从此代码中删除所有new。您可能会发现这些错误完全消失了,并且优秀的调试器会检查向量,从而更容易发现问题。 如果您发布一个完整但最小的示例供读者尝试,您可以避免因缺少此类示例而关闭问题。 您是否尝试过使用调试器运行它并单步执行直到出现错误? @DominicMcDonnell 你有推荐的 Windows 调试器吗? 你用什么编译?通常编译器自带一个。 Visual Studio 非常好,并且有免费版本可用。我也用过Code::Blocks,在那个下调试有点差,但还是不错的。 【参考方案1】:

您在 addPoly (polyThreeC) 中分配的系数数组不够大。分配数组时忘记在度数上加一。

【讨论】:

救命稻草!太简单。谢谢!

C++ 动态内存分配与结构中的数组

...态内存、指针和结构部分后,我尝试将它们放在一个示例程序中。本质上,我正在尝试动态分配结构数组(程序输入“produce” 查看详情

访问导致运行时错误的动态数组

...态内存,结果碰上了一堵巨大的墙。我正在尝试创建一个程序,用户可以在其中输入任意数量的字符串,然后可以随时退出,但是在输入第二个字符串后,程序崩溃而没有给我任何特定的错误消息。#include"stdafx.h"#include"str 查看详情

C++ - 数组的动态指针

...的新手。作为我硕士论文的一部分,我正在用C++编写一个程序,它还将变量m和d(均为整数)作为参数。d是2的幂(这意味着2^d元素)。参数m定义了一个元素与整个组(2^d元素)之间可能的交互次数。可能的交互次数计算如下:\... 查看详情

指向 C++ 中动态分配的二维数组中的一行的指针

】指向C++中动态分配的二维数组中的一行的指针【英文标题】:Pointertoonerowindynamicallyallocated2DarrayinC++【发布时间】:2016-07-2519:43:17【问题描述】:我有一个动态分配的二维数组Volatility[r][c]在C++中具有r行和c列。是否可以以某种... 查看详情

数组和指针+错误

...7-1700:18:21【问题描述】:这是作业:您的目标是编写一个程序,以相反的顺序显示来自输入的一系列整数。您的程序将提示用户输入此列表中的值的数量,它将用作动态的大小在此提示后声明的数组。数组size未知,value是指针,... 查看详情

指针数组中的 C++ 错误

】指针数组中的C++错误【英文标题】:C++Errorinarrayofpointers【发布时间】:2013-07-2316:57:16【问题描述】:我有一段代码:intCPUs=GetNumCPUs();FILE*newFile[CPUs];我遇到了一个错误。它在第二行标记了“CPU”并说:“表达式必须有一个常数... 查看详情

C++:delete[] 错误,指针未分配

...8-09-0700:01:03【问题描述】:我正在为一个实验室开发一个程序,我需要一些有关内存管理的帮助。作为一个整体,我是C++新手,虽然我有其他语言的经验,但动态内存管理让我感到困惑。另外,因为这是针对实验室的,所以我不... 查看详情

assertionfailed是啥原因?

...越界:访问了超过数组长度的内存。以下面一段简单的源程序代码为例:在执行程序时弹出的“DebugAssertionFailed”错误警告对话框,这种情况大多是指针引起的错误。下图红框标记的地方,是Distance类析构函数,这里使用了delete... 查看详情

函数在不删除动态数组的情况下工作。程序有效且没有错误

】函数在不删除动态数组的情况下工作。程序有效且没有错误【英文标题】:Functionworkswithoutdeletingthedynamicarray.Programworksandhasnoerrors【发布时间】:2016-05-2310:36:21【问题描述】:我是C++的学生和初学者。目前在课堂上我们正在练... 查看详情

使用动态数组的 C++ 逻辑错误

...pItems作为一个数组,是ditems的两倍,并且还临时存储ditems中的所有项目;然后删除并分配ditems等于tempItems代码符合要求,但是当使用足够的 查看详情

C++中的指针和多维数组

】C++中的指针和多维数组【英文标题】:PointersandmultidimensionalarraysinC++【发布时间】:2018-01-0621:21:32【问题描述】:我有这段代码:#include<iostream>intmain()intia[3][4];//arrayofsize3;eachelementisanarrayofintsofsize4int(*p)[4]=ia;//ppointstoanarr 查看详情

请详细解释一下c++中的new和delete

...解+手打,笑纳O(∩_∩)O~一、new和delete首先,你需要知道程序在内存中。它分为四部分:code:代码data:数据(全局、静态变量)stack:栈(局部变量)heap:堆(你负责的地方,比如用来动态内存分配,即new和delete)程序运行时所需... 查看详情

C++ 中的静态数组与动态数组

】C++中的静态数组与动态数组【英文标题】:Staticarrayvs.dynamicarrayinC++【发布时间】:2011-02-0922:54:01【问题描述】:C++中静态数组和动态数组有什么区别?我必须为我的班级做一个作业,它说不要使用静态数组,只能使用动态数... 查看详情

动态数组和结构

...谢!我只需要将作业的右侧转换为Term。我必须创建一个多项式的动态数组,每个多项式都有一个动态的项数组。当给这个术语一个指数和系数时,我得到一个错误“\'\'标记之前的预期表达式”。分配值时我做错了什么?另外,... 查看详情

回溯 C++ 代码 gdb 中的指针

...布时间】:2014-01-0103:04:34【问题描述】:我在运行C++应用程序时遇到seg错误。在gdb中,它以某种方式显示我的一个指针位置已损坏。但是我在我的应用程序中创建了10万个这样的对象指针。我怎样才能看到导致崩溃的一个我可以... 查看详情

c++中的new函数怎么用?

...ble[n2+1];for(i=0;i<=n1;i++)for(j=0;j<=n2;j++)a[i][j]=0.0;returna;这程序具体是怎么运行的啊?特别是double**a=newdouble*[n1+1];这句话怎么定义的。new 的作用是动态开辟空间,一般的形式估计你也会了。我就直接给你讲题吧你这个函数的作... 查看详情

为啥 C++ 中的以下结构声明会导致退出 127?

】为啥C++中的以下结构声明会导致退出127?【英文标题】:WhydoesthefollowingstructdeclarationinC++causesexit127?为什么C++中的以下结构声明会导致退出127?【发布时间】:2020-02-0218:08:53【问题描述】:我正在尝试解决USACO培训页面上的一个... 查看详情

C++ 指针和 C 风格的数组初始化

...on【发布时间】:2021-02-1615:26:13【问题描述】:这里是Java程序员,C++新手。我一直在使用C风格的“传统”数组(类似于java中的数组)。我理解在C++中我们可以创建一个简单的数组,如下所示:Personpeople[3];这个数组的内容本质上... 查看详情