Recently Updated C++ And Oracle Interview Questions Part – 5
What Is Realloc() And Free()? What Is Difference Between Them?
void* realloc (void* ptr, size_t size) : This function is used to change the size of memory object pointed by address ptr to the size given by size. If ptr is a null pointer, then realloc will behave like malloc(). If the ptr is an invalid pointer, then defined behaviour may occur depending the implementation.
Undefined behaviour may occur if the ptr has previously been deallocated by free(), or dealloc() or ptr do not match a pointer returned by an malloc(), calloc() or realloc().
void free (void* ptr) : This function is used to deallocate a block of memory that was allocated using malloc(), calloc() or realloc(). If ptr is null, this function does not doe anything.
What Is Difference Between Shallow Copy And Deep Copy? Which Is Default?
When you do a shallow copy, all the fields of the source object is copied to target object as it is. That means, if there is a dynamically created field in the source object, shallow copy will copy the same pointer to target object. So you will have two objects with fields that are pointing to same memory location which is not what you usually want.
In case of deep copy, instead of copying the pointer, the object itself is copied to target. In this case if you modify the target object, it will not affect the source. By default copy constructors and assignment operators do shallow copy. To make it as deep copy, you need to create a custom copy constructor and override assignment operator.
What Do You Mean By Persistent And Non Persistent Objects?
Persistent objects are the ones which we can be serialized and written to disk, or any other stream. So before stopping your application, you can serialize the object and on restart you can deserialize it. [ Drawing applications usually use serializations.]
Objects that can not be serialized are called non persistent objects. [ Usually database objects are not serialized because connection and session will not be existing when you restart the application. ]
Is It Possible To Get The Source Code Back From Binary File?
Technically it is possible to generate the source code from binary. It is called reverse engineering. There are lot of reverse engineering tools available. But, in actual case most of them will not re generate the exact source code back because many information will be lost due to compiler optimization and other interpretations.
What Is The Difference Between #include And #include “file” ?
We use # include to include a file. The difference between two ways of file inclusion lies in the order in which preprocessor searches for the file specified. When the preprocessor encounters #include statement, it looks for the file specified in the angled brackets in the default location (Path defined in INCLUDE environment variable of the system).
When # include followed by file name in double quotation marks is encountered by the preprocessor, it looks for the file in the current directory. If the file is not found in the current directory, it will look for the file in the default location.
Can #include Handle Other File Formats Than .h?
Yes. Irrespective of the file type, Preprocessor will do its job and will include any file like test.z.
What Is A Void Pointer?
A void pointer is a special pointer type which can point to any data type without letting the compiler know. It helps to pass a pointer to some function of any type which can be decided at run time. In the following example input parameter a and b can be both integer and string.
Void PointerVoid(int type, void *a, void *b, void *c)
{
if(type == 1)/* int*/
*c = ((int) *a) + ((int)*b);
else /*string*/
sprintf((char*)c,”%s%s”,(char*)a,(char*b));
}
What Is The Value Of Null?
The value of NULL is 0 or (void*)0. Whenever NULL has to be compared with some variable or assigned to a variable, depending upon the type of that variable, the value of NULL will be decided.
Can The Size Of An Array Be Declared At Runtime?
No. The size of an array must be stated at the time of compilation. Alternate way is to use dynamic allocation by calloc or malloc.
What Is The Difference Between Malloc() And Calloc()?
Calloc:
Allocates a region of memory large enough to hold “n” elements of “size” bytes each.
Calloc initializes the whole memory with 0
Malloc:
Allocates “size” bytes of memory.
Malloc does not change the existing memory. So it returns a memory chunk with garbage value.
What Is The Heap In Memory?
The heap is where malloc(), calloc(), and realloc() get memory. The allocation of memory from the heap is much slower than the stack. But, the heap is much more flexible about memory allocation than the stack. Memory can be allocated and deallocated in any time and order. This heap memory isn’t deallocated by itself, method free() has to be called in order to do so.
What Will Be The Output Of The Following Code Snippet?
float num1 = 6 / 4;
float num2 = 6 / 4.0;
printf(“6/4 == %f or %fn”, num1, num2);
Output will be : 6/4 == 1.000000 or 1. 500000. This is a case of operator promotion. The variable num1 is set to “6/4”. Because, both 3 and 4 are integers. So integer division is performed on them and the result is the integer 0. The variable num2 is set to “6/4.0”. Because 4.0 is a float, the number 6 is converted to a float as well, and the result will be floating value 1.5.
What Happens If You Free A Pointer Twice?
It is really dangerous to free the same memory twice. If the memory has not been reallocated in between, it will generate a “double free” error, since the memory location has already been freed.
How Does Free() Method Know About How Much Memory To Release?
There’s no concrete way. Most systems, keeps a track of each memory block as linked lists. When memory is allocated, all the blocks that are given to that particular call are put into a linked list and the size, block number and serial number are written in the head node.
There is no assurance, though. But in some way or other, the system keeps track of each block to know the size of each allocated portion of the heap.
How To Restrict A Header File From Including More Than Once?
In C, to avoid double inclusion, we use a include guard also known as macro guard. It is #ifndef – #endif pair. “ifndef” is an indication of “if not defined”.
#ifndef FAMILY_H
#define FAMILY_H
struct Example
{
int member;
};
#endif /* FAMILY _H */
How To Print An Address?
The best way is to use “%p” in printf() or fprintf. The “%p” will tell compiler to use the best type to use, while printing the address according to the environment, since the size of a pointer changes from system to system.
Explain Recursive Functions? Also Explain The Advantages And Disadvantages Of Recursive Algorithms?
A recursive function is a function which calls itself.
The advantages of recursive functions are:
A substitute for very complex iteration. For example, a recursive function is best to reduce the code size for Tower of Hanoi application.
Unnecessary calling of functions can be avoided.
The disadvantages of Recursive functions:
The exit point must be explicitly coded ,otherwise stack overflow may happen
A recursive function is often confusing. It is difficult to trace the logic of the function. Hence, it is difficult to debug a recursive function.