小编Dip*_*Dip的帖子

在C ++资源中不经常提到堆栈和堆是有特定原因的吗?

在诸如“坚果壳中的C#”之类的书中,在堆栈和堆上分配的内容很快就被介绍了。但是,诸如“使用C ++编程原理和实践”之类的C ++资源,该标准以及cppreference.com都从未提及有关内存分配的堆栈或堆-即使当他们谈论存储持续时间/类时也是如此。为什么是这样?它是特定于实现的吗,所有编程语言之间在堆栈和堆的使用方面分配的是相同的?如果后者是正确的,我会理解缺乏对不同实体分配位置的覆盖。

c++ heap stack memory-management

8
推荐指数
1
解决办法
324
查看次数

使用全局变量或几个参数?

是否最好使用全局变量,例如:

int bumNum;
int extraNum;
bool isIt;

void setup()
{
    std::cin >> bumNum;
    std::cin >> extraNum;
    isIt= false;
}

void anotherFunc()
{
    //do something with the global variables
}

int main()
{
    setup();
    anotherFunc();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

或局部变量,例如:

void setup(int& bumNum, int& extraNum, bool& isIt)
{
    std::cin >> bumNum;
    std::cin >> extraNum;
    isIt= false;
}

void anotherFunc(int& bumNum, int& extraNum, bool& isIt)
{
    //do something with the global variables
}

int main()
{
    int bumNum;
    int extraNum;
    bool isIt;
    setup(bumNum, …
Run Code Online (Sandbox Code Playgroud)

c++

-4
推荐指数
1
解决办法
81
查看次数

标签 统计

c++ ×2

heap ×1

memory-management ×1

stack ×1