Click to See Complete Forum and Search --> : stack overflow


Daniel Canham
September 4th, 1999, 07:18 PM
Hello,
I'm getting a stack overflow error everytime I try initialize the array below? What am I doing wrong, and how can I make it initialize. I have 300MB of RAM on the system that's initializing it so I don't think the system should be running out of memory? Thanks


CString componants[7000][21];

Rail Jon Rogut
September 4th, 1999, 07:51 PM
Use CStringArray to create an array of CString objects.

To reduce stack usage 1) don't allocate variables on the stack - use new and delete to allocate memory off the heap and 2) pass values by reference to your functions.

Rail

------------
Recording Engineer/Software Developer
Rail Jon Rogut Software
http://home.earthlink.net/~railro/
railro@earthlink.net

ALM
September 4th, 1999, 10:33 PM
Let me get this straight: you need to allocate a huge 2 dimensional array of CStrings on the stack?! Why? This means that everytime you call this function the system has to allocate several megabytes of memory on the stack. Out of curiosity, how many times are you calling this function?

Well, to solve your problem, I have a couple of simple solutions for you:

1. Don't put this variable on the stack. Make it a static (or global) variable instead. This means that it will be instanciated and initialized only once (without using the thread's stack).

2. If you really need to put this on the stack, go to your Project/Settings/Link tab. In the Output category set the "Stack Allocations/Reserve" to several megabytes.

That's it, good luck!
Alvaro

Sef
September 4th, 1999, 10:59 PM
You had it right at the outset. Ah, let me see, a 7000 x 21 array of CStrings on the stack. Only the inexperienced would ever allocate 147,000 of anything (in most cases). It's up to the experienced to set them straight. You don't do that by questioning the wisdom of their decision and then pass them the rope to hang themself.

Daniel Canham
September 4th, 1999, 11:42 PM
Basically this gets instanced once. It holds a dataset from a SQL server table that gets accessed frequently. I have one container class for this project that contains several public arrays this is 1 of 5 that are getting loaded in the same fashion when the app starts up. This app deals with approx 50 Million records and will end up being 500,000 lines of source code +-.
When you say make it public, it already is? It is initialized in the container class's header in the public section. What would be the syntax for making it global??

ALM
September 7th, 1999, 07:31 AM
I didn't say anything about making it public. I said to make it static or global.

And in this case I believe you should make it static. In your header file put static in front of your array's declaration and then in the cpp initialize it, like this:

CString CWhateverClass::componants[][21];


If instead you wanted to make it global (which you don't need to), you'd declare it like this in your cpp:

CString componants[7000][21];

and then in your header you'd put:

extern CString componants[][21];

Cheers!
Alvaro

ALM
September 7th, 1999, 08:12 AM
Sef,

I think you should read my response to Daniel again a bit more carefully. Then you'll see that I did not "pass him the rope". I merely offered him a choice of two simple solutions. And they both ARE solutions because they solve his stack overflow problem.

Alvaro

Sef
September 7th, 1999, 02:31 PM
Your missing the point. Someone who doesn't know that creating 147,000 strings on the stack is a problem is demonstrating their inexperience. It's absurd to allocate this many strings under normal circumstances. Doing it on the stack is even worse. I therefore question his judgment at attempting this. Had I responded to him directly, I would have asked what he's trying to do first and then offered a more viable alternative. My comment to you was simple. Why encourage bad habits in people still learning. There's enough shoddy work in this field already.

ALM
September 7th, 1999, 03:40 PM
Well for an inexperienced programmer Daniel's got his hands full! According to him is app will read millions of records from an SQL Server database and contain hundreds of thousands of lines of code...

You see Sef, my purpose in responding to Daniel's problem was not to tell him that his approach was good or bad. That's up to him to decide. My objective was to give him all the possible solutions to his PROBLEM. If you reread my response (like I suggested you to) I never ENCOURAGE anything bad. In fact, I kind of imply that the first solution is the better of the two (which, by the way, is the one he took). Who am I to decide that what he is doing is wrong? As you yourself put it, it's wrong "under normal circumstances". Who's to say that his circumstances are normal?

So my advice to you: spend more time answering people's questions and less time critizing their answers.

Alvaro