0

I was working on a project for my university and I discovered something strange.

The declaration of an array of N elements (of type long long int), where N is input from the user, throws segmentation fault for large numbers. On the contrary, the declaration of a vector of the same number works like a charm.

Declarations:

vector<long long int> A(N);

versus

long long int A[N];

I assume there is a difference in the way the information is stored in the memory, but correct me if I am wrong, I think that both arrays and vectors store their elements sequentially in order to achieve O(1) access to their elements. So what's the real difference between the two and what causes the segmentation fault?

gdaras
  • 9,401
  • 2
  • 23
  • 39
  • How big was `N` that you saw a segfault? –  Apr 02 '18 at 21:20
  • By the way, variable length arrays like `long long int A[N];` are not standard features, but an extension provided by your compiler. You should use vectors instead. – eesiraed Apr 02 '18 at 21:36

0 Answers0