dimanche 24 mai 2015

In c++, how do I declare and initialise an array of unsigned char dynamically properly?

the header file

/*
 * ArrayOfBit.h
 *
 *  Created on: 23 mai 2015
 *      Author: pierre-antoine
 */

#ifndef ARRAYOFBIT_H_
#define ARRAYOFBIT_H_

namespace nsBitVector
{

    typedef unsigned char byte_t;
    typedef unsigned short int UInt16;

    class ArrayOfBit
    {
    private:
        UInt16 m_Size;
        UInt16 m_RealSize;
        byte_t *array = 0; 
        // I initialised the pointer to 0 according to what I've read
        //not letting a pointer non-initialised
    public:
        ArrayOfBit (UInt16 bits);
        ~ArrayOfBit ();
        UInt16 size () const noexcept;
        UInt16 MaxSize () const noexcept;
        UInt16 operator[] (UInt16 index) noexcept;
    };
}
#endif /* ARRAYOFBIT_H_ */

the code

/*
 * ArrayOfBit.cpp
 *
 *  Created on: 23 mai 2015
 *      Author: pierre-antoine
 */

#include "ArrayOfBit.h"

//some macros to go faster.
typedef unsigned short int UInt16;
typedef unsigned char byte_t;

/**
 *
 */
nsBitVector::ArrayOfBit::ArrayOfBit (UInt16 bits) : 
m_Size (bits), m_RealSize ((UInt16) (bits + 7) / 8)
{
    //HERE IS THE LINE WHICH PROVOKES ERROR :
    byte_t *array[] = new byte_t [m_RealSize] {0};
    //m_RealSize is a unsigned short int of the size of the array needed
    //calculated. if need 12 bits, take (12 + 7 = 19 bits / 8 = 2 bytes)
}

nsBitVector::ArrayOfBit::~ArrayOfBit ()
{
    delete[] array;
}

UInt16 nsBitVector::ArrayOfBit::size() const noexcept
{
    return m_Size;
}

UInt16 nsBitVector::ArrayOfBit::MaxSize() const noexcept
{
    return m_RealSize;
}

UInt16 nsBitVector::ArrayOfBit::operator[] (UInt16 index) noexcept
{
    return (UInt16) (*array & ((0b1) << index));
}

This is the end of the code.

I get 3 errors out of this :

1)  ../src/ArrayOfBit.cpp: In constructor 
    ‘nsBitVector::ArrayOfBit::ArrayOfBit(nsBitVector::UInt16)’:
        ../src/ArrayOfBit.cpp:21:49: erreur:
        initializer fails to determine size of ‘array’

Why does this fails ? I couldn't get valid answers on other posts on how to make it work.

2)  ../src/ArrayOfBit.cpp:21:49: erreur:
        array must be initialized with a brace-enclosed initializer 

This one I don't get, as there are curly braces around a zero.

3)  ../src/ArrayOfBit.cpp:21:13: attention : 
        unused variable ‘array’ [-Wunused-variable]

This one I don't get either, because I use the array later.

Now, the goal of this class is to be wrapped to make a bit set, and not using 3 whole bytes for 3 boolean. later on, i plan to extend this to any size I wish, like a field of 3 bits variables.

This is why I don't use the containers of the STL. The problem is not about functionning, but compiling: once i can compile, i will be able to begin unit testing

To be clear, if you missed the macro lines, byte_t is a unsigned char, and UInt16 is a unsigned short int.

I thank you for reading my post.

I would like to add i'm a newborn in the programmation world.

Aucun commentaire:

Enregistrer un commentaire