Tag Archives: raw pointer

The complete guide to a C++ raw pointer

The complete guide to a C++ raw pointer
SomeClass *ptr=SomeClass();
SomeClass *ptr=SomeClass;

These two definitions are equivalent. Pointers can be slow and can be harder to debug pointer airthmatic code and data involving multiple pointers. Always set pointers to null after freeing otherwise your check for p != NULL will be useless which could cause memory corruption.
A dangling pointer arises when you the address of a pointer after its lifetime is over. This occurs when you are returning addresses of automatic variables from a function or using the address of a memory block that been freed.
The following code will not generate any errors unless indicated in the comments:

using namespace std;

class base {
int i;
void set_i(int x) { i = x; }
class derived: base {};

int main() {
//this does not generate error
/* int *p,*q;
cout<<"p"<FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

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.

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!