Computational Science Stack Exchange is a question and answer site for scientists using computers to solve scientific problems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I want to use large array say mpz_t A[100000], I got "Segmentation fault (core dumped)" during my compilation. Is there any easier way to solve this?

share|improve this question
Have you tried allocating it dynamically instead of statically? Sometimes static allocations go on the stack while dynamic goes on the heap. Alternatively, you can change your stack size and see if that fixes the problem. Without knowing what system you are on, I can't tell you how to do that though. – tpg2114 Oct 31 '12 at 0:07
up vote 5 down vote accepted

tpg2114's comment is spot on. Try:

/* at top */
#include <stdlib.h>

/* definition */
mpz_t *A;

/* initialization of A */
A = (mpz_t *) malloc(100000 * sizeof(mpz_t));
if (NULL == A) {
    printf("ERROR: Out of memory\n");
    return 1;

/* no longer need A */

If the malloc call here triggers an error, you don't have enough memory available in your system.

If you're interested in using a static array on the stack, then you can try increasing the stack limit size in Linux with the ulimit command.

share|improve this answer
Thanks a lot. During my compilation I got the following warning " incompatible implicit declaration of built-in function ‘malloc’ [enabled by default]". However it works. – user12290 Oct 31 '12 at 0:45
@user12290 great, I've fixed the code sample to properly include stdlib.h – Aron Ahmadia Oct 31 '12 at 1:00
Thanks a lot. It works fine now. – user12290 Oct 31 '12 at 2:06
For the love of all that's good and right in this world, put a free(A) in that example! – Bill Barth Oct 31 '12 at 3:01
Fair enough, fixed. – Aron Ahmadia Oct 31 '12 at 12:10

You might also consider using mpz_class in C++ (ref.) rather than mpz_t. It can make arbitrary precision arithmetic straightforward.

Here's a random example:

#include <gmp.h>
#include <gmpxx.h>
#include <iostream>
using namespace std;

int main() {
  mpz_class A[100000];
  for(int i=0;i<100000;i++) A[i]=142412+i;

  for(int i=0;i<100000;i++) cout << i << " " << A[i] << endl;

  return 0;

Compiled with e.g.:

g++ [[filename]] -lgmp -lgmpxx
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.