2

I have an array that I would like to initialize

char arr[sizeof(int)];

Would this expression evaluate to a compile time constant or result in a function call?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Anonymous
  • 1,500
  • 3
  • 18
  • 30
  • 1
    This is valid code, but probably doesn't do what you want. Please describe your larger goal - what are you going to put into this array? – zwol Aug 08 '13 at 15:12
  • The reason I ask is I am not sure how the sizeof is implemented, perhaps there is a compile time constant that is better suited to this? – Anonymous Aug 08 '13 at 15:14
  • `sizeof(TYPE)` is a compile time constant as long as `TYPE`'s size is fully determinable at compile time (which is the case for nearly all types). But that doesn't change the fact that this probably doesn't do what you want -- not because it isn't a compile-time constant, but because it is probably the wrong size to use. – zwol Aug 08 '13 at 15:16
  • In C++ you generally don't need to use sizeof in your code, so asking what you actually want to do may be better. Also, why have you tagged performance? sizeof is resolved at compile time. – Neil Kirk Aug 08 '13 at 15:17
  • 4
    @Zack `sizeof(T)` is _always_ a constant in C++. There are no exceptions. (In C, it ceases to be a constant if `T` is a VLA.) – James Kanze Aug 08 '13 at 15:19
  • I hope its better now. – Anonymous Aug 08 '13 at 15:21
  • @JamesKanze Several compilers implement C99-like VLAs as an extension to C++, and I had thought that a subset of the feature was included in C++11. – zwol Aug 08 '13 at 15:22
  • @Anonymous You've made your question clear, but it is still the wrong question to ask. Again, please describe what you want to *do* with this array, so we can tell you whether `sizeof(int)` is how big it *should* be. – zwol Aug 08 '13 at 15:23
  • 1
    `char arr[sizeof(int)];` gives you an array of `char` that's the same size as an `int`. It could be useful if you want to manipulate the representation of an `int`, but it's probably more useful to use `unsigned char` rather than `char`. And you said you would like to initialize it; why haven't you? – Keith Thompson Aug 08 '13 at 15:33
  • +1 for clarifying the question. – Shafik Yaghmour Aug 08 '13 at 15:47
  • @Zack C++11 has no VLAs, C++1y does - and `sizeof` does not work on them. So `sizeof(T)` continues to be a constant expression in C++. – Casey Aug 08 '13 at 15:54
  • @ShafikYaghmour I'm well aware that gcc and clang have VLAs as an extension. That doesn't effect anything I said in my comment in reference to standard C++. (Ironically enough, `sizeof(x)` might not be a constant expression in an implementation with C99 VLAs, but `char arr[sizeof(x)]` is always valid as a function-local declaration regardless.) – Casey Aug 08 '13 at 16:37
  • @KeithThompson gets my upvote because I use an unsigned long int to represent a set that has operations applied to it. Putting the bits (which represent the presence/absence of set elements) into an array of chars makes it easy to code the operations. – mathematrucker May 22 '16 at 14:08

5 Answers5

5
char arr[sizeof(int)];

As far as the language is concerned, it is fine, though the array is only declared (and defined), it is NOT initialized if it is a local variable. If it is declared at namespace level, then it is statically zero-initialized.

Note that sizeof(int) is a constant expression of size_t type; its value is known at the compile time.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
1

This is an initialization:

  char arr[sizeof(int)] = { 'A', 'B', '0', 'F' };

This of course assumes that sizeof(int) is (at least) 4, or it will fail to compile.

And to answer the actual (new) question:

sizeof() is a compile time operator. In C++ [according to the standard, some compilers do allow C style variable length arrays], it will not result in anything other than a compile time constant. In C, with variable length arrays, it can become a simple calculation (number of elements * size of each element - where number of elements is the variable part).

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

There is no initialization here. There's nothing wrong with declaring or defining an array with sizeof(int) elements, except that it might look a little odd to readers of the code. But if that's what you need, that's what you should write.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
0

It really depends on how you intend to use the array.

sizeof(int) may vary with different implementations so you just need to be careful how you access the elements in the array. Don't go assuming that an element that is accessible on your machine is accessible on another, unless it's within the minimum sizes specified in the C++ standard.

Steve
  • 486
  • 2
  • 8
  • 19
0

sizeof is evaluated at compile time, the only time sizeof would would be run time evaluated would be in the case of variable length arrays in C99 code or in gcc or other c++ compilers that support VLA as an extension. So this code is valid:

char arr[sizeof(int)];

Although if it is local variable it won't be initialized.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740