Explain static and dynamic memory allocation with an example each.



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

Post New Answer

More C++ General Interview Questions

what kind of projects are suitable for c and c++

1 Answers  


How to change constant values?

6 Answers   Huawei, Symphony,


How do I make turbo c++ full screen?

1 Answers  


What are the five basic elements of a c++ program?

1 Answers  


Please explain class & object in c++?

1 Answers  


Declare a class vehicle and make it an abstract data type.

1 Answers  


If dog is a friend of boy and boy is a friend of house, is dog a friend of house?

1 Answers  


Why do we need constructors in c++?

1 Answers  


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

1 Answers  


Difference between pass by value and pass by reference?

1 Answers  


Evaluate !(1&&1||1&&0) a) Error b) False c) True

1 Answers  


Categories