Solved

Stack size question

Posted on 1998-05-20
18
499 Views
Last Modified: 2010-04-10
I get a stack overflow error at run time...

How do i increase the stack size in Borland's Turbo C++ 4.5?

Jeff
0
Comment
Question by:jkelly061597
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 5
  • +1
18 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1164418
If this is a windows program the stack size specified is the initial stack size, the stack will grow as needed so you shouldn't run into a stack overflow unless you have a bug.  (Or iif you know that you legitimately use a LOT of stack space.)  So if this is a windows program that doesn't have unusual stack usage, look for a bug, like infinite recursion instead of changing the stack space.  If this is a DOS program, you might be running out of stack space, so ignore this.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1164419
I guess it is a DOS program. Do you use any large array allocated on the stack like the following? Use dynamic allocation instead (C++ operator new and delete) or simply define it as a static or global variable allocated on the heap.

void MyFunc()
{
    ...
    char achBuf[2048];
    ...
}

Another possible reason causing this is a deep recursive function.
0
 

Author Comment

by:jkelly061597
ID: 1164420
I have a large array of classes... would this fit the large array description?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:chensu
ID: 1164421
Yes.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1164422
Just try putting "static" there to see whether the error still exists. If not, this should be an answer. :-)


void MyFunc()
{
    ...
    static CMyClass myObjects[2048];
    ...
}

I would suggest using dynamic allocation instead of using static or global variables. Using static or global variables will make your executable files bigger.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1164423
In DOS you are more limited in your address space .. there isn't any virtual memory, so when it is full, it is full.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1164424
However, in DOS you can set the stack size.  And this is the maximum stack size, since the stack does no grow, so it is important.  However, you are limited to a pretty small stack so your best bet is probably to reduce your stack usage as Chensu suggested (since it seems like you are unneccesarily using stack space).  

I would tell you how to set stack size, just so you know, but I'm using VC 5.0 which doesn't produce DOS programs, so I don't know.
0
 

Author Comment

by:jkelly061597
ID: 1164425
How do I dynamically allocate my array?
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1164426
for an array 100 objects of class "Cls" you would do

Cls *ClsAry = new Cls[100];

Note that the 100 does not have to be "hardcoded"  it could be a variable.  This allows you to allocate arrays of whatever size you determine you need at run-time.

Are you familar with using "new" and "delete"?
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1164427
to dynamically alocate you create an array of POINTERS to your class objects (allocate with new or malloc or stl classes or whatever) and then for each entry, use new to allocate the objects and store pointers to the in the array.  You need to be careful when doing things like removing elements or copying them.  If not, you can end up with dangling pointers or inaccessible objects.

Techniques like smart pointers can help you manage your objects without you needing to anything (much) special in your code.


0
 
LVL 22

Expert Comment

by:nietod
ID: 1164428
Roger, is that just for jkelly's additional edification.  Or is there some evidence that I missed that he wants an arrray of pointers.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1164429
if he wants to take advantage of dynamic storage to avoid using up stack space, then creating the class objects on the heap and storing pointers only in the array should help.

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1164430
Alternatively, there are dynamic array classes in the standard stl library that could do the trick.  Does Turbo C++ 4.5 have STL support?

0
 
LVL 23

Expert Comment

by:chensu
ID: 1164431
I am sure Turbo C++ 4.5 does not support C++ template.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1164432
jkelly,

Have you tried what I suggested?
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1164433
In that case, without STL (and templates) if Jeff wants to make use of dynamic allocation, he would be better allocating the objects with new and storing pointers to them in a dynamic array (can you malloc/realloc/free to do the dynamic array).

He should probably wrap this dynamic array in a class so that he can control the allocation, freeing, copying etc of the array and its elements.


0
 

Author Comment

by:jkelly061597
ID: 1164434
When I created a static array rather than automatic I was able to avoid the overflow...

I'm trying to tweek the code withyour given info...

---

Is this the proper use of delete:

delete my_array;
0
 
LVL 22

Expert Comment

by:nietod
ID: 1164435
Yes, but only in my_array was allocated with new, like

int *my_array = new int;

If my_array was allocated with new [], then use delete [], like

int *my_arrray = new int[10];
delete [] my_array;

If my_array is not dynamically allocated, that is, it is global, local, or static don't use delete or delete[]!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c++ how to tell if the progra is ctl or mfc atl ect 6 110
How to split this in C++ 4 129
C++ question 3 82
Header of docx file 17 145
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question