[英]How do I fix this Stack overflow error in my C++ program?
所以,我是C ++的初學者......我認為我已經牢牢掌握了動態內存分配,但我想我沒有。 我在網上搜索了很多解決方案,但我仍然無法解決我的問題。 我一直收到錯誤0xC00000FD:運行調試時堆棧溢出。 我很確定這個問題與我在默認構造函數中分配內存的方式有關,但也許是其他的東西。 我想我需要更深入的見解。 任何幫助或暗示確定問題將不勝感激。
這是一個從用戶輸入計算矩形的面積和周長的簡單程序。
這是頭文件:
#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;
這是客戶端文件:
#include "stdafx.h"
#include "my_Rectangle.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
float xlength,xwidth;
cout<<"Welcome to the rectangle Area Calculator!"<<endl;
cout<<"Please enter the length and width of your rectangle:"<<endl;
cout<<"Length: "; cin>>xlength;
cout<<"Width: "; cin>>xwidth;
cout<<"Rectangle Information:"<<endl;
cout<<"Length: "<<xlength<<"m\tWidth: "<<xwidth<<" m"<<endl;
my_Rectangle userRECT(xlength,xwidth);
reTRY:
int user_choice;
cout<<"More options:"<<endl;
cout<<"1. Change rectangle size"<<endl;
cout<<"2. Exit"<<endl;
cout<<"Enter a number: ";
cin>>user_choice;
switch (user_choice)
{
case 1:
cout<<"Please enter the new length and width of rectangle:"<<endl;
cout<<"Length: "; cin>>xlength;
cout<<"Width: "; cin>>xwidth;
cout<<"\nRectangle Information:"<<endl;
cout<<"Length: "<<xlength<<"m\tWidth: "<<xwidth<<" m"<<endl;
userRECT.change_RECTsize(xlength,xwidth,userRECT);
break;
case 2:
exit(1);
break;
default: cout<<"\nThat is not a valid option. Please try again!" ;
goto reTRY;
break;
}
cin.get();cin.get();
return 0;
}
堆棧溢出與動態分配幾乎沒有關系 - 堆棧用於函數調用和局部變量(靜態和自動分配)。 但是在默認構造函數中確實有無限遞歸:
my_Rectangle::my_Rectangle() //default constructor
{
tempSTORE=new my_Rectangle;
這將分配tempSTORE
並在其上調用默認構造函數,它將分配一個tempSTORE
,其中......
你似乎對記憶和施工操作的順序感到困惑。 構造函數和析構函數在已由其他人分配的內存上運行。 順序如下:
new
) delete
) 這意味着你的構造函數不應該關心為它正在構造的對象獲取內存,它已經被某人提供了。 析構函數也不應該關心釋放對象本身占用的內存,一旦析構函數結束,這將由其他人完成。
要了解更多相關信息,我建議您閱讀一本好書 。
my_Rectangle::my_Rectangle() //default constructor
{
tempSTORE=new my_Rectangle;
有問題 - new my_Rectangle
調用這個構造函數,當它試圖創建一個無限的矩形鏈時,它會陷入遞歸死亡螺旋。
我不知道如何修復它,因為我不知道tempSTORE
應該是什么。 你好像在寫一些隨機值,從來沒有把它們讀回來 - 它看起來根本不應該存在。 無論如何,你肯定無法在這個地方以這種方式創建它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.