Вы находитесь на странице: 1из 5

MOST IMPORTANT FAQ in C

1. Why would you use a structure?


structure is to declare different data types at a time, where as a arra y is a group of related
data types. the main difference b/w structure & union is in memory requirement. In
structure s memory is sum of all the datatypes declared in it, where as in union memory is
the one which holds the highest bytes.
struct{ int i;float g;};here memory req is 2+4=6
union{int i;float g;}; here memory req is only 4 bytes
2. Difference between class and structure?
There is no much difference between a class and a structure and the slight
difference is the default scope of class is private where as the default scope of structure is
global.
Class is a user defined data type in object oriented paradigm. It consists of both
data and code (methods).where as structure is also a user defined data type but consists of
only data members. Not methods.
Structures allow organization of data whereas class allows both organization of
data and data security.
3. whats is structure padding? Say a given structure?
Struct{
int a;
char c;
float d;
}
the size of structure is 7 here. you can control padding using pragma pack why you need
padding ?. Ans is you need to align your VAR according to word boundary, padding will
make your programne work fast , If you dont have padding , use of above struct will give
bus error on some machine. To understand that we need go through more document on
this
4. what is the difference between methods and function?
The major difference between methods and functions is that methods called by the
reference variables called objects where as the functions do not having any reference
variables.
5. What is static memory allocation and dynamic memory allocation?
Static memory allocation: The compiler allocates the required memory space for a
declared variable. By using the address of operator, the reserved address is obtained and
this address may be assigned to a pointer variable. Since most of the declared variable
have static memory, this way of assigning pointer value to a pointer variable is known as
static memory allocation. Memory is assigned during compilation time.
MAIL_TO :
rajeshkumar.mannu@yahoo.com

MOST IMPORTANT FAQ in C


in static memory allocation memory is assigned at compile timefor
ex int arr[] = {1,2,3}; compiler will allocate memory for 3 integer at compile.
Dynamic memory allocation: It uses functions such as malloc( ) or calloc( ) to get
memory dynamically.If these functions are used to get memory dynamically and the
values returned by these functions are assingned to pointer variables, such assignments
are known as dynamic memory allocation.memory is assined during run time.
whil in dynamic memory allocation memory is assigned at run time.
Ex malloc(20) compiler will simply see its a function and is argument. while the
memory will be allocated at run time.
6. What is indirection?
If you declare a variable, its name is a direct reference to its value. If you have a
pointer to a variable, or any other object in memory, you have an indirect reference to its
value.
7. Between a long pointer and a char pointer which one consumes more memory?
1) As We All Know That Pointer(*) Holds Address!
2) Address is Always INTEGER (2-Bytes)
3) Hence, 2-Bytes Are Allocated For Any Data Types!
Both will consume same amount of memory. why because they means long or
char pointer always stores the address of the character or long integer.
8. Is it better to use malloc() or calloc()?
Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each
operates slightly different from the other. malloc() takes a size and returns a pointer to a
chunk of memory at least that big:
void *malloc( size_t size );
calloc() takes a number of elements, and the size of each, and returns a pointer to a chunk
of memory at least big enough to hold them all:
void *calloc( size_t numElements, size_t sizeOfElement );
Theres one major difference and one minor difference between the two functions. The
major difference is that malloc() doesnt initialize the allocated memory. The first time
malloc() gives you a particular chunk of memory, the memory might be full of zeros. If
memory has been allocated, freed, and reallocated, it probably has whatever junk was left
in it. That means, unfortunately, that a program might run in simple cases (when memory
is never reallocated) but break when used harder (and when memory is reused).
calloc() fills the allocated memory with all zero bits. That means that anything
there youre going to use as a char or an int of any length, signed or unsigned, is
guaranteed to be zero. Anything youre going to use as a pointer is set to all zero bits.
Thats usually a null pointer, but its not guaranteed.Anything youre going to use as a
float or double is set to all zero bits; thats a floating-point zero on some types of
machines, but not on all.

MAIL_TO :
rajeshkumar.mannu@yahoo.com

MOST IMPORTANT FAQ in C


