关于 c++ 中基本指针使用的几个简单问题和 c++记忆模型
我一直在 iTunes U 上学习斯坦福大学的课程,并且已经掌握了 C++ 的知识。我想我理解指针是如何工作的,但我只想检查如何做一些简单的事情。假设我想创建一个动态数组:
double *array;
此时堆栈中有一个名为“array”的变量,而堆中没有任何内容。第一个问题 - 此时“数组”中存储了什么?指向一些无意义内存的指针?
然后我使用“new”分配内存:
array = new double[10];
第二个问题 - 此时,“数组”中存储了什么?指向足以容纳十个双精度数的连续内存块的指针? (对于简单的问题感到抱歉,但我真的想确保我理解)
我将 double 2.0 分配给数组中的每个元素:
for(int i=0; i<array.length(); i++) array[i]=2.0;
第三个问题 - 这与使用取消引用运算符进行分配有何不同? (即,*array[i]=2.0
)。然后我将数组传递给其他函数:
myFunc(double array[]){
for(int i=1; i<array.length(); i++){
array[i]=array[i]*array[i-1];
}
}
第四个问题 - 在传递给 myFunc 时,由于数组是指向双精度数的指针数组,而不是双精度数数组,因此它通过引用传递而不带“&”,对吗?这意味着我的循环中的操作正在影响存储在“数组”中的实际数据。如果我想按值传递,这样我就不会接触“数组”中的数据怎么办?我会使用
myFunc(double *array[]){...}?
最后一个问题 - 如果我想出于某种原因操纵“数组”内容的内存地址怎么办?我可以使用
someVar = &array[5];
将 array[5] 的十六进制地址分配给 someVar 吗?
我已经阅读了阅读器中关于指针的部分,并且观看了 Binky 视频十几次,但它仍然没有意义。任何帮助将不胜感激。
编辑:非常感谢迄今为止所有回答的人。如果你不介意的话我还有一个问题。在声明 double *array;
中,“array”被声明为指向 double 的指针,但是一旦我使用“new”对其进行赋值,“array”就不再是指向 double 的指针,并且变成双打数组,对吗?
I've been studying along with the Stanford courses on iTunes U and have hit pointers in C++. I think I understand how pointers work, but I just want to check how to do some simple stuff. Let's say I want to create a dynamic array:
double *array;
At this point there's a variable called "array" in the stack and nothing in the heap. First question - what's stored in "array" at this point? A pointer to some nonsense piece of memory?
I then allocate memory using "new":
array = new double[10];
Second question - at this point, what's stored in "array"? A pointer to some contiguous piece of memory big enough to hold ten doubles? (Sorry for the simple questions, but I really want to make sure I understand)
I assign the double 2.0 to each element in the array:
for(int i=0; i<array.length(); i++) array[i]=2.0;
Third question - is this different from using the dereference operator to assign? (i.e., *array[i]=2.0
). I then pass the array to some other function:
myFunc(double array[]){
for(int i=1; i<array.length(); i++){
array[i]=array[i]*array[i-1];
}
}
Fourth question - on the pass to myFunc, since array is an array of pointers to doubles, and not an array of doubles, it passes by reference without "&", right? That means the operations in my loop are affecting the actual data stored in "array". What if I wanted to pass by value, so that I wouldn't be touching the data in "array"? Would I use
myFunc(double *array[]){...}?
Last question - what if I wanted to manipulate the memory addresses for the contents of "array" for some reason? Could i use
someVar = &array[5];
to assign the the hex address of array[5] to someVar?
I've read the section on pointers in the reader and watched the Binky video a dozen times and it still doesn't make sense. Any help would be greatly appreciated.
EDIT: Thanks a lot to everyone who answered so far. If you wouldn't mind I just have one more question. In the declaration double *array;
, "array" is declared as a pointer to a double, but once I use "new" to assign it, "array" ceases being a pointer to a double, and becomes an array of doubles, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
array
包含垃圾数据 - 在array
存在之前该内存位置中的任何内容仍然存在。如果你尝试使用它,你就会搬起石头砸自己的脚,这就是为什么你需要将它分配给一个有效的内存位置(因此随后调用new[]
)。*array[i]=2.0
实际上不会编译。 array[i] 产生一个double
,并且您不能对 double 使用取消引用运算符。double* someVar = &array[5];
将返回一个指向数组第 6th 元素的指针。array[5]
为您提供双精度数,并获取它的地址(使用&
)将为您提供该双精度数的内存地址(指针)。array
contains junk data - whatever was in that memory location beforearray
existed is still there. If you try to play with it you're going to shoot yourself in the foot, which is why you need to assign it to a valid memory location, (hence the ensuing call tonew[]
).array
now contains a pointer (memory address) to some contiguous piece of memory big enough to hold ten doubles.*array[i]=2.0
won't actually compile. array[i] results in adouble
, and you can't use the dereference operator on a double.double* someVar = &array[5];
will return to you a pointer to the 6th element of the array.array[5]
gives you the double, and taking the address of it (with&
) will give you the memory address (pointer) of that double.*
(用于取消引用)是一个一元运算符,但您已向它传递了两个参数。您可以确定执行的是乘法(或其重载版本) - 另外,array[i](*array[i-1])
意味着什么?你不能取消引用不是指针的东西(或者没有重载一元*
运算符)。向量
)另外,请注意,当您在堆上分配时,之后必须删除内存。在这种情况下,您将使用
delete[] array;
*
(for dereference) is a unary operator, and yet you have passed it two arguments. You can be sure it is multiplication that is performed (or an overloaded version of it) - also, what couldarray[i](*array[i-1])
mean? you can't dereference something that isn't a pointer (or doesn't have the unary*
operator overloaded)vector
)Also, be weary that when you allocate on the heap, you have to delete the memory afterwards. In this case, you would use
delete[] array;
声明后,
array
变量包含一个任意值。你不被允许做任何有这个价值的事情。在new
之后,它包含一个指向连续内存范围的指针,该内存范围足够大以容纳10个双精度数。*array[i]=2.0
是一个错误(这意味着 array 是一个指向 double 的指针数组)。索引运算符[]
只是*(array+i)=2.0
的语法糖。第四个问题:说什么?在该代码中的任何位置都没有指向双精度的指针数组。在函数中,void f(double *x) 和 void f(double x[]) 是同一件事:指向 double 的指针。如果将数组传递给
f
,x
将接收第一个元素的地址(即数组的 VALUE)。您不能按值传递数组。或者,它们总是按值传递(与 C 中的其他所有内容一样),但请注意数组的 VALUE 是其第一个元素的地址。
你的最后一个问题:我不知道你想要实现什么目标,但这个问题清楚地表明你很困惑。地址就是地址,不存在“十六进制地址”之类的东西。
After declaration, the
array
variable contains an arbitary value. You're not allowed to do anything with that value. Afternew
, it contains a pointer to a contiguous range of memory large enough to hold 10 doubles.*array[i]=2.0
is an error (that would imply that array is an array of pointers to double). Indexing operator[]
is just a syntactic sugar for*(array+i)=2.0
.Forth question: SAY WHAT?? You don't have an array of pointers to doubles anywhere in that code. In functions,
void f(double *x)
andvoid f(double x[])
are THE SAME THING: a pointer to double. If you pass tof
an array,x
will receive the address of the first element (which is the VALUE of an array).You can't pass arrays by value. Alternatively, they are always passed by value (as everything else in C), but note that the VALUE of an array is the address of its first element.
Your last question: I have no idea what you're trying to achieve, but the question clearly shows that you're confused. An address is an address, there's no such thing as "hex address".