3

When compiling below code using GCC 4.8.3, I am getting an error saying "arrTest.c:12:4: error: variable-sized object may not be initialized". But the same code compiles fine when compiled with GCC 4.1. Can anyone help me to fix this?

#define OFFSET(structure, member)    /* byte offset of member in structure*/\
                ((const int) &(((structure *) 0) -> member))

typedef struct test{
   int a;
   char b;
   int c;
}test;

void main()
{
   int arr[OFFSET(test, b)] = {0};

   printf("%d %d\n", arr[0], OFFSET(test, b));

   return;
}

Though, removing the array initialization would fix the problem. But, there are lot of instances like this in my code. So i don't want to go every where and modify. I wanted to know if there exists any way in GCC 4.8 to fix this either through some compilation flags or modifying the definition of the MACRO, as the same code gets compiled cleanly with GCC 4.1.

3
  • change int arr[OFFSET(test, b)] = {0}; to int arr[OFFSET(test, b)]; Commented May 5, 2015 at 10:36
  • Thanks Grijesh, Actually there are lot of instances like this in my code. So i don't want to go every where and modify as you suggested. I wanted to know if there exists any way in GCC 4.8 to fix this either through some compilation flags or modifying the definition of the MACRO, as the same code gets compiled cleanly with GCC 4.1. Commented May 5, 2015 at 10:50
  • I believe there should be something. You should also add this information in comment to question at bottom. Commented May 5, 2015 at 10:54

2 Answers 2

3

Your OFFSET macro is incorrect in modern C as dereferencing a null pointer is undefined behavior; furthermore, there's no provision in modern C for arbitrary pointer arithmetic to result in a compile time constant, which is why gcc complains that the array is variable-sized.

Fortunately, modern C supplies an offsetof macro in stddef.h, which has fully defined behavior and results in a compile-time constant:

#include <stddef.h>
#define OFFSET(structure,member) offsetof(structure, member)

// rest of code

Live example.

I don't have access to as old a compiler as gcc 4.1, but if it happens not to support offsetof then you can fall back to your definition via a preprocessor conditional version check:

#ifdef __GNUC__ && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC__MINOR__ >= 8))
#    include <stddef.h>
#    define OFFSET(structure,member) offsetof(structure, member)
#else
#    define OFFSET(structure, member)    /* byte offset of member in structure*/\
            ((const int) &(((structure *) 0) -> member))
#endif
Sign up to request clarification or add additional context in comments.

1 Comment

I would assume that OFFSET is (despite appearance) not a compile time constant so that arr would be a variable length array in the OP'S code. offsetof, by contrast, is a compile time integer constant, leading to a "normal" array definition.
2

Try C library offsetof() macro. In GCC it uses __builtin_offset() function and seems to work OK.

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.