Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

STACK VS HEAP

‘Stack’ is a common name for the memory space in which automatic variables (and often function
parameters) are allocated.

‘Heap’ is a common name for the “free store”, the memory space where dynamic objects are allocated
(see “new” and “delete”).

The stack is used to allocate temporary objects while the heap is used by a programmer to reserve
allocation. Basically, an object in a heap is not scope dependent, it dies when you tell it to. Meanwhile,
an object on the stack is destroyed automatically when its scope ends.

The scope of memory allocated on the ’stack’ is the scope of the enclosing block. They get destroyed
when the enclosing block is finished. Thats why they are called ‘auto’ objects.int* pJ;

int* pI = new int [20];

// Note that 2 things are happening here:

// 1.) An int array is allocated on the free store (aka heap).

// This array will be there until a corresponding delete [] is

// executed

// 2.) A variable pI is created. Since pI was not dynamically created

// with new or malloc, it is an auto object (created on the stack)

// Thus the enclosing block determines its lifetime

pJ = pI;

} // block ends. pI gets destroyed, but not so the int array. It

// will stay there in memory until a delete is done.

delete [] pJ; // now the memory in the free store (aka heap) gets released.
C does not have any classes or objects. It is procedure and function driven. There is no concept of access
through objects and structures are the only place where there is a access through a compacted variable.
c++ is object oriented.

C structures have a different behaviour compared to c++ structures. Structures in c do not accept
functions as their parts.

C input/output is based on library and the processes are carried out by including functions. C++ i/o is
made through console commands cin and cout.

C functions do not support overloading. Operator overloading is a process in which the same function
has two or more different behaviours based on the data input by the user.

C does not support new or delete commands. The memory operations to free or allocate memory in c
are carried out by malloc() and free().

Undeclared functions in c++ are not allowed. The function has to have a prototype defined before the
main() before use in c++ although in c the functions can be declared at the point of use.

After declaring structures and enumerators in c we cannot declare the variable for the structure right
after the end of the structure as in c++.

For an int main() in c++ we may not write a return statement but the return is mandatory in c if we are
using int main().

In C++ identifiers are not allowed to contain two or more consecutive underscores in any position. C
identifiers cannot start with two or more consecutive underscores, but may contain them in other
positions.

C has a top down approach whereas c++ has a bottom up approach.

In c a character constant is automatically elevated to an integer whereas in c++ this is not the case.

In c declaring the global variable several times is allowed but this is not allowed in c++.

Interfaces vs Abstract Classes

feature interface abstract class

multiple A class may implement several A class may extend only


Interfaces vs Abstract Classes

feature interface abstract class

inheritance interfaces. one abstract class.

An abstract class can provide


default An interface cannot provide any
complete code, default code, and/or
implementation code at all, much less default code.
just stubs that have to be overridden.

Static final constants only, can use


them without qualification in classes
that implement the interface. On Both instance and static constants are
the other paw, these unqualified possible. Both static and instance
constants
names pollute the namespace. You intialiser code are also possible to
can use them and it is not obvious compute the constants.
where they are coming from since the
qualification is optional.

An interface implementation A third party class must be rewritten


third party
may be added to any existing third to extend only from
convenience the abstract class.
party class.

is-a vs -able or Interfaces are often used to describe An abstract class defines the core
can-do the peripheral abilities of a class, not
identity of its descendants. If you
its central identity, e.g. defined a Dogabstract class then
an Automobile class might Dalmatian descendants are Dogs,
implement they are not merely dogable.
the Recyclable interface, Implemented interfaces enumerate the
which could apply to many otherwise general things a class can do, not the
totally unrelated objects. things a class is.
In a Java context, users should
typically implement the
Runnable interface rather than
extending Thread, because they’re
not really interested in providing
some new Threadfunctionality, they
normally just want some code to have
the capability of running
independently. They want to create
Interfaces vs Abstract Classes

feature interface abstract class

something that can be run in a thread,


not a new kind of thread.The similar
is-a vs has-a debate comes up when
you decide to inherit or delegate.
multiple inheritance for further
discussion of is-a vs has-a

You can write a new replacement


module for an interfacethat
contains not one stick of code in You must use the abstract class
common with the existing
as-is for the code base, with all its
implementations. When you
attendant baggage, good or bad.
implement the interface, you start
The abstract class author has
plug-in from scratch without any default
imposed structure on you. Depending
implementation. You have to obtain
on the cleverness of the author of
your tools from other classes; nothing
the abstract class, this may be
comes with the interface other
good or bad.
than a few constants. This gives you
freedom to implement a radically
different internal design.
homogeneity If all the various implementations If the various implementations are all
share is the method signatures, then of a kind and share a common status
an interface works best. and behaviour, usually
an abstract class works best.
Another issue that’s important is what
I call "heterogeneous vs.
homogeneous." If
implementors/subclasses are
homogeneous, tend towards
an abstract base class. If they are
heterogeneous, use an interface.
(Now all I have to do is come up with
a good definition of hetero/homo-
geneous in this context.) If the
various objects are all of-a-kind, and
share a common state and behavior,
then tend towards a common base
class. If all they share is a set of
Interfaces vs Abstract Classes

feature interface abstract class

method signatures, then tend towards


aninterface.
Just like an interface, if your
If your client code talks only in terms
client code talks only in terms of
of an interface, you can easily
maintenance an abstract class, you can easily
change the concrete implementation
change the concrete implementation
behind it, using a factory method.
behind it, using a factory method.
Slow, requires extra indirection to
find the corresponding method in the
speed actual class. Modern JVMs are Fast
discovering ways to reduce this speed
penalty.
You can put shared code into
The constant declarations in
an abstract class, where you
an interface are all
cannot into an interface. If
presumedpublic static
interfaces want to share code, you
final, so you may leave that part
will have to write other bubblegum to
out. You can’t call any methods to
terseness arrange that. You may use methods to
compute the initial values of your
compute the initial values of your
constants. You need not declare
constants and variables, both instance
individual methods of
and static. You must declare all the
an interface abstract. They
individual methods of
are all presumed so. an abstract class abstract.
If you add a new method to If you add a new method to
an interface, you must track an abstract class, you have the
adding down all implementations of option of providing a default
functionality that interface in the universe and implementation of it. Then all
provide them with a concrete existing code will continue to work
implementation of that method. without change.

You might also like