0

I have a global include file which contains a set of structures. Somewhere in my program, I have a class that contains a member array. The number of elements in this array is dependent on the size of a specific field in a specific struct. I want to make it so that the array size will get automatically updated if the sizeof the structure field is changed. I have been able to do this succesfully with the following expression:

bool shadowChkBox[sizeof(FSCconfigType::WriteEn)*8*MAX_FSCS];

FSCconfigType is the struct type and WriteEn is one of the fields. Now this worked but only on ubuntu. On RHEL 5, the compiler declared it as an error. What other alternatives could I have for doing this? I am working with Qt.

7
  • 2
    What's the error you're getting on RH? Commented Nov 28, 2010 at 21:48
  • 4
    I suspect it has more to do with which version of GCC you are using, rather than which version of Linux. Commented Nov 28, 2010 at 21:50
  • Can you use the type of the field directly, rather than referencing it through the field? I'm guessing no (because that's a reason why the sizeof might change), but it doesn't hurt to check. Commented Nov 28, 2010 at 21:56
  • @steve in deed that is the reason why the sizeof might change Commented Nov 28, 2010 at 22:15
  • 1
    @yan bellavance, I agree that you shouldn't require your users to upgrade gcc. I was just trying to clarify the problem. I think the code should compile, and if it's a bug in the older version of gcc, then I think you're going to be stuck with an uglier workaround. Commented Nov 28, 2010 at 22:22

3 Answers 3

2

Here is one possible answer:

#include <iostream>

struct A
{
        int a;
        int b;
        int c;
};

bool items[sizeof(reinterpret_cast<A *>(0)->b)];

int main()
{
        std::cout << sizeof(reinterpret_cast<A *>(0)->b) << ",";
        std::cout << sizeof(items) << std::endl;
        return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

thx it worked. One question though. what exactly are you doing with the (0)? Seems to be an null address.
The argument to sizeof is not evaluated except to determine its type. Casting NULL to A * is as good as a way as any to get something that has the type of pointer to A. (( A*)printf(0) would do just as well, for example.)
0

One fragile answer is to take the difference of the offsetof values for WriteEn and the next field up (or failing that, the sizeof the whole struct).

This may give a slightly larger answer than sizeof due to alignment-based padding.

A bigger problem is what happens if you rearrange your fields without fixing this - but it might be an option if you're desperate.

Comments

0
#include <iostream>

struct C
{
    int iv;
    void* pv;
    char buf[128];
};

template< typename TObj, typename TFieldType >
std::size_t field_size( TFieldType (TObj::*) )
{
    return sizeof(TFieldType);
}

int main() {

    std::cout << field_size(&C::iv) << std::endl;
    std::cout << field_size(&C::pv) << std::endl;
    std::cout << field_size(&C::buf) << std::endl;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.