adspace
Explain static and dynamic memory allocation with an example each.
Answer Posted / 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 View All Answers