• 검색 결과가 없습니다.

C/C++ Data Types

N/A
N/A
Protected

Academic year: 2021

Share "C/C++ Data Types"

Copied!
18
0
0

로드 중.... (전체 텍스트 보기)

전체 글

(1)

C/C++

Data Types

Wanho Choi

(2)

Bit / Byte

(3)

C/C++ : Data Types

여러 가지 자료형을 사용하는 이유

정수와 실수의 데이터 표현 방식이 다르기 때문

메모리를 효율적으로 사용하기 위해서

(4)

Example: 8 Bit Signed Integer

Int8 0 0 0 1 0 0 0 Sign Bit Value Bit = 4

Sign-Magnitude form

가장 왼쪽 비트: 부호(0: +, 1: -)

나머지 비트들: 크기

0 표현의 중복: +0, -0

1’s complement form:

가장 왼쪽 비트: 부호(0: +, 1: -)

음수일 때 보수로 표현

여전히 0 표현의 중복: +0, -0

2’s complement form:

가장 왼쪽 비트: 부호(0: +, 1: -)

음수일 때 보수+1로 표현

0 표현의 중복이 없음

(5)

C/C++ : Data Types & Sizes

64 bit OS 기준

Type Name Bytes

char 1 short 2 int 4 unsigned int size_t 8 long 8 long long 8 float 4 double 8 long double 16

Type Name Description

int8_t 1 byte signed integer

int16_t 2 byte signed integer

int32_t 4 bytes signed integer

int64_t 8 byte signed integer

uint8_t 1 byte unsigned integer

uint16_t 2 byte unsigned integer

uint32_t 4 bytes unsigned integer

(6)

C/C++ : Data Types & Ranges

64 bit OS 기준

bytes (bits)

range limit

min max min max char 1 (8) -128 (= 27) 127 (= 27-1) CHAR_MIN

-128 CHAR_MAX 127 unsigned char 1 (8) 0 255 (= 28-1) - UCHAR_MAX

255 short 2 (16) -32,768 (= 215) 32,767 (= 215-1) SHRT_MIN

-32,768 SHRT_MAX 32,767 unsigned short 2 (16) 0 65,535 (= 216-1) - USHRT_MAX

65,535 int 4 (32) -2,147,483,648 (= 231) 2,147,483,647 (= 231-1) INT_MIN

-2,147,483,648 2,147,483,647INT_MAX unsigned int 4 (32) 0 4,294,967,295 (= 232-1) - UINT_MAX

4,294,967,295 float 4 (32) -3.4 × 1038 3.4 × 1038 FLT_MIN

1.17549e-38 3.40282e+38FLT_MAX double 8 (64) -1.79 × 10308 1.79 × 10308 DBL_MIN

2.22507e-308 1.79769e+308DBL_MAX

(7)

Constant

Symbolic constant

이름을 가지는 변수 예) const double PI = 3.14;

Literal constant

변수 이름이 없는 상수 예) int a = 10 + 20; 에서 10과 20

연산을 위해서 사용되는 이러한 수들도 메모리에 저장되어야 한다.

데이터형에 대한 명시가 없다면

정수와 문자는 기본적으로 int형, 실수는 기본적으로 double형으로 자료형이 결정된다.

접미사(suffix)를 사용해서 자료형을 명시적으로(explicitly) 지정할 수도 있다.

123u 또는 123U: unsigned int

123l 또는 123L: long int

123ul 또는 123UL: unsigned long int

(8)

Implicit / Explicit Type Conversion

Implicit type conversion 예) int a = 1.23 + 3.14;

사용자가 개입하지 않아도 자동으로 자료형이 변환되는 것

큰 자료형에서 작은 자료형으로 변환시 데이터 손실이 발생할 수 있다.

위의 예의 경우 double형에서 int형으로 변환되면서 데이터 손실 발생!

Explicit type conversion 예) float a = (float)100.0;

이와 같이 자료형을 명시해서 자료형을 변환하는 것

역시 큰 자료형에서 작은 자료형으로 변환시 데이터 손실이 발생할 수 있다.

(9)

Overflow / Underflow

Overflow

위로 넘친다는 의미

처리할 수 있는 가장 큰 값보다 더 큰 값을 다루는 경우

INT_MAX + 1 = INT_MIN

Underflow

아래로 넘친다는 의미

처리할 수 있는 가장 작은 값보다 더 작은 값을 다루는 경우

