Array size and calculations
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Array size and calculations

1. Junior Member
Join Date
Mar 2017
Posts
4

## Array size and calculations

Hello everyone. This is my first post here..

I am a beginner in C++ and have a question..

Is a 100x100x100 array much much more complicated and calculation intensive than a say, 5x5x5 array, even though we will only have only few, same number of used elements in each ? For example assume each array will have only 2-3 used elements at any given time, even the 100x100 array.

How about if most array elements were used ?

2. ## Re: Array size and calculations

The amount of calculation required to access an element depends upon the number of dimensions of the array - not the number of elements in each dimension. The amount of memory required to hold the array is a function of both the number of dimensions and the number of elements in the dimensions. Assuming an array of int with an int using 4 bytes, a 100x100x100 matrix would require about 4Mb of memory whereas a 5x5x5 would require about 500 bytes.

The size of the array and the calculation required to access an element also does not depend upon how many array elements are actually used - there is no distinction between a used and unused element.

The size of an array is fixed at compile time. If the size is not known until run-time then other containers such as a vector may be more appropriate.

3. Senior Member
Join Date
Oct 2008
Posts
1,456

## Re: Array size and calculations

Originally Posted by 2kaud
The size of the array and the calculation required to access an element also does not depend upon how many array elements are actually used - there is no distinction between a used and unused element.
this is not quite true ( assuming I understood what we mean by 'used' ), memory locality effects can cause slowdowns of orders of magnitudes ... so, generally speaking, keeping the data a core is working with as small and compact as possible ( to minimize cache hits, etc.. ) and as distant as posssible from other cores 'hot' data ( to avoid false sharing, etc... ) is always a good idea ...

moreover, the compiler may take a decision on the specific addressing instruction used based on the array size as well, so, strictly speaking, they're not equivalent anyway
Last edited by superbonzo; March 7th, 2017 at 09:58 AM.

4. Member
Join Date
Feb 2017
Posts
240

## Re: Array size and calculations

Originally Posted by Ketanco
assume each array will have only 2-3 used elements at any given time
To use just a few of the elements in a big 3-dimensional array may not be the best option.

It may pay off to just pretend there is an array and instead associate the 2-3 elements that are currently in use with the [i,j,k] coordinates each would have if they were indeed stored in a 3d-array. The [i,j,k] coordinates can instead be viewed as a position in 3d space.
Last edited by wolle; March 8th, 2017 at 09:51 AM.

5. ## Re: Array size and calculations

Originally Posted by superbonzo
this is not quite true ( assuming I understood what we mean by 'used' ), memory locality effects can cause slowdowns of orders of magnitudes ... so, generally speaking, keeping the data a core is working with as small and compact as possible ( to minimize cache hits, etc.. ) and as distant as posssible from other cores 'hot' data ( to avoid false sharing, etc... ) is always a good idea ...

moreover, the compiler may take a decision on the specific addressing instruction used based on the array size as well, so, strictly speaking, they're not equivalent anyway
Yeah, I was being too simplistic!

6. ## Re: Array size and calculations

Originally Posted by wolle
To use just a few of the elements in a big 3-dimensional array may not be the best option.

It may pay off to just pretend there is an array and instead associate the 2-3 elements that are currently in use with the [i,j,k] coordinates each would have if they were indeed stored in a 3d-array.
For sparse arrays, there's other underlying data structures that could be considered rather than an array. Have a look at http://forums.codeguru.com/showthrea...nsional-arrays and post #7 onwards. This was for a many-dimensional array (with the example at 5) but is easy to extend to smaller/larger dimensions. This particular example obviously isn't as fast for access as using an array but depending upon the requirements something similar may be worth considering.

7. Junior Member
Join Date
Mar 2017
Posts
4

## Re: Array size and calculations

i asked this questions for making something in 3d space, to track location of moving points in 3d space, in relation to each other. i want to measure their distance and angle with respect to each other. that is why i said only few location even in 100x100 array will be used. as i said i am beginner in C++. may be there will be much better ways?

8. Member
Join Date
Feb 2017
Posts
240

## Re: Array size and calculations

Originally Posted by Ketanco
i asked this questions for making something in 3d space, to track location of moving points in 3d space
I suspected something like that and that's why I replied the way I did in #4.

In C++ it's common to group data that belong together in units called classes or structs (which have minor differences). This is a very simple example:

Code:
```struct Position {  // a position
int i,j,k; //3 integer coordinates
};

struct Element { // an element
int id; // id number to tell different elements apart
Position pos; // the position of an element in space
};

Element elem; // declares storage for one element called elem

// declares an array called manyelem to store many elements
std::vector<Element> manyelem; // (vector is a commonly used standard array that can grow/shrink as needed)```
Last edited by wolle; March 10th, 2017 at 12:11 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•