?
Solved

Borland C++ compiler, Stack issues/question

Posted on 2000-02-21
5
Medium Priority
?
417 Views
Last Modified: 2010-08-05
This is related to Borland 5.02 C++ compiler for an OWL program.

Someone I work with noticed this occurring for large variable:
owlclass::routine1()
{
  if (x)
  {
  call routine2()
  }
  else
  {
  averylargevariable1[1000]
  }
}
owlclass::routine2()
{
averylargevariable2[1000]
}

When x was true and they stepped into routine2() the stack blew up, BUT (Here is the weird part)
if the size of averylargevariable1[1000] was changed to [2], the program worked fine.
It appears that the space for averylargevariable1[1000] was being allocated even though the curley braces were not currently being entered which, I believe is a C++ standards no - no for the compiler.

Maybe something weird is happening with the optimizer or something?

Can anyone make any pertinent comments here, or suggestions? It just seems like some weird compiler behavior . The work around was to put the code and allocation in the curley brackets around averylargevariable1[1000] in its own set of curley braces.

Thanks
Jeff
0
Comment
Question by:kishj
[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
  • 3
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 400 total points
ID: 2542474
>> It appears that the space for
>> averylargevariable1[1000] was being allocated
>> even though the curley braces were not currently
>> being entered which, I believe is a C++ standards
>> no - no for the compiler.
No the standard definitely does not make any statement to prevent this.  The standard does insure that the array will not ever be initialized (if the items in it have non-trivial constructors) unless the scope it is in is entered, but the spare for the array may be allocated at any time.

so a compiler may allocate space for ALL of a function's locals when the function starts, even if some of those locals won't ve constructed.  In fact, virtually ever compiler in the world will do this, because they will use a stack frame to access the locals and you can only have one strack frame at a time (on most CPUs at least) so you need  to provide the space for all the locals at one time.

Let me know if you have any questions.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2542485
>> The work around was to put the code
>> and allocation in the curley brackets around
>> averylargevariable1[1000] in its own set of
>> curley braces.
But that work-around didn't work, right?  Its a "not work-around" right?

One solution is to expand your program's stack space.  There is likely to be a linker option for this

Another solution is to use dynamically allocated memory (new).  This memory will come from the heap, not the stack, so you won't get a stack overflow.  (You could run out of heap space, but that is less likely, the heap is usually much larger than the stack.)

Another option is to use an array class, like vector<>.  This is sort of a combination of the two approaches, the vector is local and gives you the conveneince of a local variable, but the array storage space comes from the heap and has a better chance of being allocated.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2542907
This might be due to the fact that in C calling convention the caller has to set up and pop the stack (for var args functionality) for the called function.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2542984
Sort of, its more due to the way the stack frame is created.  The stack frame is the portion of the stack used to store the locals.  This portion is not necessarily the top of the stack (it is the top when the function starts, but not necessarily the top later.)  so it can't be exapanded at will as new variables come into the scope.
0
 

Author Comment

by:kishj
ID: 2543251
I'll have to check the standard, but Thanks! This is good information.

I am fighting the battle of when we want to take the hit.

Jeff
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 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