변수 type별 memory allocation

1087 단어 cppcpp

일반적으로
정수형

  • short : 2bytes
  • int : 4bytes
  • long : 8bytes
    실수형
  • float : 4bytes
  • double : 8bytes

로 사용한다.

c++ 스탠다드에 따르면 int는 at least 16bits로 되어있어 컴파일러나 아키텍처에 따라 2bytes일 수도 있다.

이를 해결하기 위해

  • 조건이 맞는지 확인해주는 static_assertion(sizeof(int)==4)을 사용하여 확인해준다.
  • c++ 스탠다드의 Fixed width integer types를 이용한다
#include <cstdint>
~~~
cout << sizeof(int8_t) << "\n";
cout << sizeof(int64_t) << "\n";


1bytes
8bytes를 리턴한다

c++에서 array를 사용할때는 array std를 사용하는 것이 좋은데
array<uint64_t,5> arr;
같은 식으로 변수 type과 arr의 크기를 지정할 수 있기 때문이다.

% vector std를 안쓰는경우

  • 힙 메모리 allocation deallocation에 시간이 소모되어 이를 줄여야 할 경우
  • 메모리 단편화(fragment)가 없어야할 경우

좋은 웹페이지 즐겨찾기