简体   繁体   English

C ++:指针语法之间的区别

[英]C++: Difference between pointer syntaxes

Alright so I've been getting deeply into C++ as of late and I'm getting everything down. 好吧,所以最近我一直在深入C ++,并且一切都变得越来越糟。 Pointers are finally starting to make sense as far as when I should use them, how to implement them correctly, etc. 指针终于开始对我何时应该使用它们,如何正确实现它们等有意义。

However, there was one little question about the fundamental use of pointers that I still had that needed answered. 但是,关于指针的基本用法还有一个小问题,我仍然有需要回答的问题。 I'll jump right to the code: 我将直接跳至代码:

With the following class A and function foo(A* bar) ... 使用以下A类和函数foo(A* bar) ...

class A
{}

void foo(A* bar)
{}

... what's the difference between the following calls to foo ? ...以下对foo调用之间有什么区别?

A* a;
A b;

foo(a);
foo(&b);

They both compile fine, and as far as I can remember I haven't had any issues with them. 它们都可以很好地编译,据我所记得,我对它们没有任何问题。

I think that A b; 我认为A b; is instantiated right there, whereas A* a; 在此处实例化,而A* a; needs to be created with new (since it hasn't actually created the object, it's just held a 4-byte long reference to a potential A object). 需要使用new创建(因为它实际上尚未创建对象,因此它仅保留了对潜在A对象的4字节长的引用)。

I could, if I am thinking about this correctly, do a = b; 如果我考虑正确,我可以做a = b; ( EDIT make that a = &b ) and then successfully pass a to foo . 编辑使a = &b ),然后将a成功传递给foo But, if I don't do a = &b and foo tries to read the (non-existent) object pointed to by a , it will causes runtime errors. 但是,如果我不这样做a = &bfoo尝试读取(不存在)对象通过指着a ,它会导致运行时错误。

Also, if the above is correct, then it's assumed I can successfully call foo(&b); 另外,如果以上正确,则假定我可以成功调用foo(&b); just fine. 正好。

Am I correct? 我对么?

Thanks! 谢谢!

Yes, Your understanding is correct. 是的,您的理解是正确的。

 foo(&b);

passes address of an already existing object of type A as an parameter to function foo() . 将类型A现有对象的地址作为参数传递给函数foo()

foo(a);

passes a pointer to the type A as function parameter. 将指向类型A的指针作为函数参数传递。 To be able to do anything meaningful it must point to a valid A object.It can be done in two ways: 为了能够做任何有意义的事情,它必须指向一个有效的A对象,它可以通过两种方式完成:

Allocating object on stack: 在堆栈上分配对象:
Create an object of the type A on stack(local storage) & make the pointer a point to this object: 创建的类型的对象A堆栈(本地存储器)使指针a指向此目的:

A* a;
A b;

a = &b;

Dynamic Memory allocation: 动态内存分配:

   A *a = new A;

Though, Once you do a dynamic memory allocation you will have to remember to free the alloated memory explicitly after use, or you will have a memory leak: 但是,一旦执行了动态内存分配,您将必须记住在使用后显式释放分配的内存,否则会发生内存泄漏:

delete a;

Note that it is always better to avoid dynamic allocations as far as possible, and if you must do so, use Smart pointers instead of raw pointers. 请注意,最好总是尽可能避免动态分配,如果必须这样做,请使用智能指针而不是原始指针。

You can't do a = b . 你不能做a = b

It would have to be a = &b , to set a to the address of b . 它必须是a = &b ,要设置a地址 b

You are also correct about the memory management: b is allocated on the stack, while a allocates space only for a pointer and leaves creating the actual object to you. 您在内存管理方面也很正确: b是在堆栈上分配的,而a仅为指针分配空间,而将创建实际对象留给您。

foo(&b) will work file, where the behavior of foo(a) would be undefined before you initialize *a (such as via a = new A() ). foo(&b)将工作文件,在初始化*a之前, foo(a)的行为是不确定*a (例如通过a = new A() )。

In C++, pointers are first-class objects. 在C ++中,指针是一流的对象。 A pointer isn't just an invisible reference that needs an associated object to have an identity. 指针不仅仅是需要相关对象具有标识的不可见引用。 That's how Java/C# references work (or most other languages, really), but a pointer is an object in itself. 这就是Java / C#引用(实际上是大多数其他语言)的工作方式,但是指针本身就是对象。

So A* a declares a pointer. 因此A* a声明了一个指针。 It doesn't point to anything, and it doesn't have to point to anything. 它不指向任何东西,它没有指向任何东西。 And if/when it points to something, it doesn't need to own that something. 而且,如果/当它指向某物时,它不需要拥有该物。

So you don't need to do a = new A() . 因此,您无需执行a = new A() You can do a = &b as well (to have a contain the address of the object b . Or it can point to any other object of type A as well. A pointer is just an object that stores an address. It's key to your understanding that you throw away the notion that it "has an object" which "needs to be created". 你可以做a = &b以及(有a包含对象的地址b 。或者,它可以指向的任何其他类型的对象A为好。指针就是存储地址的对象。这是关键,你的理解放弃了“需要创建”的“具有对象”的概念。

It is an object, which contains an address (or it contains the special value null), and if it contains an address, there may or may not be an object of type A at that address. 一个对象,其中包含一个地址(或者它包含特殊值null),如果它包含一个地址,则该地址处可能有也可能没有类型为A的对象。

You are mostly correct. 您基本上是正确的。 You should not assume that pointers are 4 bytes (for example, it might be 8 on amd64 systems). 您不应假定指针为4个字节(例如,在amd64系统上可能为8个字节)。 Also, your assignment should be a = &b; 另外,您的分配应为a = &b; (note the addition of the address operator). (请注意地址运算符的添加)。 Other than that, it sounds pretty reasonable. 除此之外,这听起来很合理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM