6 Replies - 17051 Views - Last Post: 26 August 2008 - 09:34 AM Rate Topic: -----

#1 devesa  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 16-November 07

Number of elements in an array

Posted 26 August 2008 - 01:08 AM

I have a pointer
int *numFiles
which correspond to a integer array, which can have 0 (not initialized, NULL), 1, 2, ... n elements.

How can I know the number of elements of this array?

Thanks,
Is This A Good Question/Topic? 0
  • +

Replies To: Number of elements in an array

#2 sensui  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 30
  • View blog
  • Posts: 146
  • Joined: 24-August 08

Re: Number of elements in an array

Posted 26 August 2008 - 01:44 AM

You can use the sizeof operator.
sizeof( numFiles ) / sizeof( int );

But this only works if you have declared numFiles like this:
const int SIZE = 10;
int numFiles[SIZE];
cout << sizeof( numFiles ) / sizeof( int ) << endl;

it should output:
10

Was This Post Helpful? 0
  • +
  • -

#3 devesa  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 16-November 07

Re: Number of elements in an array

Posted 26 August 2008 - 01:52 AM

Thanks a lot, sensui, but what if I have declared numFiles like this:
int *numFiles;

because we don't know yet the size, and we might have to make reallocs?

Remember that the number of elements can be also 0 (numFiles=NULL).

Thanks again,
Was This Post Helpful? 0
  • +
  • -

#4 sensui  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 30
  • View blog
  • Posts: 146
  • Joined: 24-August 08

Re: Number of elements in an array

Posted 26 August 2008 - 01:57 AM

You can also have:
int * numFiles = { 1, 2, 3};
  cout << sizeof( numFiles ) / sizeof( int ) << endl;

But this only works when you declare the pointer, but if you have reallocs I don't know if it is possible.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5929
  • View blog
  • Posts: 12,851
  • Joined: 16-October 07

Re: Number of elements in an array

Posted 26 August 2008 - 04:29 AM

Simply put, you can't know the number of elements in a dynamically allocated array from just the pointer.

The reason is, the pointer is pointing to the first element. Or, rather, the top of the allocated block. It doesn't know how big that block is, it's the job of the programmer to keep track of it. Look around and you'll see a lot of C based array examples that pass the size; there's a reason.

Here's some test code:
int main() {
   int size, realSize;
   int *list;
   
   size = 10;
   realSize = size * sizeof(int);
   list = (int *)malloc(realSize);
   printf("size=%d\n", size);
   printf("realSize=%d\n", realSize);
   printf("sizeof(int)=%d\n", sizeof(int));
   printf("sizeof(list)=%d\n", sizeof(list));
   free(list);
   return 0;
}



Results:
size=10
realSize=40
sizeof(int)=4
sizeof(list)=4



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#6 sensui  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 30
  • View blog
  • Posts: 146
  • Joined: 24-August 08

Re: Number of elements in an array

Posted 26 August 2008 - 05:09 AM

Why aren't you using the STL solution for arrays ( vector )?
Example:
#include <iostream>
#include <vector>
using namespace std;

int main() {
  vector<int> myArray;
  for( int i = 0; i < 5; i++ )
    myArray.push_back( i );

  cout << "The array size is: " << myArray.size() << endl;
  return EXIT_SUCCESS;
}


This will output:
The array size is: 5

Use the size() method to get the size of the array.
The advantage of using the class vector is that you will not deal anymore with memory allocation for every element you add in an array like in the case of pointers. You can use the method push_back() which will deal automatically with memory.

This post has been edited by sensui: 26 August 2008 - 05:18 AM

Was This Post Helpful? 0
  • +
  • -

#7 devesa  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 16-November 07

Re: Number of elements in an array

Posted 26 August 2008 - 09:34 AM

Thanks you two, that helped me a lot.

Kind regards,

devesa
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1