Tuesday, May 13, 2008

memory segment

Data Segment - Global +static data which are initialised to non-zero values.

Bss - Global +static data which are initialised to zero by default.

Heap -Dynamically allocated.

Stack - Local or Auto variables

Apart from all the discussions, i read one article where there will be compiler documents which comes along with the compiler which specifies where these segments are created and how they are used.

Usually, the compiler used by intel processors contain a code segment ,data segment.

Where in the code segment is the read only area.
Data segment in turn contains stack and heap. Where heap grows from bottom to top and stack grows from top to bottom of DS.

Monday, May 12, 2008

Static Member Functions

Static Member Functions

C++You cannot have static and nonstatic member functions with the same names and the same number and type of arguments.

Like static data members, you may access a static member function f() of a class A without using an object of class A.

A static member function does not have a this pointer. The following example demonstrates this:

#include 
using namespace std;

struct X {
private:
int i;
static int si;
public:
void set_i(int arg) { i = arg; }
static void set_si(int arg) { si = arg; }

void print_i() {
cout << "Value of i = " << i << endl;
cout << "Again, value of i = " <<>i << endl;
}

static void print_si() {
cout << "Value of si = " << si << endl;
// cout << "Again, value of si = " <<>si << endl;
}

};

int X::si = 77; // Initialize static data member

int main() {
X xobj;
xobj.set_i(11);
xobj.print_i();

// static data members and functions belong to the class and
// can be accessed without using an object of class X
X::print_si();
X::set_si(22);
X::print_si();
}

The following is the output of the above example:

Value of i = 11
Again, value of i = 11
Value of si = 77
Value of si = 22

The compiler does not allow the member access operation this->si in function A::print_si() because this member function has been declared as static, and therefore does not have a this pointer.

You can call a static member function using the this pointer of a nonstatic member function. In the following example, the nonstatic member function printall() calls the static member function f() using the this pointer:

#include 
using namespace std;

class C {
static void f() {
cout << "Here is i: " << i << endl;
}
static int i;
int j;
public:
C(int firstj): j(firstj) { }
void printall();
};

void C::printall() {
cout << "Here is j: " <<>j << endl;
this->f();
}

int C::i = 3;

int main() {
C obj_C(0);
obj_C.printall();
}

The following is the output of the above example:

Here is j: 0
Here is i: 3

A static member function cannot be declared with the keywords virtual, const, volatile, or const volatile.

A static member function can access only the names of static members, enumerators, and nested types of the class in which it is declared. Suppose a static member function f() is a member of class X. The static member function f() cannot access the nonstatic members X or the nonstatic members of a base class of X

What is late binding?

What is late binding?

In a nutshell, late binding is run-time resolution / dynamic loading. Compile time resolution (sometimes called 'early binding'), uses the compiler and linker to verify that the argument types used to call a function matches the function's signature. In my mind, early binding allows me to check my grammar (how I am calling functions) before checking in my code. By contrast, late binding requires the developer to specify the function signatures and to ensure that the correct types are used.

Why is it called 'late' binding? 'Late' refers to the fact that the binding decisions (which binary to load, which function to call) is deferred as long as possible, often until just before the function is called, rather than having the binding decisions made at compile time (early).

In C/C++, late binding (a.k.a. run-time dynamic linking) often takes the form of LoadLibrary / GetProcAddress / FreeLibrary. The MSDN Library provides a good example of late binding in C/C++.

For the .NET Framework (.NET Compact Framework included), when you use Reflection to load an assembly, you are late binding. The MSDN Library has a nice example (in Visual Basic.NET and C#) on how to dynamically load and use types via reflection.

Why late bind?
"Run-time dynamic linking enables the process to continue running even if a DLL is not available. The process can then use an alternate method to accomplish its objective. For example, if a process is unable to locate one DLL, it can try to use another, or it can notify the user of an error."

The above MSDN Library quote describes my favorite use for late binding -- avoiding an application crash if a DLL is missing. Another great use for late binding is enabling application plugins.

Note: It is important to be aware that late binding can have an impact on your application's performance. Please be sure to measure and evaluate whether or not any performance issues take aware from any benefit(s) gained from late binding in your application.