0

I am trying to create a large size array(about 80000*80000)

I tried

int *bigarray = new int[80000*80000];

but it gave me the error

terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc

And the thing I want to do is to create a social network's edge matrix.

Can anyone tell me how to create a large size array or any other methods to solve it?

Thanks!

Hung Wei Chun
  • 145
  • 1
  • 1
  • 9
  • 2
    6 billion (Giga) int(s) are 51 Gigabytes (X8) of RAM on Win X64. – Amit G. Aug 19 '18 at 19:32
  • 6
    Look up Sparse Matrix libraries. – Richard Critten Aug 19 '18 at 19:34
  • @AmitG. So what? You can certainly put that much of RAM (and more) into a single motherboard, it's not that exotic these days. – zett42 Aug 19 '18 at 19:41
  • @zett42 It's not exotic, it's expensive. & you will be unable to run you target on other computers, because most of them have 32GB at the most. It might be possible to manage the situation in a more common way, that can be run on any mid-range computer. – Amit G. Aug 19 '18 at 21:21

1 Answers1

9

You can stop the overflow warning by not using int, use unsigned long long - add ULL to the ends of your literal numbers:

auto arr = new int[80000ULL * 80000ULL];

I would recommend using a std::vector rather than a raw dynamic array:

auto vec = std::vector<int>(80000ULL * 80000ULL);

Your std::bad_alloc means you don't have enough memory....

And the thing I want to do is to create a social network's edge matrix.

You can look into using a "sparse matrix". If most of the locations in your array are empty you can use a data structure that records only the occupied cells' values.

A crude example would be to use a std::map<std::size_t, std::map<std::size_t, int>> but there are some dedicated libraries out there that will do a better job.

For example the Eigen Library. (props @yar)

Galik
  • 47,303
  • 4
  • 80
  • 117