Classes and structs, more space taken?

Ok so say I have std::array, and it has an array of chars that is 10 long. would array<char,10> arr take up more space than char arr[10]? And if no, then does it happen for classes with multiple member variables?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <array>

struct TwoEl{
  int a;
  int b;
}x;

int main(){
  std::array<char,10> arr1;
  char arr2[10];
  if(sizeof(arr1) > sizeof(arr2){
    std::cout << "It is!\n";
  }
  if((sizeof(int)*2) < sizeof(x)){
    std::cout << "yup.";
  }
}
1
2
3
4
5
6
7
8
#include <iostream>
#include <array>

int main() {
    std:array<char, 10> a;
    char b[10];
    std::cout << sizeof(a) << " vs " << sizeof(b) << "\n";
}
10 vs 10


Nope.

-Albatross
For both? Or just the first question?

EDIT: actually I’m being stupid, if your testing code compiles than mine should too, thank you albatross. :)
Last edited on
I'm not certain, but I think a goal of std::array<> was to be just as efficient as built-in arrays. My own test on a PC bears that out. The size of both arrays is 10 bytes. Further, if I modify the code to print the numbers out, the amount of code generated (with optimization) is the same:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <array>

struct TwoEl{
  int a;
  int b;
}x;

int main(){
#if 1
  std::array<char,10> arr;
#else
  char arr[10];
#endif
  for (int i=0; i<10; ++i) {
      std::cout << arr[i] << '\n';
  }
}

What exactly is the #if 1 testing and how does that work?
Oop, silly me, I didn't read your full question.

For a struct of two ints, I doubt the struct will be larger. However, in general, a struct can end up larger than its component types due to alignment. TL;DR for performance reasons, variables larger than one byte often need to have addresses that are a multiple of some (power of two) value. For structs, this can mean an awful lot of unused bits to make sure the alignments of their member variables stay consistent with what they'd be outside of a struct.

1
2
3
4
5
6
7
8
9
10
#include <iostream>

struct Paddington {
    char a;
    int b;
} test;

int main() {
    std::cout << sizeof(test) << " vs " << sizeof(int)+sizeof(char) << "\n";
}


Structs may also be padded at the end based on the alignment needs of its member variables. In this case, you could swap the positions of a and b in the struct, and the struct's size would still be 8.

EDIT: #if 1 is arcane heresy in the age of C++17 a preprocessor statement, much like #include. Here's some light reading on them: http://www.cplusplus.com/doc/tutorial/preprocessor/#conditional_inclusions
TL;DR the stuff between an #if and the next #elif, #else, or #endif only gets compiled if the condition of the #if is true (and 1 is true). This means dhayden has a single line that he can change one character in to switch whether the following code uses an std::array or C-style array.

-Albatross
Last edited on
Thanks,
Topic archived. No new replies allowed.