常用的指针? [英] Common Uses For Pointers?

查看:110
本文介绍了常用的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个在C#中有两个类的编程学生,但我只是在C ++中学习第一门课,因此我接触到指针。

I'm a programming student with two classes in C#, but I'm just taking my first class in C++, and thus I'm being exposed to pointers.

我知道它们是如何工作的,以及使用它们的正确方法,但我想知道专业程序员在程序中使用指针的一些方法。

I know how they work, and the proper way to use them, but I wondered about some of the ways that professional programmers use pointers in their programs.

你使用指针吗?还是你?

So how do you use pointers? Or do you?

这将帮助我理解指针的一些实际应用,所以感谢!

This will help me understand some practical applications for pointers, so thanks!

推荐答案

任何时候你可以在C#中使用引用。 参考只是一个指针,它周围有很多安全气囊。

Any time you'd use a reference in C#. A "reference" is just a pointer with fancy safety airbags around it.

我在我写的C ++代码中每六行使用一次指针。在我的头顶,这些是最常见的用途:

I use pointers about once every six lines in the C++ code that I write. Off the top of my head, these are the most common uses:


  • 当我需要动态创建一个对象的生命周期超过

  • 当我需要分配一个在编译时未知大小的对象时。

  • 对象从一个事物到另一个,而不实际复制它(像在链表/堆/任何真正大,昂贵的结构)

  • 当我需要引用两个不同的对象地方。

  • 当我需要直接写入特定区域的内存时(因为它有内存)已映射的IO)。

  • When I need to dynamically create an object whose lifetime exceeds the scope in which it was created.
  • When I need to allocate an object whose size is unknown at compile time.
  • When I need to transfer ownership of an object from one thing to another without actually copying it (like in a linked list/heap/whatever of really big, expensive structs)
  • When I need to refer to the same object from two different places.
  • When I need to slice an array without copying it.
  • When I need to write directly to a specific region of memory (because it has memory-mapped IO).

这篇关于常用的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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