Switch to full style
C++ code examples
Post a reply

Dynamic Array Allocation

Thu Nov 13, 2008 3:07 pm

Array allocation dynamically.
cpp code
#include <iostream>

using namespace std;

// Read in the indicated number of numbers, then print them backwards.
void rne(int arr[], int size)
{
for(int i = 0; i < size; ++i)
cin >> arr[i];

cout << "----------------------------------------------------" << endl;
for(int i = size-1; i >= 0; --i)
cout << arr[i] << " ";
cout << "\n----------------------------------------------------"
<< endl;

}

main()
{
// Fixed size.
int fred[5];
rne(fred, 5);

// Allocate with malloc (C style).
int size;
cin >> size;
int *arr = (int *)malloc(size * sizeof(int));
rne(arr, size);
free(arr);

// Allocate with new (C++ style).
cin >> size;
arr = new int[size];
rne(arr, size);
delete [] arr;
}




Post a reply
  Related Posts  to : Dynamic Array Allocation
 dynamic array as global variables     -  
 Dynamic Domain Name System (Dynamic DNS )     -  
 How do you 301 redirect a dynamic URL?     -  
 make Dynamic Bar Chart by php     -  
 How to generate dynamic 3d pie chart in php???     -  
 DYNAMIC DATES IN MYSQL     -  
 dynamic crystal report generation     -  
 how to implement Dynamic DDS (Domain Name System)     -  
 Can't insert dynamic link in jstl     -  
 Array difference for associate array     -  

Topic Tags

C++ Arrays