如何修复C+中的堆栈溢出错误+;节目? 所以我是C++初学者。我原以为我对动态内存分配有着坚实的掌握,但我想我没有。我在网上搜索了许多解决方案,但仍然无法解决我的问题。运行调试时,我不断收到错误0xC00000FD:堆栈溢出。 我很确定这个问题与我在默认构造函数中分配内存的方式有关,但可能是其他原因。我想我需要更深入的了解。 如果您能提供任何帮助或提示来确定问题,我们将不胜感激

如何修复C+中的堆栈溢出错误+;节目? 所以我是C++初学者。我原以为我对动态内存分配有着坚实的掌握,但我想我没有。我在网上搜索了许多解决方案,但仍然无法解决我的问题。运行调试时,我不断收到错误0xC00000FD:堆栈溢出。 我很确定这个问题与我在默认构造函数中分配内存的方式有关,但可能是其他原因。我想我需要更深入的了解。 如果您能提供任何帮助或提示来确定问题,我们将不胜感激,c++,memory-management,constructor,stack-overflow,C++,Memory Management,Constructor,Stack Overflow,这是一个从用户输入计算矩形面积和周长的简单程序 这是头文件: #pragma once class my_Rectangle { private: float m_area; float m_perimeter; float m_length; float m_width; void update_rec(); my_Rectangle*tempSTORE; public: float calc_area(float length,floa

这是一个从用户输入计算矩形面积和周长的简单程序

这是头文件:

#pragma once
class my_Rectangle
{
private:
    float m_area;
    float m_perimeter;
    float m_length;
    float m_width;
    void update_rec();
    my_Rectangle*tempSTORE;
public:
    float calc_area(float length,float width);
    float calc_perim(float length,float width);
    void change_RECTsize(float length,float width,my_Rectangle tempSTORE);


    my_Rectangle(); //constructor
    my_Rectangle(float length,float width);
    ~my_Rectangle(); //destructor
};
这是类成员定义文件:

#include "StdAfx.h"
#include "my_Rectangle.h"
#include <iostream>

//using namespace std;

//This function calculates the area
float my_Rectangle::calc_area(float length,float width)
{
    float area = width * length;

    std::cout<<"\nThe area of the rectangle is: "<<area<<" square metres."<<std::endl;  // ::scope operater
    return area;
}

//This function calculates the perimeter
float my_Rectangle::calc_perim(float length,float width)
{
float perimeter=(2*length)+(2*width);

std::cout<<"\nThe perimeter of the rectangle is "<<perimeter<<" metres."<<std::endl;
return perimeter;
}
//this function changes the size of the rectangle
void my_Rectangle::change_RECTsize(float length,float width,my_Rectangle tempSTORE)
{
    tempSTORE.m_length=length;
    tempSTORE.m_width=width;
    my_Rectangle::calc_area(length,width);
    my_Rectangle::calc_perim(length,width);
}



my_Rectangle::my_Rectangle() //default constructor
{
    tempSTORE=new my_Rectangle;

    tempSTORE->m_length=0.00;
    tempSTORE->m_width=0.00;
    tempSTORE->m_area=0.00;
    tempSTORE->m_perimeter=0.00;

}
my_Rectangle::my_Rectangle(float length,float width) //initialized constructor
{
    tempSTORE=new my_Rectangle;

    tempSTORE->m_length=length;
    tempSTORE->m_width=width;
    calc_area(length,width);
    calc_perim(length,width);
}


my_Rectangle::~my_Rectangle() //destructor
{
     delete tempSTORE;
std::cout<<"\nThe memory has been freed!!"<<std::endl; 
#包括“StdAfx.h”
#包括“my_Rectangle.h”
#包括
//使用名称空间std;
//此函数用于计算面积
浮动我的\u矩形::计算\u区域(浮动长度、浮动宽度)
{
浮动面积=宽度*长度;

std::cout堆栈溢出与动态分配关系不大-堆栈用于函数调用和局部变量(静态和自动分配)。但是默认构造函数中存在无限递归:

my_Rectangle::my_Rectangle() //default constructor
{
    tempSTORE=new my_Rectangle;
这将分配
tempSTORE
并调用其上的默认构造函数,该构造函数将分配一个
tempSTORE
,该构造函数


您似乎对内存和构造操作的顺序感到困惑。构造函数和析构函数在其他人已分配的内存上运行。顺序如下:

  • 内存是以某种方式分配的(在局部变量的堆栈上,通过
    new
    在堆上)
  • 构造函数在内存中自动执行
  • 对象寿命
  • 启动对象死亡(局部变量超出范围,有人调用
    delete
  • 析构函数在内存中自动执行
  • 内存被释放
  • 这意味着您的构造函数不应该关心为正在构造的对象获取内存,因为它已经由某人提供。析构函数也不应该关心释放对象本身占用的内存,这将在析构函数结束后由其他人完成

    要了解更多信息,我建议您遵循以下步骤

    有一个问题-
    new my_Rectangle
    调用这个构造函数,当它试图创建一个无限长的矩形链时,陷入了一个递归死亡螺旋


    我不知道如何修复它,因为我不知道什么是<代码> TunSturt。你似乎是在写随机值,并且从来没有读过它——它看起来根本就不应该存在。无论如何,你当然不能在这个地方用这种方式创建。顺便说一下,在C++中永远不要使用<代码> Goto < /Cord>。hoever(告诉你/向你展示/暗示你/通过心灵感应与你沟通)“<代码> Goto <代码>是C++中一个好的或有用的想法。永远不要使用它。永远不涉及你的问题,但是CueEXLigStand按值取My矩形。所做的修改只会影响你所期望的副本。考虑通过引用来代替它。从<代码> ReCTAN中删除<代码> TunSturt。gle

    。如果你想要一个临时变量用于某些计算,那么在需要时声明它,不要把它放在你的类中。弗朗索瓦·莫桑,我也看到了这一点,但我不确定我是否可以通过引用或指针传递类。有可能吗?使用循环而不是goto。那么,我应该如何最好地解决分配内存调用的问题y指向新创建的类对象?最初,我没有在类声明中定义tempSTORE,但如果没有它,析构函数将无法识别新创建的指向my_Rectangle类的已分配内存的指针。例如,在构造函数中:my_Rectangle*tempSTORE=new my_Rectangle//在析构函数中为新类对象分配内存或:删除临时存储;//未识别临时存储(未定义)NeurSer2424为什么存在TunSturn?我不明白你想用它做什么。NelelS24我已经扩展了答案。好,我要从我的代码中倾倒TwittStand并尝试你所说的。谢谢所有的帮助!我将做一些深入的内存分配的阅读。我用的是C++的Primer-Plus,Stephen Prata,其中的一些朋友。S推荐给我。它起初很容易使用,但是后面的章节有点吓人。我会尝试看看其他的书。从C到C++的转换不像我想象的那么容易。NILRESR24注意到我链接的书列表并没有确切地推荐C++ +底漆+…
    my_Rectangle::my_Rectangle() //default constructor
    {
        tempSTORE=new my_Rectangle;
    
    my_Rectangle::my_Rectangle() //default constructor
    {
        tempSTORE=new my_Rectangle;