3

I've built a class called IntSet. My problem is that i dont want to storage another element that is the maximum amount of elements i want to introduce in elem array. So, in add method or any other method, i want to find out the max size i've allocated in the IntSet (int dim_max) constructor using this operation :

int n = sizeof(elem) / sizeof(*elem); //or sizeof(elem) / sizeof(int);
cout << "N is = " << n;

However, this doesnt work, everytime n is 1, even if i allocate elem = new int[dim_max];, where dim_max is a variable i read from the keyboard and it's much bigger than 1. Here is the code :

 #include <iostream>
using namespace std;

class IntSet{
    int *elem;
    int dim;

    public:

    IntSet (int dim_max) {
        dim = -1;
        elem = new int[dim_max];
    }

     void add(int new_el) {
        int n = sizeof(elem) / sizeof(int);
        cout << "N is =" << n;
        for(int i = 0; i < n; i++) {
            if(elem[i] == new_el) {
                cout << "It's already there !";
                return;
            }
        }
        dim++;
        if(dim == n) {
            elem = (int*)realloc(elem, sizeof(int) * (n + 1));
            global_var++;
        }
        elem[dim] = new_el;
     }
};
ballow
  • 81
  • 1
  • 8
  • `elem` is a pointer to type int and `*elem` is an int type. It's always giving `1` because `sizeof(elem) == sizeof(int)` on your system. I'm assuming `dim` keeps track of the number of elements allocated to `elem` but you forgot to reassign `dim = dim_max` in the constructor and hence you don't have to calculate `n` since you already have the number of elements stored in `dim`. – srt1104 Oct 22 '19 at 06:17
  • Sizeof is evaluated at compile time so doesn't know about dynamic allocations – Alan Birtles Oct 22 '19 at 06:18
  • The size of a pointer is the size of the pointer itself, not what it might point to. If you want "dynamic arrays" use [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) instead. Or if this is an assignment or exercise where you must use pointers and `new[]`, then keep track of the number of elements yourself. – Some programmer dude Oct 22 '19 at 06:18
  • Possible duplicate of [How to find the 'sizeof' (a pointer pointing to an array)?](https://stackoverflow.com/questions/492384/how-to-find-the-sizeof-a-pointer-pointing-to-an-array) – Alan Birtles Oct 22 '19 at 06:18
  • I understand now what i've done wrong. Thanks a lot. So i have to use another value to storage the max length of this array. And yes, I forgot to tell you, `dim` keeps track of how many elements i've got in that array after every 'add' method. – ballow Oct 22 '19 at 06:22

4 Answers4

4

The sizeof operator works on the types at compile time. Since elem is an int*, sizeof(elem) is the same as sizeof(int*). And *elem is an int, so sizeof(*elem) is sizeof(int).

So in the end, your formula is equivalent to sizeof(int*)/sizeof(int), regardless of what you put in elem. There is no standard way to find out the number of elements of the array that that was allocated for a pointer.

For your purpose, you either must either keep track of dim_max in your class, or, better, replace the use of pointers and arrays with the nicer vector.

Vectors offer a size() function, and allow to easily add new element dynamically at the end using push_back(). Maybe it could interest you as well: there is also a set container in the standard library.

Christophe
  • 68,716
  • 7
  • 72
  • 138
2

elem is a pointer and sizeof(ptr) is always fixed. On 32-bit machine sizeof pointer is 32 bits ( 4 bytes), while on 64 bit machine it's 8 byte. Regardless of what data type they are pointing to, they have fixed size.

So, the computation you are doing sizeof(elem)/sizeof(*elem) will always yield 1 as it's matching with sizeof(int). This would work if elem is an array with predetermined size. To keep track of current size, you need to have another variable.

Another thing is don't mix new and realloc. Always use new as you are using C++.

Wander3r
  • 1,801
  • 17
  • 27
2

It is because, elem is a pointer. So, every time you are doing sizeof(elem) or sizeof(*elem); will give you the size of data type & pointer respectively.

If you havent used dynamic allocation, then answer would be OK.

I would suggest you to use STL container or store max size as a data member.

0

First thing first you divide two non double value so the result is not what you expect 12/8 is 1.

sizeof a pointer depending on the architecture is 8 or 4.

What you think is sizeof returning the size of array which is only the case for automatic arrays not dynamic ones.

Oblivion
  • 7,176
  • 2
  • 14
  • 33