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.

हाय एवेर्य्बोद्य

Attention !!!! All u need to know about CPP & C++

Good Sites for Tech Stuff Reloaded

A Tutorial on creating DLLs with VC++
Advanced C++ Lessons
C Language FAQ Part 2
CodeProject Thread Synchronization for Beginners. Free source code and programming articles
Contents
DLLs
Free C and C++ eBooks - Download C and C++ eBooks - List of C and C++ eBooks
Free ebooks, audiobooks & magazines from the web fiction, nonfiction C++ and Visual Basic
Head First Object-Oriented Analysis and Design A Brain Friendly Guide to OOA Free eBooks Download!
InformIT Visual C++ 6 Unleashed
Instructions for use
Interview questions for tech companies » C++ notes for discussion
ITStudy8.org
Mfc Programming With Visual C 6 Unleashed Free eBooks Download!
What are Virtual Functions How to implement virtual functions in C&

Wednesday, January 16, 2008

Some more Interview Qns

Useful Links
http://diamond/exchweb/bin/redir.asp?URL=http://www.gnu.org/software/libc/manual/html_node/index.html
http://diamond/exchweb/bin/redir.asp?URL=http://www.parashift.com/c%2B%2B-faq-lite/

1. What is logical difference between Memory leak & dangling pointer?
A dangling pointer occurs when a pointer doesnot have a target. Example includes an uninitialized pointer and a pointer whose target has been deallocated(perhaps via an aliasing pointer).
A memory leak occurs when a process fails to deallocate memory and does not retain any pointer to that memory. The result is that the process can make no use of memory, nor can it deallocate the memory.
2. How to allocate dynamic memory on stack?
http://diamond/exchweb/bin/redir.asp?URL=http://www.codersource.net/c%2B%2B_dynamic_memory_allocation.aspx
3. What is Template?
http://diamond/exchweb/bin/redir.asp?URL=http://www.cplusplus.com/doc/tutorial/templates.html
http://diamond/exchweb/bin/redir.asp?URL=http://www.codersource.net/cpp_class_templates.html
4. What is STL? Explain different containers. Introduction & Overview:
http://diamond/exchweb/bin/redir.asp?URL=http://www.codebeach.com/tutorials/what-is-stl.asp
Everything about STL:
http://diamond/exchweb/bin/redir.asp?URL=http://www.decompile.com/html/tut.html
5. What are smart pointers. Explain in detail.
http://diamond/exchweb/bin/redir.asp?URL=http://en.wikipedia.org/wiki/Smart_pointer http://diamond/exchweb/bin/redir.asp?URL=http://www.informit.com/articles/article.aspx?p=31529
6. Explain exception handling.
http://diamond/exchweb/bin/redir.asp?URL=http://en.wikipedia.org/wiki/Exception_handling
http://diamond/exchweb/bin/redir.asp?URL=http://www.eastcoastgames.com/articles/cppexception.html
7. What is casting?
http://diamond/exchweb/bin/redir.asp?URL=http://www.codeguru.com/forum/showthread.php?t=312456
http://diamond/exchweb/bin/redir.asp?URL=http://www.acm.org/crossroads/xrds3-1/ovp3-1.html
8. Difference between new operator and operator new. When do we use operator new?
Here is what I meant.
------ 1 ------ Foo* p = static_cast(operator new (n * sizeof (Foo))); // Stuff delete p; ---------------
------ 2 ------ Foo* p = new Foo [n]; // Stuff delete[] p; The former will allocate with the global operator new allocator, enough room for n Foo objects, but will not call any constructor. So, p will point to raw memory, and accessing any member of Foo, or casting Foo to one of its base class, or doing similar things, will have undefined behaviour (see [basic.life]-5) Moreover, the delete expression will call a destructor on a non-alive Foo object. Thus, the behaviour is undefined. If, you replace the delete expression by an explicit call to the global operator delete, it correct this problem:
Foo* p = static_cast(operator new (n * sizeof (Foo))); // calls the global operator new // You can't access Foo members or subtypes here ::operator delete (p); // free the memory
The later code allocates memory with operator new[], which may be the global operator new[], but may also be an overloaded operator new[] by the Foo class. It also constructs all objects, using the default-constructor of Foo (or nothing if Foo is a POD type).
delete[] p, will destroy properly all Foo objects, and release memory with the correct version of operator delete[], which may be overloaded by the Foo class.
9. Why do we use virtual destructor?
http://diamond/exchweb/bin/redir.asp?URL=http://www.geekinterview.com/question_details/19709
http://diamond/exchweb/bin/redir.asp?URL=http://www.codersource.net/cpp_virtual_destructors.html
10. Difference between malloc, calloc and relloc.
http://diamond/exchweb/bin/redir.asp?URL=http://wiki.answers.com/Q/What_is_the_difference_between_malloc_and_calloc_functions
11.can constructors be virtual and why? http://diamond/exchweb/bin/redir.asp?URL=http://www.acetheinterview.com/questions/cats/index.php/cplusplus/2006/10/24/virtual-constructors-by-neetu
http://diamond/exchweb/bin/redir.asp?URL=http://www.geekinterview.com/question_details/36942
12. Static Keyword
http://diamond/exchweb/bin/redir.asp?URL=http://www.cprogramming.com/tutorial/statickeyword.html
13.what is the aggregation and composition..................when it is used and which one is preferred
http://diamond/exchweb/bin/redir.asp?URL=http://www.artima.com/forums/flat.jsp?forum=17%26thread=24715
http://diamond/exchweb/bin/redir.asp?URL=http://ootips.org/uml-hasa.html

