C FAQ.....
X86 Assembly Language FAQ
Memory Management Referenced FAQ
JTAG FAQ
UNIX Socket FAQ
Tuesday, November 25, 2008
Friday, November 14, 2008
#define Vs typedef
#define Vs typedef
1. A typedef declaration does not create a new type, it provides an alias to an existing type. of course, typedef is like #define, the difference is typedefs are interpreted by the compiler, and it can cope with textual substitutions that are beyond the capabilities of the preprocessor whereas #defines are handled by preprocessor.
2. typedef does typechecking.
In case of #define it will replace the macro with macro definition and then checks the type during the compilaton.
#define CHARP char*
typedef char* CHAR_P;
CHARP ch1,ch2;
CHAR_P ch3,ch4;
ch1 = "Character1"; //legal
ch2 = "Character2"; //illegal
ch2 = 's'; //legal
ch3 = "Character3"; //legal
ch4 = "Character4"; //legal
3. It is difficult to use #define to define a structure ,union or a class.
1. A typedef declaration does not create a new type, it provides an alias to an existing type. of course, typedef is like #define, the difference is typedefs are interpreted by the compiler, and it can cope with textual substitutions that are beyond the capabilities of the preprocessor whereas #defines are handled by preprocessor.
2. typedef does typechecking.
In case of #define it will replace the macro with macro definition and then checks the type during the compilaton.
#define CHARP char*
typedef char* CHAR_P;
CHARP ch1,ch2;
CHAR_P ch3,ch4;
ch1 = "Character1"; //legal
ch2 = "Character2"; //illegal
ch2 = 's'; //legal
ch3 = "Character3"; //legal
ch4 = "Character4"; //legal
3. It is difficult to use #define to define a structure ,union or a class.
C Structures Vs C++ Structures
C Structures Vs C++ Structures
1.structures in C don't have member functions
2.The defination of the structure in C is limited to within the module and cannot be initialized outside its scope.
Where as in C++ you can initialize the objects anywhere within the boundaries of the project.
3. In C++ structure label is typedeffed by default
Ex:
struct Mystruct
{
int i;
char c;
};
Mystruct Ms1; //illegal in C but legal in C++
struct Mystruct Ms2; //legal in both C and C++
typedef struct Urstruct
{
int I;
char C;
}US;
Urstruct Us1; //illegal in C but legal in C++
struct Urstruct Us2; //legal in both C and C++
US Us3; //legal in both C and C++
struct US Us4; //illegal in both C and C++
4. Size of empty structure is 0 in C but 1 in C++.
1.structures in C don't have member functions
2.The defination of the structure in C is limited to within the module and cannot be initialized outside its scope.
Where as in C++ you can initialize the objects anywhere within the boundaries of the project.
3. In C++ structure label is typedeffed by default
Ex:
struct Mystruct
{
int i;
char c;
};
Mystruct Ms1; //illegal in C but legal in C++
struct Mystruct Ms2; //legal in both C and C++
typedef struct Urstruct
{
int I;
char C;
}US;
Urstruct Us1; //illegal in C but legal in C++
struct Urstruct Us2; //legal in both C and C++
US Us3; //legal in both C and C++
struct US Us4; //illegal in both C and C++
4. Size of empty structure is 0 in C but 1 in C++.
Friday, November 7, 2008
new vs malloc
new vs malloc
new
1.new allocates memory and intializes it by calling constructor
2.new returns the pointer to the type which it has just allocated memory,no need to cast
for example: new int ---- This returns a integer pointer
3.new makes sure to allocate memory successfully before calling constructor so no need check whether the call is successfull or not
4.new is an operator.
malloc
new
1.new allocates memory and intializes it by calling constructor
2.new returns the pointer to the type which it has just allocated memory,no need to cast
for example: new int ---- This returns a integer pointer
3.new makes sure to allocate memory successfully before calling constructor so no need check whether the call is successfull or not
4.new is an operator.
malloc
1.malloc does only memory allocation,not intialization
2.malloc returns a void pointer,you have to cast it.
3.If malloc fails to allocate memory it returns a NULL pointer.so u must check the returned pointer.
4.malloc is a function
Enumerations(C enum Vs C++ enum)
Difference b/w enum in C ans C++(C enums Vs C++ enums):
C Implementation Example
enum num
{
first ,
second ,
};
enum num urnum= first; //legal
enum num mynum = second; //legal
num urnum= first; //illegal
num mynum = second; //illegal
typedef enum colors
{
red,green,yellow
}COL;
colors mycolors = red; //illegal
colors urcolors = green; //illegal
colors ourcolors = yellow; //illegal
COL hiscolors = green; //legal
COL hercolors = yellow; //legal
enum COL hiscolors = green; //illegal
enum COL hercolors = yellow; //illegal
enum colors hiscolors = green; //legal
enum colors hercolors = yellow; //legal
hiscolors = 4; //legal
i = hiscolors; //legal
C++ Implementation Example
enum num
{
first ,
second ,
};
enum num urnum= first; //legal
enum num mynum = second; //legal
num urnum= first; //legal
num mynum = second; //legal
typedef enum colors
{
red,green,yellow
}COL;
colors mycolors = red; //Legal
colors urcolors = green; //Legal
colors ourcolors = yellow; //legal
COL hiscolors = green; //legal
COL hercolors = yellow; //legal
enum COL hiscolors = green; //illegal
enum COL hercolors = yellow; //illegal
enum colors hiscolors = green; //legal
enum colors hercolors = yellow; //legal
mycolors = 4; //illegal
int i = mycolors; //legal
C Implementation Example
enum num
{
first ,
second ,
};
enum num urnum= first; //legal
enum num mynum = second; //legal
num urnum= first; //illegal
num mynum = second; //illegal
typedef enum colors
{
red,green,yellow
}COL;
colors mycolors = red; //illegal
colors urcolors = green; //illegal
colors ourcolors = yellow; //illegal
COL hiscolors = green; //legal
COL hercolors = yellow; //legal
enum COL hiscolors = green; //illegal
enum COL hercolors = yellow; //illegal
enum colors hiscolors = green; //legal
enum colors hercolors = yellow; //legal
hiscolors = 4; //legal
i = hiscolors; //legal
C++ Implementation Example
enum num
{
first ,
second ,
};
enum num urnum= first; //legal
enum num mynum = second; //legal
num urnum= first; //legal
num mynum = second; //legal
typedef enum colors
{
red,green,yellow
}COL;
colors mycolors = red; //Legal
colors urcolors = green; //Legal
colors ourcolors = yellow; //legal
COL hiscolors = green; //legal
COL hercolors = yellow; //legal
enum COL hiscolors = green; //illegal
enum COL hercolors = yellow; //illegal
enum colors hiscolors = green; //legal
enum colors hercolors = yellow; //legal
mycolors = 4; //illegal
int i = mycolors; //legal
Subscribe to:
Posts (Atom)
