7 Replies - 4737 Views - Last Post: 23 January 2009 - 11:16 AM Rate Topic: -----

#1 emerald_gal  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 64
  • Joined: 21-November 08

Dynamic memory allocation

Post icon  Posted 22 January 2009 - 01:49 AM

I want to ask why it is said that dynamic memory allocation is efficient but slower than the static memory allocation.
Is This A Good Question/Topic? 0
  • +

Replies To: Dynamic memory allocation

#2 FrozenSnake  Icon User is offline

  • En man från Sverige!

Reputation: 122
  • View blog
  • Posts: 995
  • Joined: 30-July 08

Re: Dynamic memory allocation

Posted 22 January 2009 - 01:51 AM

Googled "dynamic memory allocation is efficient but slower" and found this:

Dynamic RAM (eg.-DDR) refreshes data stored over it vigorously to retain it for along time. Therefore slower as compared to static.

Hope it is the answer you where looking for.
Was This Post Helpful? 0
  • +
  • -

#3 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Dynamic memory allocation

Posted 22 January 2009 - 02:01 AM

Static memory is of predetermined size, you can't change the size of the allocated memory. When you store something in the memory, the computer knows where to put it and later, where to find it. Dynamic memory allows you to change the size of the allocated memory but what do you think happens if the additional space you want to allocate is already occupied, then something needs to be done. You could for example use a memorypointer to transfer to another position in memory where you can continue storing data but following a pointer takes time of course. Alternatively you could search for a space where all the data can be fitted and move it there. This would also take considerable time.
Was This Post Helpful? 1
  • +
  • -

#4 codemax88  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 40
  • Joined: 07-February 07

Re: Dynamic memory allocation

Posted 22 January 2009 - 09:52 PM

View PostFrozenSnake, on 22 Jan, 2009 - 12:51 AM, said:

Googled "dynamic memory allocation is efficient but slower" and found this:

Dynamic RAM (eg.-DDR) refreshes data stored over it vigorously to retain it for along time. Therefore slower as compared to static.

Hope it is the answer you where looking for.

Your answer applies to hardware not software. Eneraldgal asked about memory allocation pertaining to software.
The semiconductor material(components) determines the operating characteristics of both types.
Goin's answer refer to software. :)
Was This Post Helpful? 0
  • +
  • -

#5 emerald_gal  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 64
  • Joined: 21-November 08

Re: Dynamic memory allocation

Posted 22 January 2009 - 11:42 PM

Thats correct Goin's answer refers to software and i asked about the software not hardware, but still i am not clear about it, i know it is efficient but why is it slow?
Was This Post Helpful? 0
  • +
  • -

#7 codemax88  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 40
  • Joined: 07-February 07

Re: Dynamic memory allocation

Posted 23 January 2009 - 01:11 AM

View Postemerald_gal, on 22 Jan, 2009 - 10:42 PM, said:

Thats correct Goin's answer refers to software and i asked about the software not hardware, but still i am not clear about it, i know it is efficient but why is it slow?

Dynamic memory allocation is more efficient because memory locations are created at the time the program is ran. The reason why dynamic memory allocation is slow because it requires more overhead ( processing ..cpu cycles) to keep track of variables, storage locations using pointers etc
Just like in Goln's example "Dynamic memory allows you to change the size of the allocated memory but what do you think happens if the additional space you want to allocate is already occupied [...}"? Since Dynamic memory locations are not set-up like static allocation before hand, it may try to store data in areas that are already occupied, which requires the program to transfer data from one location to another to create space. This causes Dynamic memory allocation to be slow.
Was This Post Helpful? 1

#9 Linkowiezi  Icon User is offline

  • D.I.C Regular

Reputation: 58
  • View blog
  • Posts: 316
  • Joined: 07-October 08

Re: Dynamic memory allocation

Posted 23 January 2009 - 01:18 AM

Static memory:
The ammount of memory is predefined and will be allocated at the time the application starts and the computer knows where it is. This might result in the application taking longer to start, but runs faster since you can't tamper with the memory as much later on and everything is in its place where the computer knows where to find it.

Dynamic memory:
Memory is not allocated at runtime and you will have the possibility of creating new and/or deleting memory allocated this way. If it is a list this list may need pointers to point to the next piece in the list because it might not be next to the previous item. Creating and searching in this kind of list takes longer time than if nothing of these thing have to be done while the application is running.
But you might want to take this approach because you don't know how much memory your program will be using. And it will let you delete items in the middle of the list without having to rearrange the whole list, just delete one item in the middle and let the previous point to the new next item in the list.

Ex:
Dynamic memory can be a very good approach if you have a huge program and can't afford to make every array 'exArray[10000]' because at some point the program might use 10000 slots, but mostly it uses 3. Then if you have 10000 arrays that might require 10000 slots each that stores arrays with 10000 slots, then static allocation would be 'exArray[10000][10000][10000]' witch would eat quite a bit of memory compared to if you would use dynamic memory. Then you would probably still have your initial 10000 arrays but they can take up as many slots as they need 'exArray[10000][3][3]'. They might take a little longer time to access for the computer but on the other hand... you have plenty of memory left for other things such as textures or whetever you want.

Hope this made things a bit clearer for you :)
Was This Post Helpful? 1

#13 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: Dynamic memory allocation

Posted 23 January 2009 - 11:16 AM

Dynamic Allocation gives you more usability with Pointers

type* Pointer = NULL; // it doesn't point to any

Pointer = new type; // Pointer to allocated Object

*Pointer++; // increment by dereferencing the Pointer

delete Pointer; // delete the Object

Pointer = new type[ size ]; // create a dynamic array

*Pointer // the first element
*(Pointer+3) // the 4th element
Pointer[2] // using array style

delete [] Pointer; // delete the dynamic array

note: Pointers and Dynamic Allocation gives you more usability
especially when you have Inheritance Classes from Base

|"""""""""""""||------------[ Derived Class A ]
| Base Class ||
|_________.||------------[ Derived Class B ]

A One( ...);
B Two( ...);

Base** Pointer = Base*[2];
Pointer[0] = &One;
Pointer[1] = &Two;

for(int i=0;i<2;i++)
. Pointer[i]->Virtual_Function( ...);

:: hopefully that was Helpful, if it happened to be ...
:: please, press "this post was helpful" ...

This post has been edited by Plus: 23 January 2009 - 11:27 AM

Was This Post Helpful? 2

Page 1 of 1