0

Please, help me to create a nested struct with an array. How do I fix this code?

class CMain
{
    public:
        CMain();
        ~CMain();

    private:
        struct
        {
            CCheckSum() : BufferSize(500) {memset(Buffer, 0, BufferSize);}
            const int BufferSize;
            char Buffer[BufferSize];
        }SmallBuffer;
}

Thanks.

4 Answers 4

2

Even though you declared BufferSize as const, at class scope this does not declare a compile-time constant, which is needed for declaring array sizes in C++, but a constant data member, which, once set in the constructor, cannot be changed anymore for the lifetime of the instance (but can still be different for each instance).

Two solutions: Make

  1. BufferSize static const at class scope, or
  2. (just) const at namespace scope (including global scope).
Sign up to request clarification or add additional context in comments.

Comments

1

Static arrays need to know their length at compile time, or you need to dynamically allocate memory:

struct CCheckSum
{
    CCheckSum()
    : BufferSize(500),
      Buffer(new char[BufferSize])
    {
        memset(Buffer, 0, BufferSize);
    }
    ~CCheckSum() { delete[] Buffer; } // Note the use of delete[]!
    const int BufferSize;
    char* Buffer;
}SmallBuffer;

You're probably better off using std::vector though:

struct CCheckSum
{
    CCheckSum() : Buffer(500, 0) {}
    std::vector<char> Buffer;  // A std::vector keeps
                               // track of its size enternally
}SmallBuffer;

4 Comments

I intended to use array because I need to fill it with consecutive blocks of raw data bytes. Push_back() it will cause reallocation which I want to avoid. If I declare a vector of 500 bytes how do I copy, say , blocks of 100, 200 and 200 bytes into it one after another without putting them byte-by-byte with [] operator?
Actually, come to think of it, std::(tr1::)array, not std::vector is the correct thing to use here, since the size is (supposed to be) a compile-time constant.
Two things: 1) You wouldn't be pushing back, you've already got an array of 500 bytes. 2) Vectors are stored as contiguous memory, so you can use them like raw-pointer arrays. &Buffer[0] is a pointer to the beginning of the array which can be used in c-style memory functions.
If he has access to it, std::(tr1::)array is a good choice too. If not, vector is the way to go.
0

There is no problem related to the nesting.

Arrays have constant size. Either make BufferSize constant or use a std::vector instead of a char[].

2 Comments

But I declared BufferSize as const int. And I use raw buffer since I need to do some simple and quick work with raw data coming from network.
You need to make it static as well.
0

Array size cannot be changed at run time. It should be known at compile time itself. You can use std::vector for this use case. Also, the struct name should be given before writing the constructor with the name CCheckSum.

struct CCheckSum
        {
            CCheckSum() : ....

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.