5

I have recently migrated to a new laptop - HP dv6119tx (Intel Core i5, 4 GB RAM). It has Windows 7 Home Premium 64 bit installed.

I am trying to create an array of type int of length 10^6 in C++ (Dev C++), which I used to create comfortably on my last laptop (32 bit Windows 7 Ultimate/Ubuntu Linux, 2GB RAM) and every other environment I have programmed on (It should take around 3.5 MB of RAM). But with the current setup, I am getting a "Segmentation Fault" error in Debug Mode.

Screenshot when I am trying to create an array of length 10^5

Screenshot when I am trying to create an array of length 10^6

SCREENSHOTS (EDIT) :
The first screenshot shows 10^5 working on the current setup and 10^6 not. I do not have a screenshot for 10^6 working on my last machine but I have used it many times.

EDIT:
The program would work just fine if I declared the array as global instead or created it dynamically on the heap as

int* a = new int[MAX];  

But what I fail to understand is that when the local array is taking a meager 3.5 MB of memory on the stack (and was working fine on a 2 GB machine), why should this issue surface with a 4GB machine? Is this a user stack space issue? Can it be increased manually?

EDIT 2:
I am particularly asking this question because I have submitted numerous solutions on SPOJ with 10^6 sized arrays created on the stack. With my current setup, I feel crippled not being able to do that. I prefer stack over heap whenever possible because it has no memory leak issues; and local variables over global variables because they are neat and do not mess up the namespace.

11
  • 2
    This is a great place for this question. Commented Sep 24, 2011 at 19:45
  • 1
    @RaymondChen: count again, there are six zeros. Commented Sep 24, 2011 at 19:45
  • 1
    You can increase the stack size but don't. You just tie up a bunch of memory for no good reason. Don't abuse the stack. Put large objects on the heap. Commented Sep 24, 2011 at 19:46
  • 1
    @Raymond You can delete a comment for good. Hover the cursor over the very right hand end of the comment (after the time) and you can see a little cross appear. Commented Sep 24, 2011 at 19:47
  • 1
    The amount of stack space is set at link time, IIRC -- at which point the compiler and linker and such don't know or care which machine your code will run on, or how much memory it has. Unless you tell the linker and/or compiler to set a bigger stack, you'll get whatever the default is. Commented Sep 24, 2011 at 19:52

3 Answers 3

9

A four megabyte stack is pretty big. The default size on Windows is 1MB. You need to use the /STACK option to the linker to ask for a larger size.

Sign up to request clarification or add additional context in comments.

2 Comments

The stack is limited in size by design. You can increase the stack size, but for such large objects it's probably better to find a different way to allocate them.
True, in general. I'm using a hands-off library in one of my projects right now, though, that needs a 2MB stack to initialize properly, hence my mindset is that sometimes this is necessary.
8

Arrays created like that are stored on the stack. However, the stack has very limited size, therefore you are hitting a stackoverflow and a crash.

The way to do it is to allocate it on the heap:

int *a = (int*)malloc(MAX * sizeof(int));

//  Do what you need to do.

//  Free it when you're done using "a".
free(a);

8 Comments

Thanks, I just edited the question to include this viewpoint. Can you suggest now?
Yes, the user stack space is (by default) very small. When you declare a local array int a[MAX], it is placed on the stack. So a large array can overrun the stack.
Just a note: VLAs are a C99 Feature. Also these code lines may end up writing in memory they should not, and even try to free something that never really existed in case malloc fails. add an if(a){ ... }. The cast is not needed in case it is C.
@Muggen: Agreed, though it's not a VLA since MAX is a constant. Also the question is tagged both C and C++, so I gave a solution that would work in both.
@Mysticial, oops correct about the macro. I understand the problem about both tags.
|
2

Instead of malloc'ating memory you can specify your buffer as static. E.g.:

static int a[MAX];

Advantage of this approach is that you need not to track your memory allocation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.