在功能块中使用新运算符时在堆上还是堆栈上生存?

问题描述 投票:-1回答:1

例如:

void Func()
{
    int* i = new int; // the simplest case

    vector<int*> v = new vector<int*>; // another case, not sure if **new** is using correctly here
    vector<int*>* vPointer = new vector<int*>; 
}

void main()
{
    Func();
}

因此,如果我通过使用新的运算符为函数中的局部变量分配动态内存,则>

  1. 它们生活在堆还是堆栈上?
  2. 当程序退出函数时,它们仍然悬在堆上还是被破坏为函数变量?
  3. 我可以在非指针类型上使用新运算符吗?以后如何删除
  4. (或返回分配的堆内存)?

    谢谢!

例如:void Func(){int * i = new int; //最简单的情况vector v = new vector ; //另一种情况,不确定** new **是否在这里正确使用vector <...>

c++ pointers stack heap new-operator
1个回答
0
投票
int i = 3;
© www.soinside.com 2019 - 2024. All rights reserved.