1

I have a situation where I pass an array containing 2 1 byte values to a function, but somehow the function thinks the array is 4 bytes long, which messes up my bit manipulation big-time. I even tried explicitly casting each array value as a uint8,but to no avail. Any ideas about what might be happening? Using cygwin's gcc tools on Eclipse Mars.1.

typedef char uint8; //char is 1 byte in my system.

void setBitArray(uint8 bitArray[], int first, int last, uint8 type) {
    if(first >= 0 && last < sizeof(bitArray) * 8) { // If the block is in bounds
        ...
    }
}
...
int main() {
    uint8 bitArray[2] = {(uint8)0, (uint8)0};
    setBitArray(bitArray, 0,10, 1);
    return 0;
}

EDIT

One more thing. sizeof(bitArray) yields 2 bytes in main().

AaronF
  • 2,841
  • 3
  • 22
  • 32
  • 1
    You cannot use `sizeof` on an array parameter. See [this answer](https://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c/10349610#10349610). – kaylum Nov 19 '15 at 00:44
  • Not 16 bits but 32 bits – Bug Hunter 219 Nov 19 '15 at 00:48
  • I've used sizeof(array)/sizeof(element) all the time to find the number of elements. http://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c – AaronF Nov 19 '15 at 00:57
  • But not on an array **that is a parameter to a function**. That's exactly what the answer says too: "iff you are dealing with arrays not received as parameters". – kaylum Nov 19 '15 at 00:58
  • Ahh. That makes sense. Because you could only really know the size on statically declared local variables. – AaronF Nov 19 '15 at 01:08
  • If you use sizeof on a parameter with a recent gcc, you will see an warning that looks similar to this: warning: sizeof on array function parameter will return size of 'int *' instead of 'int []' [-Wsizeof-array-argument] – bruceg Nov 19 '15 at 01:26

2 Answers2

1

Passing an array to a function sends the address of the first element in the array, Not the whole array as an object. The function has no way to identify what type the passed address is so it can not give you the size.

The simple solution would be to pass the size with the array in a struct or randomly

typedef char uint8; //char is 1 byte in my system.

void setBitArray(uint8 bitArray[], int size, int first, int last, uint8 type) {
    if(first >= 0 && last < size * 8) { // If the block is in bounds
        ...
    }
}
...
int main() {
    uint8 bitArray[2] = {(uint8)0, (uint8)0};
    setBitArray(bitArray, (int)sizeof(bitArray), 0, 10, 1);

    return 0;
}  
Lukas
  • 3,423
  • 2
  • 14
  • 26
0

The arguement that is passed in the function is not the array but the pointer to the array. The size of a pointer in c is 8 bytes here. Consider the code I have added where I created a character pointer first and then allocated memory dynamically using malloc. Since bitArray2 is a char pointer its size is 8 instead of 2. In passing arguements a new char pointer is created and hence its size is 8 instead of 2

    #include <stdio.h>
    typedef char uint8; //char is 1 byte in my system.

    void setBitArray(char bitArray[], int first, int last, uint8 type) {
        // if(first >= 0 && last < sizeof(bitArray) * 8) { // If the block is in bounds
            printf("Adress location = %ld\n", &bitArray);
            printf("%ld\n", sizeof(bitArray));         
        // }
    }
    int main() {
        char bitArray[2] ;  
        char *bitArray2=(char *)malloc(sizeof(char)*2);
        printf("Address = %ld\n", &bitArray);
        printf("s = %ld\n", sizeof(bitArray));    
        printf("s = %ld\n", sizeof(bitArray2));    
        setBitArray(bitArray, 0,10, 1);
        return 0;
    }
Bug Hunter 219
  • 312
  • 1
  • 14