Explain static and dynamic memory allocation with an example each.
Answer / Shailendra Singh Chauhan
Static Memory Allocation: Static memory is allocated at compile time and remains in memory for the lifetime of the program. It can be initialized or uninitialized, and its size cannot be changed during runtime. Example: n```nint arr[5]; // static array of integersn```nDynamic Memory Allocation: Dynamic memory is allocated at runtime using dynamic allocation functions such as malloc(), calloc(), realloc() or new operator. Its size can be changed during runtime and it must be deallocated using free() or delete operator to avoid memory leaks. Example: n```nint *ptr = (int*)malloc(5 * sizeof(int)); // dynamic allocation of an integer arrayndelete [] ptr; // deallocating the memory allocated by malloc()n```
| Is This Answer Correct ? | 0 Yes | 0 No |
what kind of projects are suitable for c and c++
How to change constant values?
How do I make turbo c++ full screen?
What are the five basic elements of a c++ program?
Please explain class & object in c++?
Declare a class vehicle and make it an abstract data type.
If dog is a friend of boy and boy is a friend of house, is dog a friend of house?
Why do we need constructors in c++?
what is data Abstraction? and give example
147 Answers Aaditya Info Solutions, American Express, CMS, College School Exams Tests, Data Entry Operator, First Advantage, Google, HCL, IBM, Infosys, Microsoft, Mind Links, NIIT, Oracle, Pact, QBit Systems, TCS, WAYA, Wipro,
class A { public: void f(); protected: A() {} A(const A&){} }; Examine the class declaration shown above. Why are the default and copy constructors declared as protected? 1. To ensure that A cannot be created via new by a more derived class 2. To ensure that A cannot be copied 3. To ensure that A cannot be used as a base class except when public inheritance has been used 4. To ensure that A cannot be created/copied outside the inheritance chain 5. To ensure that A cannot be instantiated as a static variable
Difference between pass by value and pass by reference?
Evaluate !(1&&1||1&&0) a) Error b) False c) True