Leanne
Leanne

Reputation: 687

getting length of the array in c++

I am creating int array using c++ and trying to get the length of it

int *masterArray;
int count = 0;
int a = 0;
int var = 0;
ifstream myfile("sample_10.txt");
if (myfile.is_open())
{
    while(myfile.good())
    {

            string word;

        while(getline(myfile, word))
        {
            count++;
        }

        cout << "count: " << count << endl;
        masterArray = new int [count];

        myfile.clear();
        myfile.seekg(0);
        while(getline(myfile, word, '\n'))
        {
            cout << word  << " ";
            istringstream ( word ) >> var;
            masterArray[a] = var;

            a ++;
        }
    }
}

name of the int array is master array, and after I add variables in the array I do..

cout << "sizeof(masterArray) : " <<sizeof(masterArray);

which gives me 8, instead of 10.

I tried to print out all variables stored in the array and it gives me 10, which means all variables are stored correctly.

should I retrieve the length by doing

cout << "sizeof(masterArray) : " <<sizeof(masterArray) / sizeof(*masterArray);

??

Because that gives me 2 (obviously, cuz it is dividing 8 by 4)

Thanks

Upvotes: 1

Views: 420

Answers (8)

TemplateRex
TemplateRex

Reputation: 70516

Here's your example rewritten to using std::vector

std::vector<int> masterArray;
int var = 0;

ifstream myfile("sample_10.txt");
if (myfile.is_open())
{
    while(myfile.good())
    {

            string word;

        while(getline(myfile, word))
        {
            count++;
        }

        cout << "count: " << count << endl;
        masterArray.reserve(count); // will allocate at least count ints

        myfile.clear();
        myfile.seekg(0);
        while(getline(myfile, word, '\n'))
        {
            cout << word  << " ";
            istringstream ( word ) >> var;
            masterArray.push_back(var); // store the current word
        }
    }
}

Upvotes: 0

juanchopanza
juanchopanza

Reputation: 227370

This

sizeof(masterArray);

gives you the size of an int*, which is 8 on your platform (or 64 bits, assuming an 8 bit char).

Looking at your code, it seems to me that you could use std::vector instead of the array, and add elements using the std::vector::push_back method. If you actually needed the length, you could get it from the size() method, but what you normally do with a vector is to iterate over its contents using it's begin and end iterators (see methods begin() and end() respectively).

Upvotes: 1

anxieux
anxieux

Reputation: 757

I would suggest to use std::vector in your case. Note, that in C++ it is a common practice to use vectors for any array-like objects. You should have very strong arguments if you want to manage dynamically allocated arrays by yourself.

Upvotes: 2

bar
bar

Reputation: 176

Im guessing you are using 64 bit computer? sizeof returns the size of the variable it given in this case a pointer in other words a memory address which in 64 bit computer is equal to 8 bytes. In order to find the length of the array in c you need to use another variable with the size of the array stored in it.

Upvotes: 1

jrok
jrok

Reputation: 55395

You already got the length - it's count. It's the only way of knowing a length of dynamically allocated array, by manually keeping track of it. As others have pointed out, you only get a pointer to the first element if you allocate an array via new. sizeof(masterArray) will return the size of this pointer and it happens to be 8 bytes on your platform.

Upvotes: 1

Lyubomir Vasilev
Lyubomir Vasilev

Reputation: 3030

Your masterArray variable is of pointer type. I suppose you are on a 64bit machine, so the pointers are 8 bytes. That's why it gives you 8 when you do a sizeof().

There is no standard way of getting the size of an array, at least not that I know of. You have a count that you get from the user and allocate the array with. I guess it would be best to keep that and use it.

Upvotes: 2

Corbin
Corbin

Reputation: 33437

sizeof() only works on arrays. You're getting 8 because a pointer is 8 bytes (on a 64 bit system). You cannot determine the length of a dynamically allocated array. This means that you need to keep track of the length, rather than re-determining it.

By the way, this looks like a very good situation to use a vector.

Upvotes: 0

pb2q
pb2q

Reputation: 59607

Using sizeof to get the number of elements in an array will only work for variables that are declared as arrays. E.g. int masterArray[10];.

Your array is declared as an int * - later allocated dynamically - so you're getting the size of that type.

Upvotes: 0

Related Questions