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


Please Help Members By Posting Answers For Below Questions

How c functions prevents rework and therefore saves the programers time as wel as length of the code ?

1165


Can union be self referenced?

1267


daily Routine of father

1485


What is the latest version on c++?

1208


What character terminates all character array strings a) b) . c) END

1395