< C++ .NET Exception Handling 7 | Main | C++ .NET Arrays & Collections 2 >



Arrays and Collections 1



What we have in this page?


  1. Introduction

  2. Native C++ Arrays


  1. Passing Arrays to Functions

  2. How Do Native Arrays Work?

  3. Initializing Arrays

  4. Multidimensional Arrays

  5. Dynamic Allocation and Arrays

  6. Problems with Manual Memory Management

  7. Managed Extension for C++: __gc Arrays, The Old Syntax

  8. Using the __gc and __nogc Keywords

  9. Managed Array (array)

  10. The Old Syntax vs the new Syntax

  11. (The Managed Extension for C++ vs New C++ Syntax)

  12. Arrays and Reference Types

  13. Multidimensional Managed Arrays

  14. Creating Arrays of Managed Arrays

  15. The .NET Array Class

  16. Array Members

  17. Array.AsReadOnly Generic Method Example

  18. Array.BinarySearch Method Example

  19. Array.BinarySearch Method (Array, Object) Example





This module is concerned with data structures. You’ll learn about arrays and other collection classes, and you’ll learn how to use them in your programs. In the first part of the module, you’re going to learn about two sorts of arrays: the native arrays provided by the C++ language and the .NET managed arrays, which use functionality inherited from the .NET Framework. You’ll find out how they all work and when one should be used rather than the other. The second part of the module looks more widely at the range of collection classes provided by the .NET Framework, discussing their characteristics and showing you how and when to use them.


Native C++ Arrays


Native arrays are those provided as part of the C++ language. They’re based on the arrays that C++ inherits from C. Although native arrays are designed to be fast and efficient, there are drawbacks associated with using them, as you’ll see shortly. This first exercise will introduce you to C++ native arrays by showing you how to create an array of value types and how to use the array.


1.        Open Microsoft Visual C++/Studio .NET, and create a new CLR Console Application project named Trad.





Creating a new C++ .NET CLR Console Application project named Trad


2.        Open the source file Trad.cpp and add the following code to the main() function:


int main(array<System::String ^> ^args)


      Console::WriteLine(L"Traditional C++ Arrays");

      // Create an array

      int arr[10];


      // Fill the array

      for(int i=0; i<10; i++)

         arr[i] = i*2;


    return 0;



Declare and use an array in main program file





The array is created by giving a type, a name, and a size enclosed in square brackets ([]). Here the array is named arr and it holds ten int values. All arrays are created using the same syntax, as shown here:

// Create an array of six doubles

double arr[6];

// Create an array of two char*’s

char* arr[2];

Here’s the first important point about native arrays: once you’ve created an array, you can’t resize it, so you have to know how many elements you’re going to need before you start. If you don’t know how many elements you’re going to need, you might be better off using a .NET array, discussed later in this module.

The array size has to be known at compile time, so, for example, you can’t ask the user for a value and then use that value to specify an array dimension at run time. However, it’s common to create constants, either by using preprocessor #define declarations or by declaring const int variables, and using them to specify array dimensions. For example:

#define size 100

As you can see from the loop in the preceding code, array elements are accessed using square brackets that contain the index. Here’s the second important point about native arrays: indexing starts from zero rather than one, so the valid range of indices for an array is from zero to one less than the size of the array. In other words, for a 10-element array, valid indices are [0] to [9].


3.        Add a second loop to print out the array’s contents after filling it.


// Print its contents

for(int j=0; j<10; j++)


Using loop to print array's content



4.        Build and run your program. You should find that the values print, one to a line, as shown in the following figure.


An array console program output sample


What happens if you change the range of the second loop so that it tries to print the element at [10]?


5.        Alter the code in the second loop to look like this:

// Print its contents

for(int j=0; j<=10; j++)



Editing a loop to print array elements


Notice the less than or equal to (<=) condition. The effect of this condition is to try to print 11 elements rather than 10.


6.        Compile and run the program, and you should see output similar to the following figure.


An array console program output sample


Notice the random value that’s been printed at the end of the list. Here’s the third important point about native arrays: bounds aren’t checked. Native arrays in C++ aren’t objects and therefore they have no knowledge of how many elements they contain. It’s up to you to keep within the bounds of the array, and if you don’t, you risk corrupting data, generating buffer overflow and crashing your program. As a security and normal practice, programmers will provide codes that check the limit.


7.        To make the previous code ‘cleaner’ and meaningful, the following is the modified version.

// Print its contents

for(int j=0; j<=10; j++)

   Console::WriteLine("index, i = {0}, content = {1}", j, arr[j]);


Editing the codes so that the output will print the array index and its content


The output is shown below.


Array index and its content seen through Console program output



Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7 | Part 8




< C++ .NET Exception Handling 7 | Main | C++ .NET Arrays & Collections 2 >