Вы находитесь на странице: 1из 4

Malloc vs.

New
The basic differences between the two is that malloc exists in the C-language while new
is a specific feature of the C++ language. Malloc is also a function (which, for
programmers, reuires some time to execute! while new is an operator program (which
cuts the execution time!. This is a benefit from the operator new because programmers
doing real-time programming have a need to choose the fastest method to use.
"perator new is t#pe-safe unli$e the function malloc. %unction malloc is librar# function
for the C-language. &ll it does is allocate memor# and return a pointer to it. "n the other
hand, operator new is a language-level construct, and its 'ob is to instantiate an ob'ect b#
allocating memor# and calling the appropriate constructors. &nother difference of
function malloc from operator new is that malloc reuires t#pecasting during declaration
while the former doesn(t. &lso, function malloc uses the word )free* to clear the memor#
while )delete* is used in operator new for the similar purpose.
+f function malloc fails, it returns with a N,-- pointer. "perator new never returns a
N,-- pointer but indicates the failure b# throwing an exception instead. This is
an advantage for the operator new since the computer programmer doesn(t have to chec$
the returned pointer ever# time it calls new nor the computer programmer will miss such
an exception.
%unction malloc doesn(t construct an ob'ect (called constructor of ob'ect! but operator
new does. &nother advantage of new is that the operator can be overloaded while
function malloc cannot. "perator new reuires a specific number of ob'ects to allocate
while function malloc reuires specif#ing the total number of b#tes to allocate.
"perator new has leverage over function malloc b# returning the exact data t#pe while
the function returns void..
Memor# lea$
& memor# lea$ occurs when a piece (or pieces! of memor# that was previousl# allocated
b# a programmer is not properl# deallocated b# the programmer. /ven though that
memor# is no longer in use b# the program, it is still )reserved*, and that piece of
memor# can not be used b# the program until it is properl# deallocated b# the
programmer. +f a program has a lot of memor# that hasn(t been deallocated, then that
could reall# slow down the performance of the program. +f there(s no memor# left in the
program because of memor# lea$s, then that could of course cause the program to crash.
Memor# allignment
Most C0,s reuire that ob'ects and variables reside at particular offsets in the s#stem1s
memor#. %or example, 23-bit processors reuire a 4-b#te integer to reside at a memor#
address that is evenl# divisible b# 4. This reuirement is called 5memor#
alignment5. This is wh# the si6e that structs and classes occup# is often larger than the
sum of their members1 si6e.
Can a static function access non-static member variables of class7
No because non-static member variables of a class alwa#s belong to an ob'ect 8 meaning
that ever# ob'ect has it(s own personal cop# of non-static member variables (also $nown
as instance variables!. &nd, static functions have no ob'ect to wor$ with since the#
belong to the class as a whole.
9hat(s the difference between using uotes and angled brac$ets in the include statement7
The reason that uotes are used sometimes and angled brac$ets are used other times is
that the compiler will loo$ in different places for the header file depending on whether
uotes or brac$ets are used. :o, the distinction tells the compiler where to loo$ for the
header file. %or header files in angular brac$ets, the compiler will loo$ wherever
predefined header files are $ept in #our implementation of C++. ;emember that not all
implementations of C++ are the same, so this location ma# be different across different
implementations. +f the header file is inside uotes, then the compiler will loo$ inside the
current director# for that header file, or it will loo$ wherever header files defined b#
programmers are stored on #our s#stem.
<iamond 0roblem
:uppose we have 3 classes = and C that derive from the same class 8 in our example
above it would be class &. 9e also have class < that derives from both = and C b# using
multiple inheritance. The problem is that because < derives from both = and C which
each have their own cop# of the data members and methods of the & class-the < ob'ect d
will contain two subob'ects of the & base class. 9hen we call a function for a < ob'ect,
the compiler does not $now whether the call refers to the cop# of the function that the <
ob'ect d inherited through the = class or through the C class. The solution is to mar$ the
inheritance as >irtual to ensure that onl# one subob'ect of the & class will be created for
ever# < ob'ect.

+n C++, what(s the difference between an inline function and a macro7
The ma'or difference between inline functions and macros is the wa# the# are handled.
+nline functions are parsed b# the compiler, whereas macros are expanded b# the C++
preprocessor. This difference creates other differences, as best illustrated b# examples.
The difference between an inline function and a regular function is that wherever the
compiler finds a call to an inline function, it writes a cop# of the compiled function
definition. ?owever, with a regular function, a normal function call is generated.
& C++ class is abstract if it has a pure virtual function
&n# C++ class with at least one pure virtual function is considered to be an abstract class.
This means that there can be no ob'ect created for that class (since it is abstract!. This
also means that an# class that derives from that abstract class must implement the pure
virtual function, otherwise that derived class will also be considered to be an abstract
class as well.
Can a pure virtual function have an implementation7
?aving that implementation ma# be desirable when #ou thin$ that classes which derive
from the base class ma# need some sort of default behavior for the pure virtual function.
?owever, pure virtual functions can not have a definition inside the function declaration.
Name hiding (C++ onl#!
%riend
+n C++, the friend $e#word can be applied to either a function or a class. & friend
function or friend class has access to the private members of the class in which the
declaration occurs. %or example, suppose that we have implemented a lin$ed list arra#
class. &nd, let(s suppose that we also want to create a separate class to iterate through the
lin$ed list. The iterator class would most li$el# need to have access to the lin$ed list
class(s private members in order to function correctl#. +n this scenario, it would ma$e a
lot of sense to declare the iterator as a friend to the lin$ed list class.
?ow do #ou call C functions from C++7
+n order to call a C function from C++ code, #ou would use the )extern )C** $e#word
when declaring the function in C.
extern 5C5 void foo(!@
void main(!
A foo( !@ B
<oes the compiler create separate definitions for all possible t#pes for a function
template7
9hen a programmer creates a function template, then what exactl# is going on behind the
scenes with the compiler7 9ell, the compiler does not create separate definitions for all
possible t#pes for the function template. +nstead, the compiler does the smart thing and
onl# creates definitions for the t#pes that are actuall# used 8 so if a function template is
called with an int and a float, then the compiler will create definitions for the function
template with those 3 t#pes and not other t#pes li$e char or double.
+nline functions
C++ inline function is powerful concept that is commonl# used with classes. +f a function
is inline, the compiler places a cop# of the code of that function at each point where the
function is called at compile time. & function definition in a class definition is an inline
function definition, even without the use of the inline specifier.
9hat is function overloading and operator overloading7
%unction overloadingC C++ enables several functions of the same name to be defined, as
long as these functions have different sets of parameters (at least as far as their t#pes are
concerned!. This capabilit# is called function overloading. 9hen an overloaded function
is called, the C++ compiler selects the proper function b# examining the number, t#pes
and order of the arguments in the call. %unction overloading is commonl# used to create
several functions of the same name that perform similar tas$s but on different data t#pes.
"perator overloading allows existing C++ operators to be redefined so that the# wor$ on
ob'ects of user-defined classes.
>irtual constructor
<eclaring something virtual in C++ means that it can be overridden b# a sub-class of the
current class, however the constructor is called when the ob'ected is created, at that time
#ou can not be creating a sub-class of the class #ou must be creating the class so there
would never be an# need to declare a constructor virtual.
9hat is a template7
Templates allow to create generic functions that admit an# data t#pe as parameters and
return value without having to overload the function with all the possible data t#pes.
,ntil certain point the# fulfill the functionalit# of a macro. +ts protot#pe is an# of the two
following onesC template Dclass indetifierE functionFdeclaration@ template Dt#pename
indetifierE functionFdeclaration@ The onl# difference between both protot#pes is the use
of $e#word class or t#pename, its use is indistinct since both expressions have exactl# the
same meaning and behave exactl# the same wa#.

Вам также может понравиться