0

I would like to ask you about this code

#include <stdio.h>
#include "bit-array.h"

#ifndef BitArray
#define BitArray(array_name, size)\
    int size_in_bits = size/sizeof(long);/*thanks to this, it should work on x64*/\
    size_in_bits /= 8;/*conversion from bytes to bits*/\
    int array_name[size_in_bits];/*declaration of an array*/\
    for(int i = 0; i < size_in_bits; i++)/*filling the array by zeros*/\
        array_name[i] = 0;\

#endif

int main()
{
    BitArray(test, 100);
    for(int i = 0; i < 100; i++)
        printf("%d\n", test[i]);
    return 0;
}

As far as i know, this macro BitArray(array_name, size) (placed in separate header file bit-array.h) should define and fill an array with zeros, then it should print the zeros into terminal. However, it prints some random numbers from memory. I am kinda stuck here. Can you help please?

EDIT: Thanks for all your answers. I must apologize that I forgot to write here one important thing. And that is fact, that in this task this has to be macro with this prototype: BitArray(array_name, size).

3
  • 1
    size_in_bits is not 100. and use memset to clear memory Commented Mar 11, 2014 at 23:55
  • @KerrekSB: You seem to have forgotten about variable-length arrays, VLAs, standard in C99 and optional in C11. Commented Mar 12, 2014 at 3:23
  • @JonathanLeffler: That's true, but the construction the OP posted is needlessly using a dynamic bound when the bound is available statically. The solution I posted can still be used to create VLAs. I was objecting to the intermediate variable mostly... Commented Mar 12, 2014 at 9:09

3 Answers 3

2

You're creating an array of (size / 8) elements, where size == 100, but then printing out 100 elements, rather than 12.

There are various other problems with this code though...

Sign up to request clarification or add additional context in comments.

Comments

1

I'd use this:

#include <limits.h>
#define bit_array(type, varname, nbits) \
    type varname[((nbits) + CHAR_BIT + sizeof(type) - 1) / (CHAR_BIT * sizeof(type))]

Usage:

bit_array(unsigned int, foo, 120);    // maybe "unsigned int foo[4];"

You best be using unsigned integral types as the base type of the array.

Comments

0

The way you wrote that macro, you'd have been better off just making it a function and marking it as inline. Luckily, you don't need to be that complex.

Kerrek's answer is really close, but there's a flaw in it, which you can see if you use unsigned char for the type, and it's incomplete as it doesn't fill the array with 0's. So the more correct and complete answer is:

    #include <limits.h>
    #define BitArray( type, varname, nbits ) \
        type varname[ ( (nbits) + ( CHAR_BIT * sizeof(type) ) - 1 ) / ( CHAR_BIT * sizeof(type) ) ] = {0};

Thus your program becomes:

    int main()
    {
        BitArray( uint32_t, test, 100 );  // same as uint32_t test[4] = {0};
        for ( int i = 0; i < 4; i++ )
            printf("%d\n", test[i]);
        return 0;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.