指针到底存储什么? (C ++) [英] What exactly do pointers store? (C++)

查看:329
本文介绍了指针到底存储什么? (C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道指针存储它们指向的值的地址,但是如果直接将指针的值显示在屏幕上,则会得到一个十六进制数.如果数字恰好是指针存储的内容,那么在说

I know that pointers store the address of the value that they point to, but if you display the value of a pointer directly to the screen, you get a hexadecimal number. If the number is exactly what the pointer stores, then when saying

pA = pB; //both are pointers

您正在复制地址.那么在处理int s和bool s之类的非常小的项目时,使用指针会不会有更大的开销?

you're copying the address. Then wouldn't there be a bigger overhead to using pointers when working with very small items like ints and bools?

推荐答案

指针本质上只是一个数字.它将地址存储在数据所在的RAM中.指针本身很小(在32位体系结构上,其大小可能与int相同,在64位体系结构上,其大小与long相同).

A pointer is essentially just a number. It stores the address in RAM where the data is. The pointer itself is pretty small (probably the same size as an int on 32 bit architectures, long on 64 bit).

您是正确的,尽管使用intint *不会节省任何空间.但这不是重点(无双关语).指针在那里,因此您可以对事物进行引用,而不仅仅是自己使用事物.

You are correct though that an int * would not save any space when working with ints. But that is not the point (no pun intended). Pointers are there so you can have references to things, not just use the things themselves.

这篇关于指针到底存储什么? (C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