4

Possible Duplicate:
C programming, why does this large array declaration produce a segmentation fault?

I have written a simple program.

#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    int genotype[150000000];
}

But I get a strange error:

RUN FAILED (exit value 1, total time: 131ms)

How can I save this amount of int?

(I have enough memory to save this amount of ints and my computer is 64-bit)

6
  • 6
    That's a lot of data for the poor stack. Commented Sep 19, 2012 at 4:16
  • Indeed, allocate on the heap instead (with malloc or new[]) Commented Sep 19, 2012 at 4:16
  • @KeithRandall, std::vector<int> genotype(150000000); Commented Sep 19, 2012 at 4:17
  • @GregHewgill yes seems like ... but how a new user can find it with different title Commented Sep 19, 2012 at 4:27
  • 2
    @RegisteredUser: Through the close-as-duplicate mechanism, questions using many different wordings can all point to the same question that answers the underlying question. With those duplicates recorded, Google and site searches can help point a user toward an existing good answer using many different search terms. Closing as a duplicate is not an admonishment to the asker, but it's a natural part of the growth of the site. Commented Sep 19, 2012 at 4:30

4 Answers 4

6

Your stack is too small. Put this on the heap, using new:

int* genotype = new int[150000000];

And i hope that following will be useful.

  • signed char: -127 to 127 (note, not -128 to 127; this accommodates 1's-complement platforms)
  • unsigned char: 0 to 255
  • "plain" char: -127 to 127 or 0 to 255 (depends on default char signedness)
  • signed short: -32767 to 32767
  • unsigned short: 0 to 65535
  • signed int: -32767 to 32767
  • unsigned int: 0 to 65535
  • signed long: -2147483647 to 2147483647
  • unsigned long: 0 to 4294967295
  • signed long long: -9223372036854775807 to 9223372036854775807
  • unsigned long long: 0 to 18446744073709551615

Or you can use limit.h to find out around what your program relies on. For example, this is how you will find maximum range for int:

C:

#include <limits.h>
const int min_int = INT_MIN;
const int max_int = INT_MAX;

C++

#include <limits>
const int min_int = std::numeric_limits<int>::min();
const int max_int = std::numeric_limits<int>::max();
Sign up to request clarification or add additional context in comments.

2 Comments

Most of those value ranges aren't guaranteed.
And for all that is holy, delete[] that bad-boy when you're done with it.
4

As a linker option...

/STACK 601048576 //  150000000 * 4 (assume size of int), and 1Mb for general.

But hey.. Don't do this. I'm not even sure what will happen.

Comments

2

You are allocating this on the stack, which is more limited than the heap or static storage.

For C++, a std::vector would be a better option.

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
    vector<int> genotype(150000000);
}

Comments

1

If you want to use dynamic memory simply declare a pointer to the type you want to allocate in this case int, and call the malloc() function with the argument being the number of bytes you want the "array" to be. In this case you need to get the sizeof a single integer and multiply it by how big you want it to be (i.e. the number of cells):

Make sure you free anything you malloc'ed otherwise you will get memory leaks.

1 Comment

Since the question is tagged as both C and C++, you might want to mention the alternative of using new[] and delete[].