有什么不同?指向数组的指针VS规则排列 [英] Whats the difference? Pointer to an array vs regular array

查看:141
本文介绍了有什么不同?指向数组的指针VS规则排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我熟悉的Java,并试图教自己的C / C ++。我偷了一些课程,从承载它们的材料这里类。我很遗憾,因为我在班上我不是不能问老师。我关心的是动态数组声明下的部分:

I'm familiar with Java and trying to teach myself C/C++. I'm stealing some curriculum from a class that is hosting their materials here. I unfortunately can't ask the teacher since I'm not in the class. My concern is with the section under "dynamically declared arrays":

如果您
  希望能够改变的大小
  阵列在运行时,然后声明
  动态数组。这些与完成
  指针和新的运营商。为了
  在指针基础知识,读指针
  部分。

If you want to be able to alter the size of your array at run time, then declare dynamic arrays. These are done with pointers and the new operator. For the basics on pointers, read the pointers section.

使用新的分配内存,然后
  访问阵列中相同的方式
  你将一个静态数组。例如,

Allocate memory using new, and then you access the array in the same way you would a static array. For example,

为int * arrayPtr = INT新[10];对于
  (INT I = 0;我小于10;我++){
        arrayPtr [我] =我; }

int* arrayPtr = new int[10]; for (int i = 0; i < 10; i++) { arrayPtr[i] = i; }

的存储器画面是相同的
  静态数组,但你可以改变的
  大小,如果你需要。别忘了你
  必须释放内存前
  分配新的内存(或者你会
  有内存泄漏)。

The memory picture is identical to the static array, but you can change the size if you need to. Don't forget you must deallocate the memory before allocating new memory (or you will have a memory leak).

删除[] arrayPtr; //按[]
  删除指针数组时需要
  arrayPtr = INT新[50]; 。 。

delete [] arrayPtr; // the [] is needed when deleting array pointers arrayPtr = new int[50]; . . .

当你完全的完成
  数组,你必须删除其内存:

When you're completely done with the array, you must delete its memory:

删除[] arrayPtr;

delete [] arrayPtr;

动态多维数组
  以类似的方式对Java完成。您
  将有指针的指针。为
  例如,看到一个

Dynamic multi-dimensional arrays are done in a similar manner to Java. You will have pointers to pointers. For an example, see a

我的理解是,在C数组仅仅是数组中的第一个元素的内存地址的引用。

My understanding is that an array in C is simply a reference to the memory address of the first element in the array.

那么,是什么为int * pointerArray =新的INT [10]之间的区别; int数组[10]; 如果有的话?

So, what is the difference between int *pointerArray = new int[10]; and int array[10]; if any?

我已经做了一些测试,似乎表明他们做同样的事情。是网站错误还是我读到错了吗?

I've done some tests that seem to indicate that they do the exact same thing. Is the website wrong or did I read that wrong?

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char** argv) {

    // Initialize the pointer array
    int *pointerArray = new int[10];
    for (int i = 0; i < 10; i++){

        pointerArray[i] = i;
    }

    // Initialize the regular array
    int array[10];
    for (int i = 0; i < 10; i++){

        array[i]= i;
    }

    cout << *(pointerArray + 5) << endl;
    cout << *(array + 5) << endl;

    cout << pointerArray[5] << endl;
    cout << array[5] << endl;

    cout << pointerArray << endl;
    cout << array << endl;

    return 0;
}

输出:

5
5
5
5
0x8f94030
0xbfa6a37c

我试着动态调整大小我的指针数组作为在网站上描述的,但我的新的(大)指针数组结束了充满0这是不是非常有用的。

I've tried to "dynamically re-size" my pointer array as described on the site, but my new (bigger) pointer array ends up filled with 0's which is not very useful.

推荐答案

int数组[10]; 静态声明数组的大小,这意味着它是固定的 - 这是唯一的主要区别。它也可能被分配到是函数的栈帧,即程序的堆栈里面。您不必担心使用删除[] 在该类型的数组,其实你可能会崩溃程序,如果你删除了。

int array[10]; declares the array size statically, that means it is fixed - which is the only major difference. It also might be allocated to be inside the function's stack frame, i.e. on the program's stack. You do not need to worry about using delete [] on that kind of array, in fact, you might crash the program if you delete it.

当您使用运营商新的,您分配内存动态可能是的的和内存通常来自的而不是程序的堆栈(尽管并不总是)。这是在大多数情况下更好,因为你是在比堆空间的堆栈空间有限。但是,必须注意的内存泄漏和删除[] 你的东西时,你不需要它了。

When you use operator new, you allocate memory dynamically which could be slower and the memory usually comes from the heap rather than the program's stack (though not always). This is better in most cases, as you are more limited in the stack space than the heap space. However, you must watch out for memory leaks and delete[] your stuff when you don't need it anymore.

至于你的阵列被用零填充,你的类材料不说的是,你必须要做到这一点:

As to your array being filled with zeros, what your class material does not say is that you have to do this:

int *arr = new int[20]; // old array
//do magic here and decide that we need a bigger array
int *bigger = new int[50]; // allocate a bigger array
for (int i = 0; i < 20; i++) bigger[i] = arr[i]; // copy the elements from the old array into the new array
delete[] arr;
arr = bigger;

这code扩展阵列改编 30更多的元素。请注意,您必须将旧数据复制到新的数组,否则它不会出现(在你的情况下,一切都变得0)。

That code extends the array arr by 30 more elements. Note that you must copy the old data into the new array, or else it will not be there (in your case, everything becomes 0).

这篇关于有什么不同?指向数组的指针VS规则排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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