11 Replies - 220 Views - Last Post: 26 January 2013 - 09:31 AM Rate Topic: -----

#1 STaylorY  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-January 13

Array causing Stack Overflow

Posted 26 January 2013 - 07:45 AM

Hey guys, I'm trying to create an array of integers with the size of 2,000,000. The program builds with no errors or warnings, however when I run it, it crashes. When I debug it I get a message saying Stack Overflow. After some time looking around in the debugger I narrowed down the source of my problem to the array. I understand this is a large array I'm trying to create, is there anyway around this or am I doing something wrong. The actual code I'm using to create the array is:

int arr[2000000];


Before anyone asks, yes it has to be an array. Would it be better if I created the array dynamically instead?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Array causing Stack Overflow

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Array causing Stack Overflow

Posted 26 January 2013 - 08:00 AM

You need to either increase the size of your stack or declare the array on the heap - make it global. Since this array is likely to be an important part of your project it's probably ok to make it a global, but I'd give it a descriptive name that will not likely be confused with something else.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: Array causing Stack Overflow

Posted 26 January 2013 - 08:02 AM

Quote

Before anyone asks, yes it has to be an array. Would it be better if I created the array dynamically instead?

Yes, with an array of that size you need to use heap memory instead of stack. There are other ways but using dynamic memory is, in my opinion, the way to go.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 STaylorY  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-January 13

Re: Array causing Stack Overflow

Posted 26 January 2013 - 08:04 AM

Thanks for the quick replies! I'll put it on the heap then.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: Array causing Stack Overflow

Posted 26 January 2013 - 08:07 AM

Hopefully using dynamic memory, instead of the global.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Array causing Stack Overflow

Posted 26 January 2013 - 08:20 AM

I don't see the advantage of dynamic allocation for something like this. An array that size is unlikely to need to be freed before the program ends, and dynamic allocation will put it on the heap just as a global will. Using dynamic allocation will require the array be passed to functions and it's likely to be needed in many of them. By making it global you can treat it like a #defined constant and use it anywhere. As long as you've named it properly there shouldn't be any confusion with local variables.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: Array causing Stack Overflow

Posted 26 January 2013 - 08:46 AM

And what is wrong with passing an array into the functions that require this array? By keeping the array local and properly passing it to only the functions that require the array will make troubleshooting problems much easier, in my opinion. Keeping variables as local a possible is usually considered the best approach.

Jim
Was This Post Helpful? 1
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Array causing Stack Overflow

Posted 26 January 2013 - 08:50 AM

Usually, but when a variable is going to be the center of attention throughout the project (as this one seems likely to be) I don't think it's necessary.
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: Array causing Stack Overflow

Posted 26 January 2013 - 08:54 AM

How can you possibly tell that this string is going to be the center of attention? There has been no actual code provided. Why do you think passing a variable to many different functions as a parameter is bad?

Jim
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Array causing Stack Overflow

Posted 26 January 2013 - 09:00 AM

Oh, I could be wrong, but in my experience a very large array is usually the focal point of the project. It certainly is with our projects.

Passing a variable isn't bad. It's usually good. It's also just another thing to do and generally speaking, less code is better.
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: Array causing Stack Overflow

Posted 26 January 2013 - 09:11 AM

But I beleive the passing of the variable with the proper use of "const" in functions that shouldn't modify the array is benificial. Also normally when I see someone using such a large array my first thought is do they really need that large array or is there a better way to accomplish the objective, but then I spent many years dealing with resourse constrained systems where memory was scarce.

Jim
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Array causing Stack Overflow

Posted 26 January 2013 - 09:31 AM

I used to think the same way and then I started working on models of diseases. Here's an array we use: Comp[status][sex][act][age][alc][vl][cd4][res]
The indices have these possible values:
status: 8
sex: 2
age: 10
act: 4
alc: 2
vl: 5
cd4: 5
res: 8


That's 256,000 elements, and each element represents some number of people of that particular disease status, sex, age group, sexual activity level, alcohol status, viral load level, CD4 level, and virus resistance type. That array gets used everywhere, and when it does get used we usually have to iterate through the entire array. Making it global simplifies dealing with it. I'm not saying to use globals all the time. I'm just saying there are times when it is the best solution.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1