Dynamic Memory Allocation

Can anyone explain it?

Page 1 of 1

4 Replies - 1937 Views - Last Post: 24 January 2007 - 03:08 PM Rate Topic: -----

#1 mattman059  Icon User is offline

  • Epic Awesomeness
  • member icon

Reputation: 15
  • View blog
  • Posts: 538
  • Joined: 23-October 06

Dynamic Memory Allocation

Post icon  Posted 16 January 2007 - 01:20 PM

We're about to start covering Dynamic Memory Allocation in my intermediate programming class and i was wondering if there was anyone who could help me understand it a little better. Right now im not to sure what its all about.

Thanks for your help
Is This A Good Question/Topic? 0
  • +

Replies To: Dynamic Memory Allocation

#2 BitByte  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 194
  • Joined: 09-August 06

Re: Dynamic Memory Allocation

Posted 16 January 2007 - 02:50 PM

Dynamic memory allocation is quite easy to get to grips with. All you are doing is asking for enough memory to hold your specified amount of data, be it ints doubles or whatever.

When you ask for memory all you need to do is use 'new', well, not on it's own.

Example: You want enough space in memory for an array of ints, but you don't know until the program is run how much space you are going to need. So, we have to ask for it.

// start by declaring an int variable.
int val;

// Ask the user for some random value.
std::cin >> val;

// Now with that value you know how many int's you need.
// Lets get enough memory to hold that many ints in an array.
int *my_array = new int[val];


Now the memory has been given to you. my_array points to the address
in memory where our reserved space is. You have enough memory to hold val amount of ints. Most important is that you should always free the memory when you have finished with it. If you don't free the memory you will end up with a memory leak, either your program will crash, or in the worst case scenario you can bring down the os. You can free the memory by using delete to give the memory back to the os for use by some other application.

delete [] my_array;


Only use the brackets if it is an array when using delete. Dynamic memory is not just limited to arrays. Anywhere you want memory and don't know until runtime how much you need you can use dynamic allocation. Even structures.

Hope this help you a bit.
Was This Post Helpful? 0
  • +
  • -

#3 mattman059  Icon User is offline

  • Epic Awesomeness
  • member icon

Reputation: 15
  • View blog
  • Posts: 538
  • Joined: 23-October 06

Re: Dynamic Memory Allocation

Posted 17 January 2007 - 04:30 PM

:D Yeah it helped a lot! Really cleared some things up for me.

Thanks a lot!
Was This Post Helpful? 0
  • +
  • -

#4 mattman059  Icon User is offline

  • Epic Awesomeness
  • member icon

Reputation: 15
  • View blog
  • Posts: 538
  • Joined: 23-October 06

Re: Dynamic Memory Allocation

Posted 24 January 2007 - 02:35 PM

Okay i understand how to use Dynamic Memory Allocation on 1D arrays..but what about multidimensional?

Ex. i have an array myArray[80][40] which holds how many rows of seats and columns of seats are in a theatre. I want to be able to let the user tell how many rows/cols there should be.

is it the same as before?

int Row, Col;
cin >> Row;
cin >> Col;

int *myArray = new int[Row] int[Col];

?
Was This Post Helpful? 0
  • +
  • -

#5 BitByte  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 194
  • Joined: 09-August 06

Re: Dynamic Memory Allocation

Posted 24 January 2007 - 03:08 PM

I did an example so you can look at it and learn from it. Arrays are really messy though. When you get to vectors you will be glad to use them. None of this dynamic rubbish. Anyway, hope this helps:

#include <iostream>

int main( void )
{
	std::cout << "Enter a number: ";
	int row;
	std::cin >> row;

	std::cout << "Enter another number: ";
	int col;
	std::cin >> col;

	int **my_array; // A pointer to a pointer to an array of ints.
	// Each row element 0 is a pointer to a column of ints.
	my_array = new int *[row];
	
	for ( int i = 0; i < row; i++ )
	{
		// Create an array of cols for each row.
		my_array[i]= new int [col];
	}

	// Fill the 2D array with ones as an example.
	for ( int i = 0; i < row; i++ )
	{
		for ( int j = 0; j < col; j++ )
		{
			my_array[i][j] = 1;
			std::cout << my_array[i][j] << ' ';
		}

		std::cout << std::endl;
	}

	// Then you need a loop to delete all the columns.
	// *** This must be done first ***
	for ( int i = 0; i < col; i++ )
	{
		delete [] my_array[i];
	}

	// Now finish by deleting the rest of the array.
	delete [] my_array;

	// That's it, dynamically allocating and deleting the array.
}

This post has been edited by BitByte: 24 January 2007 - 03:08 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1