Friday, January 18, 2008

All you have to know about static keyword

Hi! Folks

Hope this compilation on static keyword proves to be useful .
As a OOP i will try to explain static keyword w.r.t classes
A local variable is initialized when the thread of execution reaches its definition. By default, thishappens in every call of the function and each invocation of the function has its own copy of the variable. If a local variable is declared static , a single, statically allocated object will be used to represent that variable in all calls of the function. It will be initialized only the first time the thread of execution reaches its definition.


Both function and data members of a class can be made static.

A static variable provides a function with ‘‘a memory’’ without introducing a global variable that might be accessed and corrupted by other functions.

A static member can be referred to like any other member by non-static member function too. In addition, a static member can be referred to without mentioning an object.

There is exactly one copy of a s t a t i c member instead of one copy per object, as for ordinary non-st a t i c members.

Similarly, a function that needs access to members of a class, yet doesn’t need to be invoked for a particular object, is called a static member function.


When you declare a static data member within a class, you are not defining it. (That
is, you are not allocating storage for it.)

Instead, you must provide a global definition for it elsewhere, outside the class.
This is done by redeclaring the static variable using the scope resolution operator to identify the class to which it belongs.
This causes storage for the variable to be allocated. (Remember, a class declaration is simply a
logical construct that does not have physical reality.)
To understand the usage and effect of a static data member, consider this program:


#include
#include "conio.h"
using namespace std;
class X

{
int i;
static int x;
public:
X(int ii = 0) : i(ii) {} // Default
~X()
{
cout << "X::~X()" <<>
}

static void func() {//i=10;
x++;cout<
};
void f()

{
static X x1(47);
static X x2; // Default constructor required
}

int X::x =27;//initialization of static

int main()
{
f();
X::func();
getch();
}


Note:**
In C++, the preceding use of static is still supported, but deprecated. This means that it is not recommended for new code. Instead, you should use a namespace.


A static member variable exists before any object of its class is created.

One use of a static member variable is to provide access control to some shared resource used by all objects of a class.

Interesting use of a static member variable is to keep track of the number of objects of a particular class type that are in existence.

By using static member variables, you should be able to virtually eliminate any
need for global variables. The trouble with global variables relative to OOP is that they
almost always violate the principle of encapsulation.



Actually, static member functions have limited applications, but one good use
for them is to "preinitialize" private static data before any object is actually created.


What do you have to say please post your reply

Please comment on the same.
Feedbacks will be executed .

Do visit:
http://techie-paradise.nicetopic.net/index.htm
and become member for free.



Q: Can static member functions act only on static member variables?
A: Yes! It can only act on static member variables

Thursday, January 17, 2008

MIx C & C++

If you are including a C header file that isn't provided by the system, you may need to wrap the #include line in an extern "C" { /*...*/ } construct. This tells the C++ compiler that the functions declared in the header file are C functions.

// This is C++ code
extern "C" {
// Get declaration for f(int i, char c, float x)
#include "my-C-code.h"
}
int main()
{
f(7, 'x', 3.14); // Note: nothing unusual in the call
...
}

Alternate linkage specifications

What happens if you’re writing a program in C++ and you want to

use a C library? If you make the C function declaration,

float f(int a, char b);

the C++ compiler will decorate this name to something like

_f_int_char to support function overloading (and type-safe

linkage). However, the C compiler that compiled your C library has

most definitely not decorated the name, so its internal name will be

_f. Thus, the linker will not be able to resolve your C++ calls to f( ).

The escape mechanism provided in C++ is the alternate linkage

specification, which was produced in the language by overloading

the extern keyword. The extern is followed by a string that

specifies the linkage you want for the declaration, followed by the

declaration:

extern "C" float f(int a, char b);

This tells the compiler to give C linkage to f( ) so that the compiler

doesn’t decorate the name. The only two types of linkage

specifications supported by the standard are “C” and “C++,” but

compiler vendors have the option of supporting other languages in

the same way.

If you have a group of declarations with alternate linkage, put them

inside braces, like this:

extern "C" {

float f(int a, char b);

double d(int a, char b);

}

Or, for a header file,

extern "C" {

#include "Myheader.h"

}

Most C++ compiler vendors handle the alternate linkage

specifications inside their header files that work with both C and

C++, so you don’t have to worry about it.

Something about Singleton Class in cpp

Singleton Class :- A class whose number of instances that can be instantiated is limited to one is called a singleton class. Thus, at any given time only one instance can exist, no more.

Where To Use Singlton Class :- The singleton design pattern is used whenever the design requires only one instance of a class. Some examples:

1) Application classes. There should only be one application class.

