jimjim said:
Once again:
When you create objects dynamically (A *a = new A), there is the case that
the heap may become exhausted and therefore any effort to create objects
will "fail" (memory wont be allocated). For this reason, you ought to check
whether a == NULL, or try to catch the bad_alloc exception that is thrown
by the new operator.
When you create objects on the stack should I assume that the memory
allocated to the stack segment may become exhausted as well? If the anwer to
the question is positive, how can I check if the objects I tried to create
are indeed created (i.e. just like I did for dynamic allocation of memory
b4)?
Please try not to top-post. If the message you're replying to is
irrelevant (and no need to read it before seeing your comments), please
consider trimming it away.
Yes, stack can become exhausted. That's where you get "stack overflow"
problems, like with too deep a recursion. However, those problems only
exists on the systems where there is a stack, and if such condition
arises, any further execution of the program is usually impossible and
some kind of exception is "thrown".
C++ does not have any mechanisms to check how close the "stack" is to
its exhaustion, since the virtual machine on which C++ programs execute
does not have any particular "stack" implementation. We only know that
automatic variables (objects too) are created somewhere and fully
constructed by the time we need them. There is no additional check
necessary (or possible) to verify that when you reached a point in your
program _after_ an automatic object is defined and initialised, that
object is OK. IOW, it is OK since your program reached that point.
I guess you need to read a decent book that explains the difference
between objects with static storage duration, automatic storage duration
and dynamic storage duration. What book are you currently reading, BTW?
V