The minor difference between the two is that calloc() returns an array of objects;
malloc() returns one object. Some people use calloc() to make clear that they want an
array.
9. What is a const pointer?
There are cases when you need to define a constant pointer to a variable/object;
for instance, when taking a function address, or when you want to protect a pointer from
unintended modifications such as assignment of new address, pointer arithmetic, etc. In
fact, an objects this is a constpointer. A constant pointer is declared:
10. What is the difference between "overloading" and " overriding?
Overloading: by overloading a function we can use the same function for performing
similar operations on different data types.
Overriding: by overriding a function we can use the same function name and parameters
but perform different operations on data types.
1. Overloading: It occurs when two methods belong to same class.
2. Overriding occurs when there is inheritance.
3. In overriding the signature must be same but here just the code changes.
4. Overloaded methods should differ in any one of the following aspects
a. Number of arguments,
b. Type of arguments,
c. Order of arguments.
11. What is a null pointer?
When referring to computer memory, a null pointer is a command used to direct a
software program or operating system to an empty location in the computer memory.
Commonly, the null pointer is used to denote the end of a memory search or processing
event. In computer programming, a null pointer is a pointer that does not point to any
objects or function. In the programming language c, NULL is an available command that
can be used.
12. Explain the need for "Virtual Destructor".
If both the base class and derived class can have the constructors, if the derived
class is to call the constructor of its own using its object, it can but when it calls its
destructor, the compiler calls the destructor of base class. Thats why the virtual
destructor concept is coined. But there are no virtual constructors.
13. How many levels of pointers can you have?

MAIL_TO :
rajeshkumar.mannu@yahoo.com

MOST IMPORTANT FAQ in C


The answer depends on what you mean by levels of pointers. If you mean How many
levels of indirection can you have in a single declaration? the answer is At least 12.
int i = 0;
int *ip01 = & i;
int **ip02 = & ip01;
int ***ip03 = & ip02;
int ****ip04 = & ip03;
int *****ip05 = & ip04;
int ******ip06 = & ip05;
int *******ip07 = & ip06;
int ********ip08 = & ip07;
int *********ip09 = & ip08;
int **********ip10 = & ip09;
int ***********ip11 = & ip10;
int ************ip12 = & ip11;
************ip12 = 1; /* i = 1 */
The ANSI C standard says all compilers must handle at least 12 levels. Your compiler
might support more.
14. what is the difference between malloc and calloc?
MALLOC ( )
1. malloc takes only the "size" of the memory block to be allocated as input parameter.
2. malloc allocates memory as a single contiguous block.
3. if a single contiguous block cannot be allocated then malloc would fail.
CALLOC ( )
1. calloc takes two parameters: the number of memory blocks and the size of each block
of memory
2. calloc allocates memory which may/may not be contiguous.
3. all the memory blocks are initialized to 0.
4. it follows from point 2 that, calloc will not fail if memory can beallocated in noncontiguous blocks when a single contiguous blockcannot be allocated.
15. How do you use a pointer to a function?
The hardest part about using a pointer-to-function is declaring it. Consider an example.
You want to create a pointer, pf, that points to the strcmp() function.
The strcmp() function is declared in this way:
int strcmp(const char *, const char * )
To set up pf to point to the strcmp() function, you want a declaration that looks just like
the strcmp() functions declaration, but that has *pf rather than strcmp:
int (*pf)( const char *, const char * );
After youve gotten the declaration of pf, you can #include <string.h> and assign the
address of strcmp() to pf: pf = strcmp;

MAIL_TO :
rajeshkumar.mannu@yahoo.com

MOST IMPORTANT FAQ in C

16 what is the difference between function pointer an pointer to function?


we can create a pointer to function. so we can call the function with that pointer.
eg
let us take two functions
void sum(int a,int b);
void sub(int a,int b);
we can create pointer to these functions
void (*p)(int,int);
p=sum
p(3,2)------gives the sum of two numbers
p=sub
p(3,2) ---gives the difference of two numbers

MAIL_TO :
rajeshkumar.mannu@yahoo.com

Вам также может понравиться