sizeof()
: 변수의 사이즈를 알아내는 방법
예제)


1 byte = 8 bit.
컴퓨터는 0과 1로 동작하는데, 그 단위가 1bit.
8bit는 이런 0과 1로 동작하는 칸이 8개 있다는 의미.
int 타입은 4byte 를 가지고 있다.
a 변수를 할당하는 순간 4byte의 공간을 차지한다는 의미.
정수 short int long = 2 4 8 byte
양수정수에는 unsigned를 붙여주면 됨.
실수 float double = 4 8 byte
cppreference에서 각 변수 타입의 크기를 체크해보면 이렇다.

https://en.cppreference.com/w/cpp/language/types
Fundamental types - cppreference.com
(See also type for type system overview and the list of type-related utilities that are provided by the C++ library) [edit] Void type void - type with an empty set of values. It is an incomplete type that cannot be completed (consequently, objects of type
en.cppreference.com
at least 16이란 것은, int형의 사이즈가 컴파일러마다 다를 수 있다는 의미.
변수형의 사이즈를 확실하게 하고 싶을 때 해결 방법
1) compilation assertion 사용하기.
: 컴파일 과정에서 어떤 조건이 만족하는지 만족하지 않는지 평가해줌.


2) Fixed Type 이용하기
cppreference에서 fixed width integer types를 검색해보면,
https://en.cppreference.com/w/cpp/types/integer
Fixed width integer types (since C++11) - cppreference.com
int8_tint16_tint32_tint64_t(optional) signed integer type with width of exactly 8, 16, 32 and 64 bits respectivelywith no padding bits and using 2's complement for negative values(provided if and only if the implementation directly supports the type) (type
en.cppreference.com



메모리 레이아웃을 정확히 생각해야 할 때는 밑처럼 사용해주면 좋다
#include <cstdint> 해주고,


Array의 경우


CPP 에서 Array를 사용할 때에는 std array를 사용해서 프로그램을 짜는게 바람직하다.
#include <array> 해주고,


Pointer의 경우
uint64_t의 포인터의 크기를 알아보자.
uint64의 포인터를 저장하는 ui64ptr을 만들고 &ui8을 넣어보자.
그리고 ui64ptr의 size와 주소를 출력해 보면,



*포인터의 크기는 실행 파일이 컴파일된 아키텍처에 따라 달라진다.
32비트 실행파일은 32비트 메모리 주소를 사용하므로 포인터의 크기는 32 bit == 4 byte이다.
64비트 실행 파일에서 포인터의 크기는 64 bit == 8 byte이다.
그림으로 이해해 보면,

프로세스
: 컴퓨터는 스택 메모리의 탑 위치에서 몇 번째에 있는지 계산해서,
포인터 정보를 보니 메모리 주소가 저장되어 있다는 것을 알고,
메모리 주소를 찾아가 보니까 실제 변수가 써 있었던 맨 아래칸 위치에 접근할 수 있었던 것.
정리
long / int / float -> 4bytes
ptr -> 4bytes (32bit환경에서) / 8bytes (64bit환경에서)
'모던C++ > 메모리 구조 Memory Structure' 카테고리의 다른 글
7. 힙 스택 스태틱 Heap Stack Static - 언제 어떻게 사용해야 할까 (0) | 2022.07.06 |
---|---|
6. 힙 메모리 Heap Memory 에 대한 모든 것 (2) - 예제 + Memory Leak 메모리 릭 방지 (0) | 2022.07.01 |
5. 힙 메모리 Heap memory 에 대한 모든 것 - Heap vs Stack (0) | 2022.06.30 |
4. 스택 메모리 - 스택 프레임 Stack Frame, Call Stack, this (0) | 2022.06.16 |
3. 메모리 속 변수 타입 Variable Types - Struct, class, 포인터의 크기 (0) | 2022.06.16 |
1. 메모리 속 변수들에 대한 설명 - Stack 스택 (0) | 2022.06.16 |
sizeof()
: 변수의 사이즈를 알아내는 방법
예제)


