# sizeof?

Hi,

I’m currently creating my own library for a geiger counter
My goal is to define deadTime,Conversion factors and time intervals and pass these to the library.

For this i’m using the following class declaration

``````uint32_t intervals[] = {10000,30000,60000,120000,300000};
``````

So the user has the freedom to use as many intervals as he likes (and his Arduino has space)

Inside the library i’m want to find out how many intervals the user has submitted

``````RadHand::RadHand(uint16_t deadTimeInput, uint16_t conversionFactor, uint32_t interval[]){
arrSize = sizeof(interval);
//other coed
}
``````

But i’m always landing here with 0 as arrSize.

Why?

'interval' is (essentially) a pointer. So, 'sizeof(interval)' should give you the size of a pointer -- NOT the size of the array. But, don't know why it's giving you 0. Maybe show more code?

I have uploaded to github for better visibility

For testing i put a Serial.print(arrLen) into Radhand::service();

Posted here, for even better visibility

``````RadHand::RadHand(uint16_t deadTimeInput, uint16_t conversionFactor, uint32_t intervals[]){

convFactor = conversionFactor;
arrLen = sizeof(intervals)/sizeof(uint32_t);

for(uint8_t i=0; i<arrLen; i++ ){
timeInterval[i] = intervals[i];
lastConversion[i] = 0;
currentCPM[i] = 0;
progress[i] = 0;
updated[i] = false;
pulseCount[i] = 0;

}

totalPulseCount = 0;
``````

arrLen = sizeof(intervals)/sizeof(uint32_t);

2/4 == 0

That is the reason why you need to pass the size of an array, or the number of elements into it, when passing an array to a function.

if you want the constructor to determine the size of the arrays, you can use a class template:

``````template<uint8_t N>
class MyClass {
private:
int* myArray;
size_t arrayLength;
public:
MyClass() {
arrayLength = N;
myArray = new int[arrayLength];
}
size_t getArrayLength (void) {
return arrayLength;
}
};

MyClass<10> myInstance;

void setup() {
Serial.begin(9600);
Serial.println(myInstance.getArrayLength());
}

void loop() {
// put your main code here, to run repeatedly:

}
``````

TolpuddleSartre:
Posted here, for even better visibility

``````RadHand::RadHand(uint16_t deadTimeInput, uint16_t conversionFactor, uint32_t intervals[]){
``````
``````deadTime = deadTimeInput;
convFactor = conversionFactor;
arrLen = sizeof(intervals)/sizeof(uint32_t);

for(uint8_t i=0; i<arrLen; i++ ){
timeInterval[i] = intervals[i];
lastConversion[i] = 0;
currentCPM[i] = 0;
progress[i] = 0;
updated[i] = false;
pulseCount[i] = 0;

}

totalPulseCount = 0;
``````
``````

arrLen = sizeof(intervals)/sizeof(uint32_t);

2/4 == 0
``````

Yes, but its not allowed to pass values under uint32_t

in my case:

uint32_t intervals = {10000,30000,60000,120000,300000};

so sizeof would yield in 20 (i tested that), so i have to divide.
If i would pass only a uint8_t then there would be an error

I’ve modified the constructor to pass lenght:

``````RadHand::RadHand(uint16_t deadTimeInput, uint16_t conversionFactor, uint32_t intervals[],uint8_t arrayCount){

convFactor = conversionFactor;
arrLen = arrayCount;

for(uint8_t i=0; i<arrLen; i++ ){
timeInterval[i] = intervals[i];
lastConversion[i] = 0;
currentCPM[i] = 0;
progress[i] = 0;
updated[i] = false;
pulseCount[i] = 0;

}

totalPulseCount = 0;

}
``````

Still no luck!

I’ve now even have hard-coded the lenght inside the constructor. There must be something wrong, because it still is 0.

I’ve updated the git if someone wants to have a look

You misunderstand how arrays are passed to functions in C.
They are passed as a pointer to the first element of he array.
The size of a pointer is fixed - on eight bit AVRs, it is two bytes.

BulldogLowell:
if you want the constructor to determine the size of the arrays, you can use a class template:

Its not a must, i just want that my library knows how many values there are in a array. If i pass the lenght, thats also fine. But currently that doesnt work too..

If i pass the lenght, thats also fine. But currently that doesnt work too..

Then you're doing it wrong.

sgt_bear:
Its not a must, i just want that my library knows how many values there are in a array. If i pass the lenght, thats also fine. But currently that doesnt work too…

you cannot create an array of length zero and simply add elements as you are trying to do:

``````private:
uint16_t convFactor;
uint32_t pulseCount[];
uint32_t timeInterval[];
uint32_t lastConversion[];
``````

when you try to do this:

``````lastConversion[i] = 0;
``````

all you are doing is stomping on memory you don’t own!

use the template method I showed you to pass the array length… but you need to have a variable that holds that number.

``````template<uint8_t N>
class MyClass {
private:
uint32_t* myArray;
size_t arrayLength;
public:
MyClass() {
arrayLength = N;
myArray = new uint32_t[arrayLength];
}
size_t getArrayLength (void) {
return arrayLength;
}
};

MyClass<10> myInstance;

void setup() {
Serial.begin(9600);
Serial.println(myInstance.getArrayLength());
}

void loop() {
// put your main code here, to run repeatedly:

}
``````