search

Home  >  Q&A  >  body text

C++创建二维指针问题

int n = 0;
int **num = new int*[n];
cin >> n;

上述的代码,Line2和Line3我弄反了,为什么结果在后面进行二维数组操作的时候没有影响?

for (int i = 0; i<n; i++) {
        num[i] = new int[i+1];
        for (int j = 0; j < i+1; j++) {
            cin >> num[i][j];
        }
    }

比如我上述操作之后,我还是可以对num[][]进行读写。new的性质到底是怎样的?

伊谢尔伦伊谢尔伦2804 days ago299

reply all(1)I'll reply

  • 黄舟

    黄舟2017-04-17 13:31:16

    C and C++ do not detect out-of-bounds arrays of basic types. You can play around with pointers, but doing so is dangerous

    reply
    0
  • Cancelreply