1

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

Leanne
  • 667
  • 3
  • 11
  • 23
  • 1
    is this a duplication of http://stackoverflow.com/questions/492384/how-to-find-the-sizeofa-pointer-pointing-to-an-array ? – lavin Aug 07 '12 at 05:41
  • Updated: I just tested this with 50 inputs(in the file), and cout << "sizeof(masterArray) : " < – Leanne Aug 07 '12 at 05:41
  • I'm not sure I understand. You already have the length of the array, in `count`. That's what you calculated the length as and that's what you initialised the pointer with. So that's the length. Why then also try to find other ways to calculate it? – Mr Lister Aug 07 '12 at 05:44
  • 3
    Waayy too many conditionals and loops... – Kerrek SB Aug 07 '12 at 05:45
  • By the way, why do you have `getline(myfile, word)` in the first loop and `getline(myfile, word, '\n')` in the second? – Mr Lister Aug 07 '12 at 05:47

8 Answers8

2

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.

Lyubomir Vasilev
  • 3,000
  • 17
  • 24
2

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.

anxieux
  • 757
  • 5
  • 14
  • This would be better off as a comment rather than an answer. The question is about learning to understanding pointers and memory, and suggesting to use things that prevent you from learning these things is not an answer to the question. – Mr Lister Aug 07 '12 at 07:27
  • @MrLister: I see nothing in the question about wanting to learn about low-level memory management; it's just asking how to get the size of a dynamic array, and this answer gives the best way to do that. – Mike Seymour Aug 07 '12 at 09:30
1

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).

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

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.

bar
  • 176
  • 2
1

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.

jrok
  • 54,456
  • 9
  • 109
  • 141
0

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.

pb2q
  • 58,613
  • 19
  • 146
  • 147
0

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.

Corbin
  • 33,060
  • 6
  • 68
  • 78
  • then how should i get the size of masterArray other than using count variable? – Leanne Aug 07 '12 at 05:46
  • @Leanne You can't. (well, you probably could by using some non portable compiler specific hacks, but that doesn't mean you *should*) – jrok Aug 07 '12 at 05:49
  • @Leanne The short answer is "Welcome to C++!". But as for the actual answer, jrok nailed it. Don't try to re-determine the length. Track it instead. – Corbin Aug 07 '12 at 06:00
0

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
        }
    }
}
TemplateRex
  • 69,038
  • 19
  • 164
  • 304