- On some compilers:
- int is 4 bytes
- long int 4 bytes
- double int 8 bytes
- float = 4 bytes
- long float = 4 bytes
- char = 1 bytes
- signed and unsigned will not affect the size. They will affect the value range
- Pointer always has same size regardless of where it is pointing.
- Example of Enum:
- enum color{Black,white};
- color c = white;
- cout<<c<<endl; //c will have the value 0 or 1 for black or white respectively.
- break; breaks the inner loop only not the outer loop.
- These all math functions work on: double, float, long double. Since, these functions work on the floating point numbers, we have to use typecasting (int).
- fabs: Computes the absolute value of a floating point no.
- abs: Computes the absolute for the integers
- exp: To compute e^x, where x is a floating point value
- sqrt: Finds the sqrt
- Max and Min functions are present in the algorithm library.
- Memory allocated by malloc, calloc, realloc might not be compatible with the one allocated by new. So manipulation is needed. Hence, malloc and delete & new and free cannot be used interchangeably.
- void as a data type is not supported
- Dynamic memory is allocated from system heap
- If you call break before returning a value then a junk value is returned.
This pointer basics
- this pointer stores the address of the class instance, to enable pointer access of the members to the member functions of the class.
- this pointer is not counted for calculating the size of the object.
- this pointers are not accessible for static member functions.
- this pointers are not modifiable.
Example:
class A { int i; public: int get() { return this->i; } void set(int k) { this->i = k; } };
Shallow copy
Shallow copy will just copy one pointer to another pointerEx:
void shallow_copy(test *s,test *t) { s->ptr = t->ptr; }
Deep Copy
Deep copy will allocate the space for the target then using memcpy to copy the value from the source to the target.Ex:
void deep_copy(test *s,test *t) { t->ptr = (char*)malloc(sizeof(s) + 1); memcpy(t->ptr,s->ptr,strlen(s->ptr)+1); }
Name hiding
Name hiding occurs when:There is overloading in the base class and inheritance involved.
Ex:
base: display(); display(int i); derived: display();
derived_instance.display(1); //It will not work since it will confuse the compiler
How does delete work
delete p: It will delete the memory for single character
delete []p: It will delete the memory for an array
Different ways to access pointers
- *(p+i)
- *(i+p)
- p[i]
- i[p]
Why do we use pointer to a pointer in BST?
- Because we have the nodes and the traversal in the form of the pointers.
- To traverse we need a pointer.
No comments:
Post a Comment