C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在C程序中修复此堆栈溢出错误?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
所以,我是C的初学者…我认为我已经牢牢掌握了动态内存分配,但我想我没有.我在网上搜索了很多解决方案,但我仍然无法解决我的问题.我一直收到错误0xC00000FD:运行调试时堆栈溢出.
我很确定这个问题与我在认构造函数中分配内存的方式有关,但也许是其他的东西.我想我需要更深入的见解.
任何帮助或暗示确定问题将不胜感激.

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

这是头文件

@H_874_13@#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 };

这是类成员定义文件

@H_874_13@#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,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;

这是客户端文件

@H_874_13@#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; }

解决方法

堆栈溢出与动态分配几乎没有关系 – 堆栈用于函数调用和局部变量(静态和自动分配).但是在认构造函数中确实有无限递归:

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

这将分配tempSTORE并在其上调用认构造函数,它将分配一个tempSTORE,其中……

你似乎对记忆和施工操作的顺序感到困惑.构造函数和析构函数在已由其他人分配的内存上运行.顺序如下:

>内存以某种方式分配(在堆栈上为局部变量,在堆上通过new)
>构造函数在内存中自动执行
>物体生活
>启动对象死亡(局部变量超出范围,有人称为删除)
>析构函数在内存中自动执行
>释放内存

这意味着你的构造函数不应该关心为它正在构造的对象获取内存,它已经被某人提供了.析构函数也不应该关心释放对象本身占用的内存,一旦析构函数结束,这将由其他人完成.

要了解更多相关信息,建议您按照good book进行操作.

大佬总结

以上是大佬教程为你收集整理的如何在C程序中修复此堆栈溢出错误?全部内容,希望文章能够帮你解决如何在C程序中修复此堆栈溢出错误?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。