[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
Paul Allen » CppCin » BeginnersGuideToCPlusPlus » CppDouble » ComputerScience » talk:OrphanPages » WhatLinksHere » node » WindowsFAQ » handle » CppArray

(C++) Array

A way to store a a large number of values and access them by index. There are two kinds of arrays:
  • Static arrays (size known at compile-time)
  • Dynamically allocated arrays (size known at run-time)
Take care not to write beyond the bounds of an array [6].

Static arrays

//Create an array
int array[10];
//Initialize it with non-random values
for (int i=0; i!=10; ++i) array[i]=i;
//Square them
for (int i=0; i!=10; ++i) array[i]*=array[i];
//std::cout them
for (int i=0; i!=10; ++i) std::cout << array[i] << std::endl;


Prefer a std::vector over an array by default [1,2,3,5].

Static arrays cannot have a size of zero elements. This is used for compile-time assertions.

Tip

The Boost C++ Library supplies a class called boost::array, which might be chosen over a std::vector when there is no need of resizing at run-time.

How to determine a static array's size?

This can be done from the array's pointer and the size of the zero-th element:
  1. include <iostream>
  2. include <cassert>
int main() { const int staticSize = 100; int myArray[staticSize]; const int size = (sizeof(myArray)/sizeof(myArray[0])); std::cout << "staticSize: " << staticSize << std::endl; std::cout << "size: " << size << std::endl; assert(size==staticSize); }


Dynamically allocated arrays

These array's sizes are unknown at compile-time and use new and delete[].
  1. include <iostream>
  2. include <cassert>
int main() { const int randomSize = std::rand()%100; int * myArray = new int[randomSize]; delete[] myArray; }


Prefer a std::vector to dynamically allocated arrays [1,2,3,4,5].

How to determine a dynamically allocated array's size?

This can NOT[/red] be done from the array's pointer and the size of the zero-th element:

  1. include <iostream>
  2. include <cassert>
int main() { const int randomSize = 2 + std::rand()%100; //Can never be 1 int * myArray = new int[randomSize]; const int size = (sizeof(myArray)/sizeof(myArray[0])); std::cout << "randomSize: " << randomSize << std::endl; std::cout << "size: " << size << std::endl; assert(size==1); assert(size!=randomSize); delete[] myArray; }


'Array' links

Code links

References



last edited (November 7, 2007) by bilderbikkel, Number of views: 24654, Current Rev: 22 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Site Management by Lars Hagelin at Kontantkort.se.