You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++自定义mystack类构造器sizeofstack值异常及输出不符求助

C++栈类实现的问题排查与修复

问题概述

尝试实现一个名为mystack的栈类,使用top变量记录栈顶位置,构造函数接收栈大小参数,但运行结果不符合预期,核心问题是构造函数中sizeofstack值出现莫名变化,且存在数组越界导致的未定义行为。

原实现代码

#include <iostream>

using namespace std;

class mystack
{
    int top;
    int arr[5];
    int sizeofstack;

public:
    mystack(int j = 5)
    {
        arr[j];
        top = -1;
        // cout<<"constructor sizeofstack1:"<<sizeofstack<<endl;
        for (int i = 0; i < sizeofstack; i++)
        {
            cout << "sizeofstack inside the constructor loop1 " << i << ":" << sizeofstack << endl;
            arr[i] = 0;
            cout << "sizeofstack inside the constructor loop2 " << i << ":" << sizeofstack << endl;
        }
        sizeofstack = j;
        cout << "constructor sizeofstack2:" << sizeofstack << endl
             << "constructor j:" << j << endl;
    }

    bool isEmpty()
    {
        if (top == -1)
            return true;
        else
        {
            return false;
        }
    }

    bool isFull()
    {
        if (top == (sizeofstack - 1))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    int size()
    {
        cout << "The size of stack is " << sizeofstack << endl;
        return sizeofstack;
    }

    void push(int val)
    {
        cout << "value:" << top << endl
             << "sizeofstack:" << sizeofstack << endl;
        if (isFull())
        {
            cout << "stack overflow" << endl;
        }
        else
        {
            top++;
            arr[top] = val;
        }
    }

    void pop()
    {
        if (isEmpty())
        {
            cout << "stack underflow" << endl;
        }
        else
        {
            arr[top] = 0;
            top--;
        }
    }

    int display()
    {
        if (isEmpty())
        {
            cout << "stack underflow" << endl;
            return -1;
        }
        else
        {
            for (int i = (sizeofstack - 1); i >= 0; i--)
            {

                cout << i << ":" << arr[i] << endl;
            }
            return 0;
        }
    }

    void pos(int i)
    {
        cout << arr[i] << endl;
    }
};

int main()
{
    mystack s1(9);
    s1.push(20);
    s1.display();
    return 0;
}

运行输出

sizeofstack inside the constructor loop1 0:8
sizeofstack inside the constructor loop2 0:8
sizeofstack inside the constructor loop1 1:8
sizeofstack inside the constructor loop2 1:8
sizeofstack inside the constructor loop1 2:8
sizeofstack inside the constructor loop2 2:8
sizeofstack inside the constructor loop1 3:8
sizeofstack inside the constructor loop2 3:8
sizeofstack inside the constructor loop1 4:8
sizeofstack inside the constructor loop2 4:8
sizeofstack inside the constructor loop1 5:8
sizeofstack inside the constructor loop2 5:0
constructor sizeofstack2:9
constructor j:9
value:-1
sizeofstack:9
8:0
7:7935504
6:0
5:9
4:0
3:0
2:0
1:0
0:20 

核心问题分析

1. 未初始化成员变量导致的未定义行为

sizeofstack是类的成员变量,在构造函数中先使用后赋值:循环判断i < sizeofstack时,sizeofstack还未被赋值,此时它的值是内存中的随机垃圾值(输出中显示为8),导致循环次数完全不可控。

2. 数组越界破坏内存

  • 类中定义的arr是固定大小为5的数组,但构造函数传入了9,后续display函数循环到索引8,直接访问了数组范围外的内存,这会覆盖其他成员变量的内存(比如sizeofstack被修改为0),引发未定义行为。
  • 构造函数中的arr[j];是无效代码,仅访问数组元素但未做任何操作,完全多余。

3. 构造函数赋值顺序错误

构造函数中先执行数组初始化循环,再给sizeofstack赋值,完全搞反了逻辑,应该先初始化sizeofstack,再用它控制循环。

修复方案

修复后的构造函数

mystack(int j = 5)
{
    // 先初始化sizeofstack,避免使用未定义值
    sizeofstack = j;
    top = -1;
    
    // 限制栈大小不超过数组的实际容量(5),避免越界
    if (sizeofstack > 5) {
        cout << "栈大小超过数组最大容量,自动调整为5" << endl;
        sizeofstack = 5;
    }
    
    // 初始化数组元素为0
    for (int i = 0; i < sizeofstack; i++)
    {
        arr[i] = 0;
    }
    
    cout << "constructor sizeofstack:" << sizeofstack << endl
         << "constructor j:" << j << endl;
}

进一步优化建议

  • 支持任意大小栈:如果需要动态调整栈大小,不要用固定数组,改用动态分配的数组:
    class mystack {
        int top;
        int* arr; // 动态数组指针
        int sizeofstack;
    public:
        mystack(int j = 5) : top(-1), sizeofstack(j) {
            arr = new int[sizeofstack]; // 分配内存
            for (int i = 0; i < sizeofstack; i++) {
                arr[i] = 0;
            }
        }
        // 必须添加析构函数释放内存
        ~mystack() {
            delete[] arr;
        }
        // 其他成员函数...
    };
    
  • 使用初始化列表:成员变量尽量在构造函数初始化列表中初始化,更符合C++规范:
    mystack(int j = 5) : top(-1), sizeofstack(j) {
        // 初始化代码
    }
    
  • 增加参数校验:构造函数中判断j是否为正数,避免无效的栈大小。

内容的提问来源于stack exchange,提问作者Dushyanth Yadav v

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 12:35:56