Reputation: 832
The first time a teacher had introduced me to C++, one of the first definitions was about "stack based languages" like Java, C and C++.
Now i read about this in a reply and I'm honestly confused.
C++ is a stack based language but doesn't requires a stack ?
Upvotes: 14
Views: 1863
Reputation: 2862
It is semantics.
Think about a car. A car is described in terms of radio, speed, power seats, CD player, number of cup holders, paint color, etc. The feature list does not talk about where the power comes from to run all this stuff. Almost all cars use gas going to an internal combustion engine and the engine turns the wheels and runs a generator to make electric power to run all the accessories. An engine is not required, but most cars have one.
The language spec talks about the lifetime of data. It does not say how the lifetime is implemented. It turns out 99% of C++ compilers use global data, a stack and a heap to meet the requirements for the lifetime of data the language spec wants.
Upvotes: 2
Reputation: 14583
A non-"stack based language" is not simply a language that does not requires a stack.
Java's bytecode language is a stack-based language because its operations do not operate on registers, they operate on a stack. On the other hand, the Intel microprocessor's ASM language uses registers, and this may be reflected in languages designed to be compiled to that architecture.
C and C++ may or may be not stack based. That is entirely up to the compiler and to the target OS/microprocessor. You can't assume that it is or that it is not. In practice however, it is largely implemented as register-based.
Even if the language operates entirely with registers, you still have a stack for function calls and local variables that does not fit the registers. The presence of this stack does not makes the language stack-based, is the absence of the registers that does.
Edit: Nitpick as suggested by the user mikera: Java's bytecode language is stack-based, but in order to run it in most architectures that are register-based, you will need something translating the stack-based bytecode language in the register-based architecture. This work may be done by the interpreter in the JVM or by a JIT or non-JIT bytecode-to-native compiler.
Upvotes: 3
Reputation: 490108
C++ (like C, Java, and most other block-structured languages) requires a "stack" in the sense of some sort of last-in, first-out data structure to hold the activation records for function calls. That is, a function can call itself recursively to some arbitrary depth, and as it does so, the parameters/local variables for each call must be independent of those for previous calls. As those function calls return, the parameters/local variables for each must be destroyed.
That does not, however, require execution on a CPU that directly supports a stack in hardware. It's entirely possible to execute C++ (and the other block-structured languages, as mentioned above) without direct hardware support for a stack.
Just for example, early Crays and (even current) IBM mainframes do not support stacks. Despite this, they can and do support C++. At least in most cases, the LIFO data structure used for activation records is allocated dynamically, and build into a linked list. As each function is called, its activation record is allocated and added to the linked list. When the function returns, that activation record is removed from the list and the memory released.
So, if you're looking at things from a somewhat abstract viewpoint, thinking of a stack as the essence of the basic operations it provides, then yes, C++ requires a stack. If you look at "stack" less abstractly, and think in terms of something like a CPU with a stack pointer register (or anything on that order) then no, C++ definitely does not need a stack (and the same goes for C, Java, etc.)
Upvotes: 4
Reputation: 53037
When I hear "stack based language" I usually think of languages like FORTH where everything is done on a stack, ie no variables. I'm guessing when your teacher said Java they meant the JVM which is stack based.
Now, the C++ standard has absolutely no concept of a stack or heap, only things like automatic and dynamic storage. C++ is specified in terms of more abstract ideas which allows it to theoretically work on many different implementations and hardware. Of course, it turns out that these ideas map directly onto the idea of a stack, so every implementation ends up using one.
Upvotes: 10