14.what is nmake what are the parameters given to it?\
http://diamond/exchweb/bin/redir.asp?URL=http://msdn2.microsoft.com/en-us/library/ms930369.aspx
15.what is copy constructor? its significance? why it is necessary? example...
http://diamond/exchweb/bin/redir.asp?URL=http://www.codersource.net/cpp_copy_constructors.html http://diamond/exchweb/bin/redir.asp?URL=http://en.wikipedia.org/wiki/Copy_constructor
16.difference between reference and pointers. why references are useful?practicle example.
http://diamond/exchweb/bin/redir.asp?URL=http://www.embedded.com/story/OEG20010311S0024
http://diamond/exchweb/bin/redir.asp?URL=http://www.go4expert.com/forums/showthread.php?t=5492
http://diamond/exchweb/bin/redir.asp?URL=http://www.daniweb.com/blogs/entry1664.html

17.what is virtual base class?
http://diamond/exchweb/bin/redir.asp?URL=http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.vacpp7a.doc/language/ref/clrc14cplr135.htm
http://diamond/exchweb/bin/redir.asp?URL=http://www.deitel.com/articles/cplusplus_tutorials/20060225/virtualBaseClass/
18.what are virtual functions? how they get invoked?
http://diamond/exchweb/bin/redir.asp?URL=http://www.parashift.com/c%2B%2B-faq-lite/virtual-functions.html
http://diamond/exchweb/bin/redir.asp?URL=http://en.wikipedia.org/wiki/Virtual_function
19.concepts related to VTABLE.Does classes having non virtual function can have VTABLE?
http://diamond/exchweb/bin/redir.asp?URL=http://www.cauniversity.org/node/382
http://diamond/exchweb/bin/redir.asp?URL=http://gcc.gnu.org/ml/gcc-help/2002-12/msg00280.html

20.what is mutable keyword?when it is used?example... if mutable allows us to change the value of const variable then why we declare it as a const?
http://diamond/exchweb/bin/redir.asp?URL=http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/mutable_storage_class_specifier.htm
http://diamond/exchweb/bin/redir.asp?URL=http://www.highprogrammer.com/alan/rants/mutable.html
21.what are volatile variables?when it is used?example...
http://diamond/exchweb/bin/redir.asp?URL=http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/proguide/ref/xviexvvs.htm
http://diamond/exchweb/bin/redir.asp?URL=http://en.wikipedia.org/wiki/Volatile_variable

22.what is the size of empty class?why?
1 Byte
Because compilers are not bound by any rule that says that class size must increase by the size of what is added. If you add an extra byte to an empty class so each object has a distinct address, then you clearly no longer need the extra byte when the class is non-empty. So the compiler discards it (or, more accurately, doesn't add it in the first place).
http://diamond/exchweb/bin/redir.asp?URL=http://www.velocityreviews.com/forums/t268090-re-why-empty-class-size-is-1-byte-.html


23. What is name mangling
http://diamond/exchweb/bin/redir.asp?URL=http://en.wikipedia.org/wiki/Name_mangling


24. What is RTTI and different operators used in that.
http://diamond/exchweb/bin/redir.asp?URL=http://en.wikipedia.org/wiki/Run-time_type_information
25. Which opertors cant be overloaded http://diamond/exchweb/bin/redir.asp?URL=http://www.velocityreviews.com/forums/t286554-operators-that-cannot-be-overloaded-why.html

Interview Questions !!!

Interview Question asked to VINAYAK

1.Tell me something about your Education and Family Background and last employer and
project

2.What are the contents in a empty class?

3.What is the size of the class?why?

4.Explain virtual mechanism in cpp?

5.What are Templates ? when r they used ?

6.what are function templates?

7.Explain working of copy constructor and assignment operator ?
why do u need to override them? Give a example and write down the code for same?

8.predict the output of the program:

#include
#include
using namespace std;
class Base
{
virtual void func(int x=10){cout<func();
getch();
return 0;
}

9.Are you ready to settle in pune This project requires lot of proactiveness from you and have to learn a lot about mech stuff and around 35% of c++ coding?

10.Did u undergone the CAA training?
we will get back to you!

extern keyword with example

http://msdn2.microsoft.com/en-us/library/0603949d(VS.80).aspx

Request For Links

Here you can request for the link you need on any technical stuff.
Please do post some important links known to you.

This is our blog.

Help yourself to keep it alive.