What is function pointer and how to use function pointer?

Sunday, August 28th, 2011  »  Posted By  »  Total 1 Comment

A function pointer is a variable which is used to hold the starting address of a functions and the same can be used to invoke a function. It is also possible to pass address of different functions at different times thus making the function more flexible and abstract. So the function pointers can be used [...]

What is the difference between macro and function?

Friday, August 19th, 2011  »  Posted By  »  Total 0 Comment

Here are the differences between macro and function, 1.Macro consumes less time: When a function is called, arguments have to be passed to it, those arguments are accepted by corresponding dummy variables in the function. Then they are processed, and finally the function returns a value that is assigned to a variable (except for a [...]

What is the difference between new and malloc?

Friday, August 19th, 2011  »  Posted By  »  Total 1 Comment

Here are the differences between new and malloc, Operator new constructs an object (calls constructor of object), malloc does not. Hence new invokes the constructor (and delete invokes the destructor)This is the most important difference. operator new is an operator, malloc is a function. operator new can be overloaded, malloc cannot be overloaded. operator new [...]

What is the use of new and delete operator in C++?

Thursday, August 18th, 2011  »  Posted By  »  Total 0 Comment

new operator is used to dynamically allocate memory on the heap. Memory allocated by new must be deallocated using delete operator. Syntax of new is: p_var = new type name; where p_var is a previously declared pointer of type typename. typename can be any basic data type. new can also create an array: p_var = [...]

What is the use of extern in c/c++?

Thursday, January 27th, 2011  »  Posted By  »  Total 0 Comment

The extern keyword is used to tell the compiler that a data object is declared in a different *.cpp or *.c file. So there wont be any new memory is allocated for these variables. Check the below example and understand the working. //file1.cpp #include <iostream.h>   int x; extern void func2();   void func1(void) { [...]

When we should use virtual destructor?

Tuesday, January 25th, 2011  »  Posted By  »  Total 0 Comment

It is used whenever a base class pointer is pointing to its derived class. In such a case when a user tries to delete the base class pointer then it results in deallocating the memory occupied by the base class.Therefore instead the derived class getting destroyed the base class does. Now as the base class [...]

How to allocate two dimensional array?

Wednesday, January 19th, 2011  »  Posted By  »  Total 0 Comment

Consider if you want to allocate a two dimensional array: int a[10][3]; Then you have to allocate the rows first and then columns using for loops. Here is the C example code. int **a, x; //Allocate memory a = malloc(sizeof(int *) * 10); for(x = 0; x &lt; 10; x ++) { a[x] = malloc(sizeof(int) [...]

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup