Dark Hypernova
Science and Fiction

Today I Learned

Allocating Memory for Multidimensional Array in C++

#include <iostream>
#include <typeinfo>

using namespace std;

int main() {

  int (*a0)[1000] = new int[10000][1000];
  int (*a1)[1000][10] = new int[10000][1000][10];
  int **a2;
  int *a3;
  int *a4 = new int[100];
  // int a5[]; // Commented out because we can not do this
  // a5 = new int[100];




  std::cout << typeid(a0).name() << endl;
  std::cout << typeid(a1).name() << endl;
  std::cout << typeid(a2).name() << endl;
  std::cout << typeid(a3).name() << endl;
  std::cout << typeid(a4).name() << endl;
  // std::cout << typeid(a5).name() << endl;

}

The outputs are

gcc version 4.6.3

PA1000_i
PA1000_A10_i
PPi
Pi
Pi

And multidimensional arrays by default are contiguous in memory.

A Random #TIL# for You

By OctoMiao

Last updated