AdSense

Tuesday, March 10, 2015

OOP 102 - C

Virtual functions
This concept comes from Multiple inheritance, which means an object or class can inherit characteristics and features from more than one parent object or parent class. Virtual inheritance means that a base class in an inheritance hierarchy is declared to share its member data instances with any other inclusions of that same base in further derived classes. This technique makes the virtual base a common sub-object for the deriving class and all classes that are derived from it. It clarifies ambiguity over which ancestor class to use, as from the perspective of the deriving class, the virtual base acts as through it were direct base of the deriving class.


class Animal {
    public:
        virtual void eat();
};
class Mammal : public virtual Animal {
    public:
        virtual void breathe();
};
class WingedAnimal : public virtual Animal {
    public:
        virtual void flap();
};
// A bat is a winged mammal
class Bat : public Mammal, public WingedAnimal {
}

In the above example, the Animal portion of Bat :: WingedAnimal is now the same Animal instance as the one used by Bat::Mammal, so Bat has only one, shared, Animal instance in its representation, and so a call to Bat::eat() is unambiguous. The Bat becomes (vpointer, Mammal, vpointer, WingedAnimal, Bat, Animal)

Memory Layout
A typical memory representation of C program consists of following sections.
1. Text segment: A text segment, also known as a code segment or simply as text, is one of the sections of a program in an object file or in memory, which contains executable instructions.
As a memory region, a text segment may be placed below the heap or stack in order to prevent heaps and stack overflows from overwriting it.
Usually, the text segment is sharable so that only a single copy needs to be in memory for frequently executed programs, such as text editors, the C compiler, the shells, and so on. Also, the text segment is often read-only, to prevent a program from accidentally modifying its instructions.

2. Initialized data segment/Data Segment: a portion of virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer. Data segment is not read-only, since the values of the variables can be altered at run time. This segment can be further classified into initialized read-only (const char* string) area and initialized read-write (int) area.

3. Uninitialized data segment: often called "bss" segment ("block started by symbol"). Data in this segment is initialized by the kernel to arithmetic 0 before the program starts executing. Uninitialized data starts at the end of the data segment and contains all global variables and static variables that are initialized to zero or do not have explicit initialization in source code.
e.g., static in i (without any explicit initialization will be initialized to 0)

4. Stack: traditionally adjoined the heap area and grew the opposite direction; when the stack pointer met the heap pointer, free memory exhausted. The stack area contains the program stack, typically located in the higher parts of memory. On the standard PC x86 computer architecture it grows toward address zero; on some other architectures it grows the opposite direction. A "stack pointer" register tracks the top of the stack; it is adjusted each time a value is "pushed" onto the stack. The set of values pushed for one function call is termed a "stack frame"; A stack frame consists at minimum of a return address.
Stack stores automatic variables, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to and certain information about the caller's environment, such as some of the machine registers, are saved on the stack. The newly called function then allocates room on the stack for its automatic and temporary variables.
For recursive functions, each time a recursive function calls itself, a new stack frame is used, so one set of variables doesn't interfere with the variables from another instance of the function.

Frame allocation
Allocation on the frame takes its name from the "stack frame" that is set up whenever a function is called. The stack frame is an area of memory that temporarily holds the arguments to the function as well as any variables that are defined local to the function. Frame variables are often called "automatic" variables because the compiler automatically allocates the space for them.

When you define a local variable, enough space is allocated on the stack frame to hold the entire variable, even if it is a large array or data structure. Frame variables are automatically deleted when they go out of scope.

When an object is defined as a frame variable, its constructor is automatically invoked at the point where the definition is encountered. When the object goes out of scope, its destructor is automatically invoked before the memory for the object is reclaimed.

5. Heap: The segment where dynamic memory allocation usually takes place. The heap area begins at the end of the BSS segment and grows to larger addresses from there. The Heap area is managed by malloc, realloc, and free, which may use the brk and sbrk system calls to adjust its size. The Heap area is shared by all shared libraries and dynamically loaded modules in a process.

Heap Allocation
The heap is reserved for the memory allocation needs of the program. It is an area apart from the program code and the stack. Typical C programs use the functions malloc and free to allocate and deallocate heap memory. C++ can use new and delete to allocate and deallocate objects in heap memory.

The new and delete operators, are good for allocating and deallocating fixed-size memory blocks and objects. If your application may need resizable memory blocks, you must use the standard C run-time library functions malloc, realloc and free to manage resizable memory blocks on the heap.


Source: http://www.geeksforgeeks.org/memory-layout-of-c-program/


See OOP 101 for Java memory layout.


Storage area network (SAN): a dedicated network that provides access to consolidated, block level data storage. SANs are primarily used to enhance storage devices, such as disk arrays. It is accessible to servers so that the devices appear like locally attached devices to the OS. A SAN typically has its own network of storage devices that are generally not accessible through the local area network(LAN) by other devices.


Note for virtual functions
#include
class Base1{
public:
 Base1(){ data1 = 0;}
 //virtual function can be redefined in a derived class, 
 //while preserving its calling properties through references.
 //int virt1(){ return 100;}
 virtual int virt1(){ return 100; }
 int data1;
};
class Base2{
public:
 Base2(){ data2 = 2; }
 int data2;
 virtual int virt2(){ return 200;} 
};
class Derived: public Base1{
public:
 Derived(){ derivedData = 1; }
 //overrides virt1 in base class
 //non-virtual members of derived classes can also be redefined
 //in derived classes, but non-virtual members of derived classes
 //cannot be accessed through a reference of the base class
 virtual int virt1(){return 150;}
 int derivedData;
};
int Global1(Base1 *b1){
 return b1->virt1();
}
//multiple inheritance
class MultipleDerived : public Base1, public Base2{
public:
 virtual int virt1(){ return 150; }
 virtual int virt2(){ return 250; }
 int derivedDatam;
};
int Global2(Base2 *b2){
 return b2 -> virt2();
}
int main(){
 /*Derived *d = new Derived;
 Derived *b = new Derived;
 printf("%d %d\n", d->virt1(), Global1(b));
 printf("%d %d\n", d->data1, d->derivedData);
 delete[] d;*/
 MultipleDerived *md = new MultipleDerived;
 printf("%d %d %d %d\n", md -> virt1(), Global1(md), md -> virt2(),
   Global2(md));
 delete[] md;
 return 0;
}







No comments:

Post a Comment