2) Logger classes. For logging purposes of an application there is usually one logger instance required.

Example:-

class CMyClass
{
private:
CMyClass() {} // Private Constructor

Static int nCount; // Current number of instances
Static int nMaxInstance; // Maximum number of instances

Public:
~CMyClass(); // Public Destructor
Static CMyClass *CreateInstance(); // Construct Indirectly
};

int CMyClass :: nCount = 0;
int CMyClass :: nMaxInstance = 1; // When maxInstance is 1, we have a pure singleton class

CMyClass:: ~CMyClass()
{
--nCount; // Decrement number of instances
}

CMyClass* CMyClass :: CreatInstance()
{
CMyClass* ptr = NULL;
if(nMaxInstance > nCount)
{
ptr = new CMyClass;
nCount++; // Increment no of instances
}
return ptr;
}

int main()
{
CMyClass* pObj = CMyClass::CreateInstance();
if(pObj)
{
// Success
}
else
{
// Failed to create, probably because the maximum number of instances has already
// been Created
}
delete pObj ;
return 0;
}

Article on Symbol table

Symbol Table:

In computer science, a symbol table is a data structure used by a language translator such as a compiler or interpreter, where each identifier in a program's source code is associated with information relating to its declaration or appearance in the source, such as its type, scope level and sometimes its location.

The symbol table of a small program is listed below. The table itself was generated using the GNU binutils' nm utility. There is one data symbol,
holaamigosh (noted by the "D" type), and many functions (self defined as well as from the standard library). The first column is where the symbol is located in the memory, the second is "The symbol type" and the third is the name of the symbol. By passing suitable parameters, the symbol table was made to sort on basis of address

Example table

Address Type Name
00000020 a T_BIT
00000040 a F_BIT
00000080 a I_BIT

20000004 t irqvec
20000008 t fiqvec
2000000c t InitReset
20000018 T _main

20000024 t End
20000030 T AT91F_US3_CfgPIO_useB
2000005c t AT91F_PIO_CfgPeriph
200000b0 T main
20000120 T AT91F_DBGU_Printk
20000190 t AT91F_US_TxReady
200001c0 t AT91F_US_PutChar
200001f8 T AT91F_SpuriousHandler
20000214 T AT91F_DataAbort
20000230 t AT91F_PDC_SetNextRx


An object code file will contain a symbol table of the identifiers it contains that are externally visible. During the linking of different object files, a linker will use these symbol tables to resolve any unresolved references.
A symbol table may only exist during the translation process, or it may be embedded in the output of that process for later exploitation, for example, during an interactive
debugging session, or as a resource for formatting a diagnostic report during or after execution of a program.


For more details ,
Other link:http://www.sco.com/developers/gabi/2001-04-24/ch4.symtab.html

By Vinod Mahalle

mahalle.v@gmail.com

Excellent Site for EBooks

I bet you wont get better site to download ebooks of your liking then here
at http://www.ebookee.com/

nJOy! Downloading

Are You Fond Of Downloading!

Q: What is Rapidshare, Megaupload or YouSendIt ?

A: These are free and unlimited file hosting services where anyone can upload and download large files including MP3 songs, videos, games, software, ebooks, etc.While Rapidshare and Megaupload are available only in the browser, alternative YouSendIt also has a desktop client that can resume uploads.Problem: Since services like Rapidshare or Megaupload make file sharing so simple, they are often used for sharing illegal content like warez, adult movies and software cracks. Rapidshare URLs are not protected by passwords so anyone can download that content.