关于 c++ 中基本指针使用的几个简单问题和 c++记忆模型

发布于 2024-12-02 03:26:33 字数 1301 浏览 6 评论 0原文

我一直在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

已下线请稍等 2024-12-09 03:26:33
  1. array 包含垃圾数据 - 在 array 存在之前该内存位置中的任何内容仍然存在。如果你尝试使用它,你就会搬起石头砸自己的脚,这就是为什么你需要将它分配给一个有效的内存位置(因此随后调用 new[])。
  2. 是的,数组现在包含一个指针(内存地址),指向足以容纳十个双精度数的连续内存块。
  3. *array[i]=2.0 实际上不会编译。 array[i] 产生一个 double,并且您不能对 double 使用取消引用运算符。
  4. 您传递的是数组中第一个元素的地址。因此,您将按值传递指针,并按引用传递数组(因为指针是对数组的引用)。要按值传递数组本身,您必须为每个条目提供一个参数。您还可以复制数组并发送副本,但副本本身也将通过引用传递。
  5. double* someVar = &array[5]; 将返回一个指向数组第 6th 元素的指针。 array[5] 为您提供双精度数,并获取它的地址(使用 &)将为您提供该双精度数的内存地址(指针)。
  1. array contains junk data - whatever was in that memory location before array 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 to new[]).
  2. Yes, array now contains a pointer (memory address) to some contiguous piece of memory big enough to hold ten doubles.
  3. *array[i]=2.0 won't actually compile. array[i] results in a double, and you can't use the dereference operator on a double.
  4. What you're passing is that address to the first element in the array. So you are passing the pointer by value, and the array by reference (as the pointer is a reference to the array.) To pass the array itself by value you'd have to have one parameter for each entry. You could also copy the array and send in the copy, but the copy itself would be passed by reference, too.
  5. 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.
素年丶 2024-12-09 03:26:33
  1. 是的,这
  2. 绝对是正在发生的事情。更具体地说,是指向连续内存块开头的指针。
  3. 不是在这种情况下; * (用于取消引用)是一个一元运算符,但您已向它传递了两个参数。您可以确定执行的是乘法(或其重载版本) - 另外,array[i](*array[i-1]) 意味着什么?你不能取消引用不是指针的东西(或者没有重载一元 * 运算符)。
  4. 你只是按值传递指针,而不是数据。如果您想按值传递数据(使其在函数外保持不变),则必须先复制它,然后传递该数据(或仅使用向量
  5. 是的,您只是获取一部分连续内存的地址,您可以存储该地址并在其他地方修改取消引用的值,数组也会被修改。

另外,请注意,当您在堆上分配时,之后必须删除内存。在这种情况下,您将使用 delete[] array;

  1. Yep, that's what's happening
  2. Most definitely. More specifically, a pointer to the beginning of a contiguous piece of memory.
  3. Not in this case; * (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 could array[i](*array[i-1]) mean? you can't dereference something that isn't a pointer (or doesn't have the unary * operator overloaded)
  4. You're only passing the pointer by value and not the data. If you want to pass the data by value (make it unchanged outside the function), you'd have to copy it first, and pass that (or just use a vector)
  5. Yes, you're just getting the address of a part of contiguous memory, and you can store the address and modify the dereferenced value elsewhere, the array will be modified also.

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;

何以笙箫默 2024-12-09 03:26:33

声明后,array 变量包含一个任意值。你不被允许做任何有这个价值的事情。在new之后,它包含一个指向连续内存范围的指针,该内存范围足够大以容纳10个双精度数。 *array[i]=2.0 是一个错误(这意味着 array 是一个指向 double 的指针数组)。索引运算符[]只是*(array+i)=2.0的语法糖。

第四个问题:说什么?在该代码中的任何位置都没有指向双精度的指针数组。在函数中,void f(double *x) 和 void f(double x[]) 是同一件事:指向 double 的指针。如果将数组传递给 fx 将接收第一个元素的地址(即数组的 VALUE)。

您不能按值传递数组。或者,它们总是按值传递(与 C 中的其他所有内容一样),但请注意数组的 VALUE 是其第一个元素的地址。

你的最后一个问题:我不知道你想要实现什么目标,但这个问题清楚地表明你很困惑。地址就是地址,不存在“十六进制地址”之类的东西。

After declaration, the array variable contains an arbitary value. You're not allowed to do anything with that value. After new, 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) and void f(double x[]) are THE SAME THING: a pointer to double. If you pass to f 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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文