关于栈溢出的思考与实验

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_43894877/article/details/90607613

堆栈的结构布局:
在这里插入图片描述
栈溢出:栈溢出就是缓冲区溢出的一种。 由于缓冲区溢出而使得有用的存储单元被改写,往往会引发不可预料的后果。程序在运行过程中,为了临时存取数据的需要,一般都要分配一些内存空间,通常称这 些空间为缓冲区。如果向缓冲区中写入超过其本身长度的数据,以致于缓冲区无法容纳,就会造成缓冲区以外的存储单元被改写,这种现象就称为缓冲区溢出。

代码测试:
运行如下代码:

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int a[2];
    double d;
} struct_t;

double fun(int i) {
    volatile struct_t s;
    s.d = 3.14;
    s.a[i] = 1073741824; /* Possibly out of bounds */
    return s.d; /* Should be 3.14 */
}

int main(int argc, char *argv[]) {
    int i = 0;
    if (argc >= 2)
	i = atoi(argv[1]);
    double d = fun(i);
    printf("fun(%d) --> %.10f\n", i, d);
    return 0;
}

根据如上代码,定义了int a[2] 以及double d,根据栈存储,先分别分配给了8个字节的存储空间,运行结果如下所示:
在这里插入图片描述
根据结果来看:输入0和1 对结果无影响,当输入3和4时,占用了double d的存储空间,覆盖了原有的值,从而导致结果错误,在接下来输入5和6时,由于没有覆盖到d的存储空间,结果无影响,但是由于会覆盖到其它重要的数据的空间,就会出现如图,核心已经转储的结果。

展开阅读全文

关于溢出错误。求助

05-13

java.lang.StackOverflowErrorrn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229)rn at com.lan.donen.login.dao.impl.LoginUserDaoImpl.findByProperty(LoginUserDaoImpl.java:229) 论坛

求助!关于溢出异常

12-23

小弟初学面向对象的WINDOWS编程,正在编一个备忘录,编辑记事的代码如下:rnusing System;rnusing System.Drawing;rnusing System.Collections;rnusing System.ComponentModel;rnusing System.Windows.Forms;rnusing System.Data;rnrnnamespace 备忘录rnrn /// rn /// Form2 的摘要说明。rn /// rn public class 编辑记事 : System.Windows.Forms.Formrn rn public System.Windows.Forms.DataGrid dataGrid1;rn private System.Windows.Forms.Button button1;rn private System.Windows.Forms.Button button2;rn /// rn /// 必需的设计器变量。rn /// rn private System.ComponentModel.Container components = null;rn rn rn public 编辑记事()rn rn //rn // Windows 窗体设计器支持所必需的rn //rn rn Dt dt1 = new Dt();rn dt1.DT();rn dt1.Link();rn Load ld1 = new Load();rn ld1.load();rn InitializeComponent();rn //rn // TODO: 在 InitializeComponent 调用后添加任何构造函数代码rn //rn rnrn /// rn /// 清理所有正在使用的资源。rn /// rn protected override void Dispose( bool disposing )rn rn if( disposing )rn rn if(components != null)rn rn components.Dispose();rn rn rn base.Dispose( disposing );rn rnrn #region Windows 窗体设计器生成的代码rn /// rn /// 设计器支持所需的方法 - 不要使用代码编辑器修改rn /// 此方法的内容。rn /// rn private void InitializeComponent()rn rn this.dataGrid1 = new System.Windows.Forms.DataGrid();rn this.button1 = new System.Windows.Forms.Button();rn this.button2 = new System.Windows.Forms.Button();rn ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();rn this.SuspendLayout();rn // rn // dataGrid1rn // rn this.dataGrid1.DataMember = "";rn this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;rn this.dataGrid1.Location = new System.Drawing.Point(0, 0);rn this.dataGrid1.Name = "dataGrid1";rn this.dataGrid1.Size = new System.Drawing.Size(344, 200);rn this.dataGrid1.TabIndex = 0;rn // rn // button1rn // rn this.button1.Location = new System.Drawing.Point(40, 224);rn this.button1.Name = "button1";rn this.button1.Size = new System.Drawing.Size(104, 23);rn this.button1.TabIndex = 1;rn this.button1.Text = "删除记录";rn // rn // button2rn // rn this.button2.Location = new System.Drawing.Point(192, 224);rn this.button2.Name = "button2";rn this.button2.Size = new System.Drawing.Size(112, 23);rn this.button2.TabIndex = 2;rn this.button2.Text = "退出并保存";rn this.button2.Click += new System.EventHandler(this.button2_Click);rn // rn // 编辑记事rn // rn this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);rn this.ClientSize = new System.Drawing.Size(344, 270);rn this.Controls.Add(this.button2);rn this.Controls.Add(this.button1);rn this.Controls.Add(this.dataGrid1);rn this.Name = "编辑记事";rn this.Text = "Form2";rn ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();rn this.ResumeLayout(false);rnrn rn #endregionrnrn private void button2_Click(object sender, System.EventArgs e)rn rn Save sv1 = new Save();rn sv1.save();rn this.Close(); rn rn rnrn public class Load : Dtrn rn public void load()rn rn ds.ReadXml("d:\\2.xml");rn rn rn rn rn public class Dtrn rn public DataTable dt = new DataTable("a");rn public DataSet ds = new DataSet();rn public void DT()rn rn dt.Columns.Add("编号",typeof(int));rn dt.Columns.Add("日期",typeof(string));rn dt.Columns.Add("事件",typeof(string));rn dt.Columns.Add("是否完成",typeof(bool));rn rn public void Link()rn rn 编辑记事 bj = new 编辑记事();rn bj.dataGrid1.DataSource = dt;rn rn rnrn public class Save : Dtrn rn public void save()rn rn ds.Tables.Add(dt);rn ds.WriteXml("d:\\2.xml");rn rn rnrnrn运行时出现未处理的“System.StackOverflowException”类型的异常出现在 mscorlib.dll 中。的这个异常!现在非常苦恼,请各位指点! 论坛

没有更多推荐了,返回首页