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)

Community
  • 1
  • 1
  • 6
    That's a lot of data for the poor stack. – chris Sep 19 '12 at 04:16
  • Indeed, allocate on the heap instead (with `malloc` or `new[]`) – Keith Randall Sep 19 '12 at 04:16
  • @KeithRandall, `std::vector genotype(150000000);` – chris Sep 19 '12 at 04:17
  • @GregHewgill yes seems like ... but how a new user can find it with different title – NullPoiиteя Sep 19 '12 at 04: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. – Greg Hewgill Sep 19 '12 at 04:30
  • @GregHewgill true words ... should be closed with duplicate link ..not delete – NullPoiиteя Sep 19 '12 at 04:34

4 Answers4

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();
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
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.

Aesthete
  • 18,622
  • 6
  • 36
  • 45
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);
}
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
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.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571