c++ - 所有指针都是放在栈中吗
怪我咯
怪我咯 2017-04-17 14:34:55
0
4
821

所有指针都是放在栈中吗,比如函数内局部指针变量、指向动态申请的对象的局部指针变量?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(4)
洪涛

What you said函数内局部指针变量、指向动态申请的对象的局部指针变量

int test = 0; //全局静态存储区
int* ptrTest = &test; // 这个指针自己也是在全局静态存储区上,指向的内存地址也是。

int main()
{
    int a = 2;//栈上
    int* ptr = &a;//ptr是在栈上,然后它指向的内存地址也是栈上的

    int* ptrHeap = new int(2);
    // new是在堆上申请的内存,返回的是这个申请的内存的地址
    // 然后ptrHeap是指向这个地址的,但是ptrHeap它自己是占的栈上的地址。
    delete ptrHeap;

    return 0;
}

The pointer itself occupies memory space, and then it points to other memory spaces.

刘奇

Yes, pointing to the heap address

Peter_Zhu

Function pointer variables themselves are not all stack variables.

Example:

int ** p = new int*;

For this example, p is a pointer to a pointer, p is also a pointer, and p points to a memory block that stores an int value. However, the memory that stores the pointer variable *p itself is the heap.

It can be seen that pointer variables are the same as ordinary variables, they can be stack variables or heap variables.

小葫芦

Definitely not. Pointers are also variables, no different from other variables. Think about vector<int*>

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!