INT_MIN - 1 = INT_MAX

The extended space is filled with the the most significant bit.

0 0 1 1 0 0 0 1 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0

(10)

Integer Promotion

short형 끼리 계산을 했고, overflow도 아닌데 int형으로 자동 형변환이 이루어졌음

크기가 4 bytes 보다 작은 값들을 연산할 때에는 자동적으로 4 bytes int형으로 변환되어 계산이 수행됨

CPU가 효율적으로 처리하기 좋기 때문

short a = 1, b =2;

cout << sizeof(a + b) << endl; // 4

double a = 123 + 3.14;

다른 자료형들끼리는 연산이 불가능함

따라서 자동 형변환이 이루어지는데, 이 때 손실을 최소화하는 방향으로 형변환이 일어남

(11)

Frequently Asked Question

cout << 25u - 50 << endl;

두 자료형이 다르기 때문에 둘 중 하나로 자료형이 통일되어야 함

“lower type”은 “higher type”으로 promoted됨

Type hierarchy

(highest) long double, double, float, unsigned long int, long int, unsigned int, int (lowest)

따라서, 여기서는 unsigned int형으로 자료형이 통일됨

unsigned int: higher type

int: lower type

그러므로 50은 50u로 promoted됨

(12)

다음 구조체의 크기는?

struct A { int x; // 4 bytes char y; // 1 byte }; struct C { char x; // 1 byte int y; // 4 bytes char z; // 1 byte }; struct B { char x; // 1 byte char y; // 1 byte int z; // 4 bytes };

(13)

다음 구조체의 크기는?

struct A { int x; // 4 bytes char y; // 1 byte }; struct C { char x; // 1 byte int y; // 4 bytes char z; // 1 byte }; struct B { char x; // 1 byte char y; // 1 byte int z; // 4 bytes };

(14)

다음 구조체의 크기는?

5 bytes

6 bytes

6 bytes

8 bytes

8 bytes

12 bytes

struct A { int x; // 4 bytes char y; // 1 byte }; struct C { char x; // 1 byte int y; // 4 bytes char z; // 1 byte }; struct B { char x; // 1 byte char y; // 1 byte int z; // 4 bytes };

(15)

Word

Processor reads 1 word at a time.

It doesn’t read 1 byte at a time from memory.

CPU가 레지스터에 한 번에 옮길 수 있는 데이터의 크기

32 비트 시스템: 1 WORD = 32 bit

64 비트 시스템: 1 WORD = 64 bit

Visual Studio에서는 option으로 조절할 수 있음

(16)

Structure Padding

struct A { int x; // 4 bytes char y; // 1 byte }; struct C { char x; // 1 byte int y; // 4 bytes char z; // 1 byte }; struct B { char x; // 1 byte char y; // 1 byte int z; // 4 bytes };

8 bytes

8 bytes

12 bytes

5 bytes

6 bytes

6 bytes

(17)

SOA / AOS

SOA (Structure of Array)

[position0, position1, position2, …]

[color0, color1,color2, …]

[normal0, ormal1,normal2, …]

AOS (Array of Structure)

[position0 / color0 / normal0]

[position1 / color1 / normal1]

[position2 / color2 / normal2]

(18)

참조

관련 문서

만성적 식량안보 문제는 기본적으로 경제개발이나 구조적 빈곤과 연계되어 있고 일시적인 식량안보의 위험(Transitory Food Crisis)은 식량 에 대한 접근이

- JR큐슈 카라이케 회장은 관광객 증가를 통한 경제 활성화가 마을 정비계획의 주된 목적이며, 이를 마을에 대한 주민들의 자신감으로 이어나가는 것이 가장 중요하다고 강조.

Rest, fresh air, sunshine and skillful nursing work miracles every

Types of phase diagrams formed when the high temperature allotrope forms a continuous series of solid solutions with the second component. β Zr ↔ α + β

각자 젂공이 있지만 젂공과 관렦된 젂시․조사․연구만 하는 것이 아니라 때로는 젂공과 무관핚 읷을 해야 하는 경우도 많음. 이러핚 경우에

화학량론적 계산:

For series 140.2 valves class “F” coils (155°C) are available encapsulated in ther- moplastic containing 30% glass fiber (types: ZB, YB) and class “H” coils (180°C)

with the experimental C versus t data. If the fit is unsatisfactory, another rate equation is guessed and tested. Integral method is especially useful for fitting simple