Write a class, such that user can create its object on heap (using new) but user should not be able to create its object on stack or data area. i.e if the class is MyClass, then MyClass stackObj; // Should give compile time error. MyClass *heapobj = new MyClass; // This should be OK, & […]
How will you find the size of a structure in C/C++ language without using sizeof operator. (Note: sum of sizes of all fields of a structure may not be equal to the exact memory allocated to the structure. See, Method-1 in solution).
A process executes the below code: fork(); fork(); fork(); How many child processes will be created ? What is the meaning of fork command ?
sizeof operator returns size in bytes. For example, In an implementation where 2 bytes (16bits) of memory is allocated to an int type, the below code will output 2 int x = 10; printf(“%d”, sizeof(x)); // Output amount of memory allocated to int data type. We talked about the sizeof operator in great detail in the last post. If we […]
sizeof is a unique operator in C/C++ language which returns the amount of memory (in bytes) allocated to its operand. For example, if an implementation stores an integer in 2 bytes then both the statements below will output 2. printf(“%d”, sizeof(int)); int x = 10; printf(“%d”, sizeof x); Note than sizeof operator is used like a […]
Functions malloc, calloc, realloc and free are used to allocate /deallocate memory on heap in C/C++ language. These functions should be used with great caution to avoid memory leaks and dangling pointers. How are these functions different (or similar)?