Ah, your prof is an ada zealot. zealots of any language actually do more harm than good. Like C++, ada has both it's strengths and weaknesses.
If he's arguing that you should use ada for realtime applications because allocation on the stack is faster, then he's wrong. In hard real time applications, one of the basic guidelines is not to use dynamic memory allocation in any form.
The notion of stack and heap are a little different in ada than they are in C++ (as the Ada runtime does things differently) as well.
All auto variables in C++ are allocated (in principle anyway) on the stack. If the dynamically allocated something doesn't have to be contiguous ......
Code:
// untested, intended to illustrate approach
struct Node
{
Node *next;
SomeType data;
Node() : next(0) {};
};
void PrintStack(Node *stack)
{
while (stack != NULL)
{
// print out stack->data
stack = stack->next;
}
}
typedef void ((*func)(Node *));
void ActInternal(func function, int size, Node *first, Node *last)
{
if (size == 0)
function(first)
else
{
Node here;
last->next = &here;
// do something to populate here.data
ActInternal(function, size-1, first, &here);
}
}
void Act(func function, int size)
{
Node first;
// do something to populate first.data
ActInternal(func, size-1, &first, &first);
}
int main()
{
Act(PrintStack, 25);
}
This code will mean that PrintStack will see a stack of 25 dynamically allocated elements (albeit as a linked list) that are on the stack.