Why can't I access the length of my array?

Pages: 12
Feb 10, 2012 at 6:49am
I have the following function:
1
2
3
4
5
6
7
string Polynomial::toString(const int arrOfNumbers[]) const{
	
	for(int i=0; i < arrOfNumbers.length() ; i++){		
	}
	
	return myString;
}


I want to use the length of my array as part of the control for my for loop but I can't. Why is this? What can I do to make it loop as many times as there are indices in the array?

edit: After thinking about it some more I figured that since when arrays are passed into functions, the only thing actually passed is a pointer to the first element so that provides access to the array but not the entire array, right? Would I have to sort of "re-create" my array's structure inside this function?
Last edited on Feb 10, 2012 at 7:32am
Feb 10, 2012 at 7:07am
use sizeof here to determine the size of array and pass the base address of array ... basically the name of variable which always store the address of array.
so your code will become as follows:

for(int i=0;i<sizeof(arrOfNumbers);i++)
{
}
Feb 10, 2012 at 7:28am
Hmm...for some reason when I use this I get 4 instead of 10. My array has 10 elements and I can even see them inside my watch window but I do the following:

int test = sizeof(arrOfNumbers);

and test = 4. Any ideas?
Last edited on Feb 10, 2012 at 7:29am
Feb 10, 2012 at 7:31am
An array is just a pointer to a block of data, so sizeof will always return 4 (unless it is specifically a static array, which may cause sizeof to give you the size of the entire block) and there is no length function. If you pass an array to a function, you also need to pass the size of the array as a second argument.
Feb 10, 2012 at 7:33am
Thanks Telion, that's what I ended up doing. I was just curious to see if maybe there were some .length() equivalent functions out there.
Feb 10, 2012 at 10:05am
There is size() method for std::array(c++11)
Feb 10, 2012 at 12:18pm
std::vector also has size(). Why switch to arrays?
Last edited on Feb 10, 2012 at 12:18pm
Feb 10, 2012 at 1:59pm
If this is for a class and you have to use array, then by all means pass a size into the function.
Feb 10, 2012 at 7:13pm
1
2
3
int array[5];

size = sizeof(array) / sizeof(array[0]; // sizeof returns size of array in bytes / array[0] in bytes 


have to use it where you created the array. i've tried sizeof within a func where array was not declared, didn't work.
Feb 10, 2012 at 7:18pm
sizeof is a poor way to get the length of an array because it is prone to misuse. My stomache cringes a little when I see people recommending it.

A much safer alternative:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <typename T,int S>
inline int countof( const T (&v)[S] ) { return S; }

//...

int main()
{
    int array[10];
    cout << countof( array );  // prints "10"

    int* ptr = new int[10];
    cout << countof( ptr );  // compiler error

    vector<int> vec(10);
    cout << countof( vec );  // compiler error 


Note that the last 2 will compile with the sizeof() approach, but will give you bogus results.
Feb 10, 2012 at 7:54pm
cool Disch .. thanks
Feb 10, 2012 at 8:16pm
but it is not working giving me error
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std ;
template<typename T, int S>
int calcsize( const T(&v)[S] ) { return S; } 
void function ( int array[] ) ;
int main()
{
        int array[] = { 4 ,5,6,7,2,3,5,3,5,5,6,7} ; 
        function( array ) ;     
        return 0;
}

void function( int array[] )
{
        int size = calcsize( array) ;
        cout<<"\n Size = "<<size;
}



but getting error
Last edited on Feb 10, 2012 at 8:35pm
Feb 10, 2012 at 8:31pm
@bluecoder, that's the point. You are trying to get the size of an array from a pointer which is not possible so you get an error. Much better than compiling code that fail at runtime.
Feb 10, 2012 at 8:38pm
Hi @Peter87 .. soory for my lack of knowledge but i am trying to duplicate the case as Disch has told .. please advice .
Feb 10, 2012 at 9:23pm
What Disch showed was just a safer way to get the size of an array than using sizeof. There is nothing extra you can do with it.
Feb 10, 2012 at 10:25pm
+1 Peter.

When you pass an array to a function by pointer there is no way to get its size. You must pass the size as a separate parameter.

1
2
3
4
5
void function( int array[] )  // 'array' is actually a pointer
{
        int size = calcsize( array) ;  // no way to get the size from just a pointer.  Compiler error
        cout<<"\n Size = "<<size;
}


The alternative would be to pass the array by reference, but this has some ugly syntax:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void function( int (&array)[10] )
{
  int size = calcsize( array );  // now this will work, but of course it will always be 10
}

int main()
{
  // this also means you can only pass arrays of 10 ints to the function.  No more, no less
  int array10[10];
  function( array10 ); // OK

  int array11[11];
  function( array11 ); // ERROR, int[11] is not int[10]

  int* ptr = new int[10];
  function( ptr ); // ERROR, int* is not int[10]
}



So yeah -- the best solution here is what's already been said: pass the size as a paramter.

That or use a container class.
Feb 10, 2012 at 10:29pm
I recommend you look into using std::vector or (C++11) std::array.
Feb 10, 2012 at 10:32pm
Another useful approach is to write functions in terms of iterators. Then it doesn't matter if it's a C array or a C++ array or a vector, as long as begin() and end() are passed.
Feb 12, 2012 at 8:47am
HI
@ Disch and @Peter87 if we need to pass the size of the vector to the function then , i am not able to understand about , as to why
we cannot use the approch of
1
2
3
int array[]  = { 5,  6, 7, 8, 8  9 }  ;

size = sizeof(array) / sizeof(array[0] ) ;
Feb 12, 2012 at 9:07am
@bluecoder

The problem is that when an array is passed to a function it is passed as a pointer, so the compiler has no information about its size inside the function. So taking sizeof(array) inside the function only returns the size of the pointer to the array, not the amount of memory allocated.
Last edited on Feb 12, 2012 at 9:07am
Pages: 12