Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a guarantee as to the size of a class that contains an array?

Given:

template <int N>
struct val2size
{
    char placeholder[N];
};

Is there any guarantee that sizeof(val2size<N>) == N?

like image 897
uj2 Avatar asked Dec 29 '22 07:12

uj2


1 Answers

The only guarantee is that

sizeof(val2size<N>) >= N

There may be unnamed padding at the end of the struct. I don't think it's likely that there will be unnamed padding, but it's possible.

like image 187
James McNellis Avatar answered Jan 24 '23 19:01

James McNellis