Dynamic Memory Allocation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
computer science crazy
Super Moderator
******

Posts: 3,048
Joined: Dec 2008
#1
08-04-2009, 07:44 AM


In computer science dynamic memory allocation is the allocation of memory storage for use in a computer program during the runtime of that program. It is a way of distributing ownership of limited memory resources among many pieces of data and code. A dynamically allocated object remains allocated until it is deallocated explicitly, either by the programmer or by a garbage collector this is notably different from automatic and static memory allocation. It is said that such an object has dynamic lifetime.

The problem of fulfilling an allocation request, which involves finding a block of unused memory of a certain size in the heap, is a difficult problem. A wide variety of solutions have been proposed, including:

1)Free lists
2)Paging
3)Buddy memory allocation

The main problem for most dynamic memory allocation algorithms is to avoid both internal and external fragmentation while keeping both allocation and deallocation efficient. Also, most algorithms in use have the problem that a large number of small allocations can cause wasted space due to collecting metadata; thus most programmers avoid this, sometimes by using a strategy called chunking.
Use Search at http://topicideas.net/search.php wisely To Get Information About Project Topic and Seminar ideas with report/source code along pdf and ppt presenaion
Reply
seminar class
Active In SP
**

Posts: 5,361
Joined: Feb 2011
#2
07-03-2011, 12:43 PM


.docx   Dynamic Memory Allocation.docx (Size: 28.21 KB / Downloads: 56)
Dynamic Memory Allocation
Memory Allocation:

There are two types of memory allocation.
1) Static memory allocation -- allocated by the compiler. Exact size and type of memory must be known at compile time.
2) Dynamic memory allocation -- memory allocated during run time. Exact sizes or amounts (like the size of an array, for example) does not have to be known by the compiler in advance. This memory is allocated by the run time system. To allocate memory dynamically, we have to use pointers.
Dynamic Memory Allocation:
To dynamically allocate memory in C++, we use the new operator. To de-allocate dynamic memory, we use the delete operator. Dynamic memory is created in an area of memory often referred to as the "free store", or the "heap". We can only allocate space during run time. We cannot create new variable names during run time -- all identifiers must be known by the compiler. For this reason, creating dynamic memory involves two steps:
1. Creating the dynamic space.
2. Storing its address in a statically allocated pointer (which has a name).
This gives us a name by which we can refer to the dynamic data. We always access dynamically allocated space through a pointer.
For step (1), use the new operator, followed by the type being allocated.
new int; // dynamically allocates an int
new double; // dynamically allocates a double
new Fraction; // dynamically allocates a Fraction object
new int[40]; // dynamically allocates an array of 40 ints
These statements above are not very useful by themselves, because the allocated spaces have no names! How do we get to them?
The new operator returns the address of the allocated space, so we must capture this in a pointer (with an assignment statement).
int * p; // declare a pointer p
p = new int; // dynamically allocate an int and load address into p
double * d; // declare a pointer d
d = new double; // dynamically allocate a double and load address into d
// we can also do these in single line statements
Fraction * fp = new Fraction;
int x = 40;
int * ptr = new int[x];
Notice that this last example shows that a variable can be used as the size, when creating an array dynamically, since the allocation is happening at run time. This is different from the static allocation, which requires a constant value for the size.
Dynamically allocating objects:
We see from the examples above that this works on user-defined types as well (like Fraction). However, don't forget that the Fraction constructor has to run when we create a Fraction object. If the class has a default constructor, then this is the one that runs unless you specify otherwise. To use a different constructor, just pass in the parameters after the dynamic allocation:
Fraction * fp = new Fraction(3,5); // initializes object to 3/5
This gives us a name by which we can refer to the dynamic data. We always access dynamically allocated space through a pointer.
For step (1), use the new operator, followed by the type being allocated.
newint; // dynamically allocates an int
new double; // dynamically allocates a double
new Fraction; // dynamically allocates a Fraction object
newint[40]; // dynamically allocates an array of 40 ints
These statements above are not very useful by themselves, because the allocated spaces have no names! How do we get to them?
The new operator returns the address of the allocated space, so we must capture this in a pointer (with an assignment statement).
int * p; // declare a pointer p
p = new int; // dynamically allocate an int and load address into p
double * d; // declare a pointer d
d = new double; // dynamically allocate a double and load address into d
// we can also do these in single line statements
Fraction * fp = new Fraction;
int x = 40;
int * ptr = new int[x];
Notice that this last example shows that a variable can be used as the size, when creating an array dynamically, since the allocation is happening at run time. This is different from the static allocation, which requires a constant value for the size.
Dynamically allocating objects: We see from the examples above that this works on user-defined types as well (like Fraction). However, don't forget that the Fraction constructor has to run when we create a Fraction object. If the class has a default constructor, then this is the one that runs unless you specify otherwise. To use a different constructor, just pass in the parameters after the dynamic allocation:
Fraction * fp = new Fraction(3,5); // initializes object to 3/5
Deleting dynamic memory:
You never need to worry about getting rid of statically allocated memory. Those variables are automatically handled by the compiler, which has already determined their scope and their lifetime. However, anything you create dynamically will not be taken care of by the compiler. It is up to you to clean this memory up when you are done with it. To deallocate dynamically allocated memory, apply the delete operator to the pointer, and it will delete the dynamic memory that the pointer is pointing to. (Note: This does not deallocate the pointer).
deletefp; // deletes the fraction pointed to by fp
delete [] ptr; // deletes the array allocated in the example above
When deallocating a dynamic array, use the form:
delete [] pointername
Notation -- The arrow operator:
If we dynamically allocate an object, and therefore can only refer to it with a pointer, then how do we call its public member data or member functions? The usual format is: objectName.memberName
Consider the following:
Fraction * fp = new Fraction; // fp is a pointer to a dynamic Fraction object
How do we call the Show() function for this object? We don't have a separate name for the object, but we can refer to it by de-referencing the pointer. So, the object's effective name is: *fp
So, here are some calls to Fraction member functions:
(*fp).Show();
(*fp).Get();
(*fp).Evaluate();
Note: The parintheses here are essential, because the dot-operator has higher precedence than the * operator, and we need to dereference the pointer first! This statement would be incorrect:
*fp.Show(); // this would be the same as *(fp.Show()); which is syntactically incorrect
Since having to deal with the parinthesesisnotationally yucky (a technical computer term), we have another operator for these situations that is nicer to use -- the arrow operator -> This operator can be used instead of the dot operator when are accessing objects through pointers. Here are the equivalent function calls with the arrow operator:
fp->Show();
fp->Get();
fp -> Evaluate(); // note that the spacing is irrelevant. Just keep the arrow together ->
Reply

