Reputation: 81
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;
}
};
Upvotes: 3
Views: 434
Reputation: 73376
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.
Upvotes: 4
Reputation: 7374
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.
Upvotes: 0
Reputation: 99
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.
Upvotes: 2
Reputation: 1881
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++.
Upvotes: 2