Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory Allocation; Static and Dynamic #6

Closed
jimin-kiim opened this issue Oct 20, 2022 · 4 comments
Closed

Memory Allocation; Static and Dynamic #6

jimin-kiim opened this issue Oct 20, 2022 · 4 comments

Comments

@jimin-kiim
Copy link
Owner

jimin-kiim commented Oct 20, 2022

@jimin-kiim
Copy link
Owner Author

jimin-kiim commented Oct 20, 2022

Object Creation

Static Way

classname objectname(arguments);

Dynamic Way

classname* objectname = new classname(arguments);
heavier and slower
so it should be done only when needed

@jimin-kiim jimin-kiim changed the title Object Creation Memory Allocation; Static and Dynamic Oct 25, 2022
@jimin-kiim
Copy link
Owner Author

jimin-kiim commented Oct 25, 2022

Memory Leak

  • when the dynamic allocation is done inside the function but the function is returned without deallocating the allocated memory
  • the condition when the dynamically allocated memory cannot be accessed and deallocated anymore unless the program is terminated
  • waste of memory; the memory is not useful anymore but still allocated
  • if it's accumulated, the program just crahses
  • in the case of Java, if there's a memory leak then the garbage collection handles it. The memory garbages are automatically collected and deallocated. So in Java, there's no deallocating

@jimin-kiim
Copy link
Owner Author

jimin-kiim commented Oct 25, 2022

Dangling Pointer

  • the pointer that points to the allocated memory that should have been deallocated but not.

@jimin-kiim
Copy link
Owner Author

jimin-kiim commented Oct 25, 2022

Pros and Cons of static and dynamic memory allocation

sometimes the static memory allocation is more suitable and sometimes the dynamic memory allocation is more suitable. Understanding the pros and cons of each of them and using them in pertinent situation is necessary.

static memory allocation

  • advantage :
    • light-weight.
    • faster to be created and deleted than dynamic allocating method
    • higher speed of the program in the execution time( since the memory allocation is already performed at the compile time. )
  • disadvantage :
    • the exact size of the array must be specified when declaring the array

dynamic memory allocation

  • advantage :
    • no need to specify the exact memory size ( even in the case when I cannot expect the size of array in advance, I can declare the array)
  • disadvantage :
    • heavy-wight
    • slower speed of the program when executed( since the prodedure of allocating the memory is performed during the run time)

  • When I have to make a fast program then it's better to use the static memory allocating method
  • When I cannot expect the size of the array in advance of executing the program, it's better to use the dynamic memory allocating method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant