我知道这是一个非常基本的问题,但在用高级语言编写了几个项目之后,我刚刚开始了一些基本的c++编程。
基本上我有三个问题:
为什么使用指针而不是普通变量? 我应该在何时何地使用指针? 如何在数组中使用指针?
我知道这是一个非常基本的问题,但在用高级语言编写了几个项目之后,我刚刚开始了一些基本的c++编程。
基本上我有三个问题:
为什么使用指针而不是普通变量? 我应该在何时何地使用指针? 如何在数组中使用指针?
当前回答
使用指针的一个原因是可以在被调用的函数中修改变量或对象。
在c++中,使用引用比使用指针更好。虽然引用本质上是指针,但c++在某种程度上隐藏了这一事实,让它看起来好像是通过值传递的。这使得更改调用函数接收值的方式变得很容易,而不必修改传递值的语义。
考虑以下例子:
使用引用:
public void doSomething()
{
int i = 10;
doSomethingElse(i); // passes i by references since doSomethingElse() receives it
// by reference, but the syntax makes it appear as if i is passed
// by value
}
public void doSomethingElse(int& i) // receives i as a reference
{
cout << i << endl;
}
使用指针:
public void doSomething()
{
int i = 10;
doSomethingElse(&i);
}
public void doSomethingElse(int* i)
{
cout << *i << endl;
}
其他回答
使用指针而不是变量的一种方法是消除所需的重复内存。例如,如果您有某个大型复杂对象,您可以使用指针为您所做的每次引用指向该变量。对于变量,您需要为每个副本复制内存。
这里描述了C语言中指针的需求
其基本思想是,语言中的许多限制(如使用数组、字符串和修改函数中的多个变量)可以通过操纵数据的内存位置来消除。为了克服这些限制,C语言中引入了指针。
此外,还可以看到,使用指针,您可以更快地运行代码,并在将大数据类型(如具有许多字段的结构)传递给函数时节省内存。在传递之前复制此类数据类型将花费时间并消耗内存。这是程序员更喜欢大数据类型指针的另一个原因。
PS:请参考提供的详细解释和示例代码链接。
指针是获得对另一个变量的间接引用的一种方式。它们不是保存变量的值,而是告诉您变量的地址。这在处理数组时特别有用,因为使用指向数组中第一个元素(它的地址)的指针,可以通过递增指针(指向下一个地址位置)快速找到下一个元素。
我读过的关于指针和指针算术的最好解释是在K & R的《C编程语言》中。开始学习c++的一本好书是c++ Primer。
因为到处复制大对象会浪费时间和内存。
关于你的第二个问题,通常你在编程时不需要使用指针,但是有一个例外,那就是当你创建一个公共API时。
The problem with C++ constructs that people generally use to replace pointers are very dependent on the toolset that you use which is fine when you have all the control you need over the source code, however if you compile a static library with visual studio 2008 for instance and try to use it in a visual studio 2010 you will get a ton of linker errors because the new project is linked with a newer version of STL which is not backwards compatible. Things get even nastier if you compile a DLL and give an import library that people use in a different toolset because in that case your program will crash sooner or later for no apparent reason.
因此,为了将大型数据集从一个库移动到另一个库,如果你不想强迫其他人使用与你使用的相同工具,你可以考虑给一个指向应该复制数据的函数的数组指针。这样做的好处是它甚至不必是一个c风格的数组,你可以使用std::vector并通过给出第一个元素&向量[0]的地址来给出指针,并使用std::vector在内部管理数组。
Another good reason to use pointers in C++ again relates to libraries, consider having a dll that cannot be loaded when your program runs, so if you use an import library then the dependency isn't satisfied and the program crashes. This is the case for instance when you give a public api in a dll alongside your application and you want to access it from other applications. In this case in order to use the API you need to load the dll from its' location (usually it's in a registry key) and then you need to use a function pointer to be able to call functions inside the DLL. Sometimes the people that make the API are nice enough to give you a .h file that contain helper functions to automate this process and give you all the function pointers that you need, but if not you can use LoadLibrary and GetProcAddress on windows and dlopen and dlsym on unix to get them (considering that you know the entire signature of the function).