Tag Archives: pointer

Pointer job interview questions involving C++ pointers and raw pointers

Pointer job interviews involving C++ pointers and raw pointers
The general difference between a reference and a pointer is that pointers can be null while references cannot be. Also, references needed to be initialized when created, cannot reseat one reference to an object, and cannot take a reference of a reference, but a pointer of pointer is possible. References should be used to define ‘attractive’ interfaces while pointers should be used to implement data structures and implement algorithms.
Shallow copy versus deep copy
Shallow copy means copy each bit of one member block to another. Also, if pointers are involved so new memory location will be created and data is copy into that location.
A deep copy (also known as a hard copy) involves recursively copying variables in a nested structure or in a nested object. This also involves copying from one structure to another if there are pointers in the structure, then the second pointer will point the same as the first one. This will copy all fields and also makes copies of dynamically allocated memory pointed by the fields. In order to deep copy, you must create a copy constructor which will overload the assignment operator otherwise it will copy and point to the original with disastrous results.
The difference between calloc and malloc are:
There are 2 arguments needed for calloc while malloc only needs one. The initializing of memory in calloc is zero.
Malloc reduces the need to request more memory from the operating system since it is done on a user by user basis. It recycles the freed memory space. When pointers are created and free ( pointer) is called, the header and foot are looked at by the memory allocator. The header contains information on necessary items including the size of space taken up, status if the memoery block is freed or allocated, and know if it can be removed or not.
When pointers are allocated, two bytes are used with the size of the memory block. The memory block can be assigned based on best fit or worst fit. This free memory list cannot a single linked list. This linked ist could be a balanced tree.
Sizes of memory could be:
Char a[] = “ “; could be two bytes while
Char b[10] would be 10.
a will contain two bytes where one is null and the other is ‘\0’. The trick is to remember that it will allocated to characters to this array. In short, a would be whatever is in the quotes plus a null characters as well.
Also, if you do char *c, size would be 4 in 32 bit systems but will be 8 in 64 bit systems so if you do:
b=c; size would become 4 as well.
Also, note char a[]=”hello” will be six bytes while char *P=”Hello” will result in four bytes.

HOW DO YOU START A PROFITABLE TRADING BUSINESS? Read more NOW >>>

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

c++ interview questions on inline functions and this pointer

Inline:
What are inline functions? How would you reduce the chances of gcc making a function
Make it virtual.
agree, or use function pointer
An inline functions is used to tell the compiler to insert the complete body of the function where ever they are called. A function defined in a class definition is automatically treated as inline.

By specifying command line option -finline-limit=n while compilation.
Simplest way out, if you have a function

typename f(…)

, you do the following declaration:

typename f(…) __attribute__ ((noinline));

If you also want to disable all other possible optimisations, stick the line

asm(“”);

somewhere in the function – compiler thinks it’s got some Assembler to handle, so it will take things easy…

Making the function ‘longer and complicated’ reduces the chances of getting the function inlined.
What is inline function??
The __inline keyword tells the compiler to substitute the code within the function definition for every instance of a function call. However, substitution occurs only at the compiler’s discretion. For example, the compiler does not inline a function if its address is taken or if it is too large to inline.

This pointer:
What happen if i delete “this” in member function ? can you call member functions after doing this ?
destructor of class is called to which this pointer belongs. Memory is freed (or should say marked as free space) to be used by OS for other purposes. I think you can call other function but behavior is undefined because you don’t know when OS will allocate this memory to some other process..

You should be safe as a long as you don’t end up using any member variables. Deleting this does not affect member functions.
Interesting it is called suicide
If the memory for this object was not on the heap then the program will crash
If the client does not come to know the object has been deleted and use the object to call a function or some thing then the program will crash

What is “this” pointer?
The this pointer is a pointer accessible only within the member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.
When a nonstatic member function is called for an object, the address of the object is passed as a hidden argument to the function. For example, the following function call
myDate.setMonth( 3 );
can be interpreted this way:
setMonth( &myDate, 3 );
The object’s address is available from within the member function as the this pointer. It is legal, though unnecessary, to use the this pointer when referring to members of the class.

What happens when you make call “delete this;” ??
The code has two built-in pitfalls. First, if it executes in a member function for an extern, static, or automatic object, the program will probably crash as soon as the delete statement executes. There is no portable way for an object to tell that it was instantiated on the heap, so the class cannot assert that its object is properly instantiated. Second, when an object commits suicide this way, the using program might not know about its demise. As far as the instantiating program is concerned, the object remains in scope and continues to exist even though the object did itself in. Subsequent dereferencing of the pointer can and usually does lead to disaster.
You should never do this. Since compiler does not know whether the object was allocated on the stack or on the heap, “delete this” could cause a disaster.

HOW DO YOU START A PROFITABLE TRADING BUSINESS? Read more NOW >>>

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!