从(bad) array[size]移动到array = new float[size]
Moving from (bad) array[size] to array = new float[size]
我正在编写一个查找类的根的程序,并且已经完成并使其正常工作。当我去把它,我看到的文档需要.cpp
编译使用Visual Studio 2012 -所以我试了一下。我通常使用Dev c++ -我发现它允许我编译"时髦的东西",比如动态声明数组而不使用malloc或new操作符。
因此,在找到与我如何错误定义数组相关的错误之后-我试图使用malloc
, calloc
和new
/delete
来解决这个问题-它一直给我内存分配错误。整个46981239487532字节错误。
现在,我试图"返回"程序到它以前的方式,现在我甚至不能让它工作。我甚至不完全确定我是如何在Dev c++中设置数组的。下面的代码是:
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
float newton(float a, float b, float poly[],float n, float *fx, float *derfx);
float horner(float poly[], int n, float x, float *derx);
float bisection(float a, float b, float poly[], float n, float *fx);
int main(int argc, char *argv[])
{
float a, b, derr1 = 0, dummyvar = 0, fr1 = 0, fr0;
float constants[argc-3];
//float* constants = NULL;
//constants = new float[argc-3];
//constants = (float*)calloc(argc-3,sizeof(float));
//In order to get a and b from being a char to floating point, the following lines are used.
//The indexes are set relative to the size of argv array in order to allow for dynamically sized inputs. atof is a char to float converter.
a = atof(argv[argc-2]);
b = atof(argv[argc-1]);
//In order to get a easy to work with array for horners method,
//all of the values excluding the last two are put into a new floating point array
for (int i = 0; i <= argc - 3; i++){
constants[i] = atof(argv[i+1]);
}
bisection(a, b, constants, argc - 3, &fr0);
newton(a, b, constants, argc - 3, &fr1, &derr1);
cout << "f(a) = " << horner(constants,argc-3,a,&dummyvar);
cout << ", f(b) = " << horner(constants,argc-3,b,&dummyvar);
cout << ", f(Bisection Root) = " << fr0;
cout << ", f(Newton Root) = "<<fr1<<", f'(Newton Root) = "<<derr1<<endl;
return 0;
}
// Poly[] is the polynomial constants, n is the number of degrees of the polynomial (the size of poly[]), x is the value of the function we want the solution for.
float horner(float poly[], int n, float x, float *derx)
{
float fx[2] = {0, 0};
fx[0] = poly[0]; // Initialize fx to the largest degree constant.
float derconstant[n];
//float* derconstant = NULL;
//derconstant = new float[n];
//derconstant = (float*)calloc(n,sizeof(float));
derconstant[0] = poly[0];
// Each term is multiplied by the last by X, then you add the next poly constant. The end result is the function at X.
for (int i = 1; i < n; i++){
fx[0] = fx[0]*x + poly[i];
// Each itteration has the constant saved to form the derivative function, which is evaluated in the next for loop.
derconstant[i]=fx[0];
}
// The same method is used to calculate the derivative at X, only using n-1 instead of n.
fx[1] = derconstant[0]; // Initialize fx[1] to the largest derivative degree constant.
for (int i = 1; i < n - 1; i++){
fx[1] = fx[1]*x + derconstant[i];
}
*derx = fx[1];
return fx[0];
}
float bisection(float a, float b, float poly[], float n, float *fx)
{
float r0 =0, count0 = 0;
float c = (a + b)/2; // c is the midpoint from a to b
float fc, fa, fb;
int rootfound = 0;
float *derx;
derx = 0; // Needs to be defined so that my method for horner's method will work for bisection.
fa = horner(poly, n, a, derx); // The following three lines use horner's method to get fa,fb, and fc.
fb = horner(poly, n, b, derx);
fc = horner(poly, n, c, derx);
while ((count0 <= 100000) || (rootfound == 0)) { // The algorithm has a limit of 1000 itterations to solve the root.
if (count0 <= 100000) {
count0++;
if ((c == r0) && (fabs(fc) <= 0.0001)) {
rootfound=1;
cout << "Bisection Root: " << r0 << endl;
cout << "Iterations: " << count0+1 << endl;
*fx = fc;
break;
}
else
{
if (((fc > 0) && (fb > 0)) || ((fc < 0) && (fb < 0))) { // Checks if fb and fc are the same sign.
b = c; // If fc and fb have the same sign, thenb "moves" to c.
r0 = c; // Sets the current root approximation to the last c value.
c = (a + b)/2; // c is recalculated.
}
else
{
a=c; // Shift a to c for next itteration.
r0=c; // Sets the current root approximation to the last c value.
c=(a+b)/2; // Calculate next c for next itteration.
}
fa = horner(poly, n, a, derx); // The following three send the new a,b,and c values to horner's method for recalculation.
fb = horner(poly, n, b, derx);
fc = horner(poly, n, c, derx);
}
}
else
{
cout << "Bisection Method could not find root within 100000 itterations" << endl;
break;
}
}
return 0;
}
float newton(float a, float b, float poly[],float n, float *fx, float *derfx){
float x0, x1;
int rootfound1 = 1, count1 = 0;
x0 = (a + b)/2;
x1 = x0;
float fx0, derfx0;
fx0 = horner(poly, n, x0, &derfx0);
while ((count1 <= 100000) || (rootfound1 == 0)) {
count1++;
if (count1 <= 100000) {
if ((fabs(fx0) <= 0.0001)) {
rootfound1 = 1;
cout << "Newtons Root: " << x1 << endl;
cout << "Iterations: " << count1 << endl;
break;
}
else
{
x1 = x0 - (fx0/derfx0);
x0 = x1;
fx0 = horner(poly, n, x0, &derfx0);
*derfx = derfx0;
*fx = fx0;
}
}
else
{
cout << "Newtons Method could not find a root within 100000 itterations" << endl;
break;
}
}
return 0;
}
所以我花了几个小时试图解决这个问题,最终,我不得不问。我看到的每一个地方都说要定义为
float* constants = NULL;
constants = new float[size];
但是这会使我的程序崩溃——大概是由于分配了太多的内存。我已经用各种方法和组合把我尝试过的东西注释掉了。如果您想更详细地了解"故障点",它们位于主要和次要功能的最开头。
这里有一个问题,主要是为constants
分配argc-3
浮点数的空间(以各种方式),但是循环中的代码写过了数组的末尾。
改变:
for( int i = 0; i<=argc-3; i++){
for( int i = 0; i<argc-3; i++){
这就足以导致分配错误。
编辑:还要注意,如果您使用new
为某些东西分配空间,则需要使用delete
删除它,否则您将继续耗尽内存并可能耗尽(特别是如果您在100,000个循环中执行此操作)。
编辑2:正如Galik在下面提到的,因为您使用derconstant = new float[n]
来分配内存,所以您需要使用delete [] derconstant
来释放内存。当您开始为类对象分配空间时,这一点很重要,因为delete []
形式将调用数组中每个元素的析构函数。
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 大于65535的C++数组[size]引发不一致的溢出
- 为什么(-1)%vector::size()总是返回0
- 从 std::string 到 std::array<char,size> 的 memcopy 额外数据是否是一种未定义的行为?
- 为什么我必须在初始化 std::array<SomeStruct, size> 时指定每个项目的类型C++
- 如何在 JniWrapper 中将 Java ArrayList<float[]> 映射到C++ Vector<array<float,size>>?
- 使用 constexpr 作为 std::array size
- 警告的原因是什么:"when type is in parentheses, array cannot have dynamic size"?
- std::span.size() vs array/vector size
- std::array::max_size 和 std::array::size 给出不同结果的示例
- 为什么 std::array::size 不是静态的?
- 具有恒定大小数组的警告"ISO C++ forbids variable-size array"
- 为什么 std::array::size constexpr 具有简单类型(int、double、..),而不是 std
- 如何在 c++11 中使用容器 std::array<type, size> 用于多维数组?
- 使用 std::array::size 实例化 std::array 时出错
- 为什么我会得到"cannot allocate an array of constant size 0"?
- 在比较 array.size() 与负值时获得 if 条件的意外行为
- ARRAY[T, SIZE]合适的默认构造函数可用
- c++中的快速排序array.size()错误
- 从(bad) array[size]移动到array = new float[size]