1 byte = 8 bit.
컴퓨터는 0과 1로 동작하는데, 그 단위가 1bit.
8bit는 이런 0과 1로 동작하는 칸이 8개 있다는 의미.
int 타입은 4byte 를 가지고 있다.
a 변수를 할당하는 순간 4byte의 공간을 차지한다는 의미.
정수 short int long = 2 4 8 byte
양수정수에는 unsigned를 붙여주면 됨.
실수 float double = 4 8 byte
cppreference에서 각 변수 타입의 크기를 체크해보면 이렇다.

https://en.cppreference.com/w/cpp/language/types
Fundamental types - cppreference.com
(See also type for type system overview and the list of type-related utilities that are provided by the C++ library) [edit] Void type void - type with an empty set of values. It is an incomplete type that cannot be completed (consequently, objects of type
en.cppreference.com
at least 16이란 것은, int형의 사이즈가 컴파일러마다 다를 수 있다는 의미.
변수형의 사이즈를 확실하게 하고 싶을 때 해결 방법
1) compilation assertion 사용하기.
: 컴파일 과정에서 어떤 조건이 만족하는지 만족하지 않는지 평가해줌.


2) Fixed Type 이용하기
cppreference에서 fixed width integer types를 검색해보면,
https://en.cppreference.com/w/cpp/types/integer
Fixed width integer types (since C++11) - cppreference.com
int8_tint16_tint32_tint64_t(optional) signed integer type with width of exactly 8, 16, 32 and 64 bits respectivelywith no padding bits and using 2's complement for negative values(provided if and only if the implementation directly supports the type) (type
en.cppreference.com



메모리 레이아웃을 정확히 생각해야 할 때는 밑처럼 사용해주면 좋다
#include <cstdint> 해주고,


Array의 경우


CPP 에서 Array를 사용할 때에는 std array를 사용해서 프로그램을 짜는게 바람직하다.
#include <array> 해주고,


Pointer의 경우
uint64_t의 포인터의 크기를 알아보자.
uint64의 포인터를 저장하는 ui64ptr을 만들고 &ui8을 넣어보자.
그리고 ui64ptr의 size와 주소를 출력해 보면,



*포인터의 크기는 실행 파일이 컴파일된 아키텍처에 따라 달라진다.
32비트 실행파일은 32비트 메모리 주소를 사용하므로 포인터의 크기는 32 bit == 4 byte이다.
64비트 실행 파일에서 포인터의 크기는 64 bit == 8 byte이다.
그림으로 이해해 보면,

프로세스
: 컴퓨터는 스택 메모리의 탑 위치에서 몇 번째에 있는지 계산해서,
포인터 정보를 보니 메모리 주소가 저장되어 있다는 것을 알고,
메모리 주소를 찾아가 보니까 실제 변수가 써 있었던 맨 아래칸 위치에 접근할 수 있었던 것.
정리
long / int / float -> 4bytes
ptr -> 4bytes (32bit환경에서) / 8bytes (64bit환경에서)
'모던C++ > 메모리 구조 Memory Structure' 카테고리의 다른 글
7. 힙 스택 스태틱 Heap Stack Static - 언제 어떻게 사용해야 할까 (0) | 2022.07.06 |
---|---|
6. 힙 메모리 Heap Memory 에 대한 모든 것 (2) - 예제 + Memory Leak 메모리 릭 방지 (0) | 2022.07.01 |
5. 힙 메모리 Heap memory 에 대한 모든 것 - Heap vs Stack (0) | 2022.06.30 |
4. 스택 메모리 - 스택 프레임 Stack Frame, Call Stack, this (0) | 2022.06.16 |
3. 메모리 속 변수 타입 Variable Types - Struct, class, 포인터의 크기 (0) | 2022.06.16 |
1. 메모리 속 변수들에 대한 설명 - Stack 스택 (0) | 2022.06.16 |