Important Note..!

If you are not satisfied with above reply ,..Please

ASK HERE

So that we will collect data for you and will made reply to the request....OR try below "QUICK REPLY" box to add a reply to this page

Quick Reply
Message
Type your reply to this message here.


Image Verification
Please enter the text contained within the image into the text box below it. This process is used to prevent automated spam bots.
Image Verification
(case insensitive)

Possibly Related Threads...
Thread Author Replies Views Last Post
  Computer Memory Based on the Protein Bacteriorhodopsin seminar projects crazy 16 9,050 06-09-2015, 04:54 PM
Last Post: Larbaski
Video Random Access Memory ( Download Full Seminar Report ) computer science crazy 2 2,423 10-05-2014, 09:44 AM
Last Post: seminar project topic
  Towards Reliable Data Delivery for Highly Dynamic Mobile Ad Hoc Networks seminar ideas 11 3,979 02-04-2014, 12:50 PM
Last Post: Guest
  Memory Management: Overlays and Virtual Memory ppt seminar projects maker 0 491 20-09-2013, 04:57 PM
Last Post: seminar projects maker
  Virtual Memory ppt project girl 1 794 11-09-2013, 03:13 PM
Last Post: seminar projects maker
  Report on Exploiting Dynamic Resource Allocation for Efficient Parallel Data study tips 0 379 21-08-2013, 04:36 PM
Last Post: study tips
  Multi-Path Routing and Rate Allocation for Multi-Source Video On-Demand Streaming pdf study tips 0 358 30-07-2013, 02:41 PM
Last Post: study tips
  Dynamic synchronous Transfer Mode (DTM) ppt study tips 1 2,160 05-07-2013, 02:44 PM
Last Post: study tips
  Report on Dynamic network load balancing study tips 0 311 05-07-2013, 12:23 PM
Last Post: study tips
  Computer Memory ppt study tips 0 430 27-06-2013, 04:55 PM
Last Post: study tips