source

긴 vsInter C/C++ - 포인트

goodcode 2022. 8. 21. 14:09
반응형

긴 vsInter C/C++ - 포인트

최근에 알게 된 사실이지만longC/C++의 길이는 C/C++와 같습니다.int쉽게 말하면, 왜?언어에 자료형을 포함시키는 것조차 거의 무의미해 보입니다.특정 용도가 있습니까?int없나요?64비트를 선언할 수 있습니다.int다음과 같이 합니다.

long long x = 0;

하지만 왜 그 언어는 단지 언어적 요소를 만드는 것보다 이런 방식을 택할까?long음...보다 더...intC# 등 다른 언어에서도 사용 가능하므로 C/C++는 어떻습니까?

C 또는 C++로 쓸 때 모든 데이터 유형은 아키텍처와 컴파일러에 따라 다릅니다.1개의 시스템 int는 32이지만 16 또는 64의 시스템 int를 찾을 수 있습니다.정의되어 있지 않기 때문에 컴파일러에 따라 달라집니다.

에 대해서는long그리고.int표준 정수가 16비트였던 시간에서 유래합니다.long32비트 정수였습니다.정확히 그 길이보다 길었어요int.

구체적인 보증은 다음과 같습니다.

  • char최소 8비트(정의로는 1바이트, 아무리 많은 비트라도)
  • short최소 16비트
  • int최소 16비트
  • long최소 32비트
  • long long(이를 지원하는 언어 버전에서는) 최소 64비트
  • 위 목록의 각 유형은 적어도 이전 유형과 같은 너비입니다(단, 거의 동일할 수 있습니다).

따라서 다음과 같이 사용하는 것이 이치에 맞다.long최소 32비트 타입이 필요하시면int상당히 빠르고 최소 16비트 타입이 필요한 경우.

실제로 적어도 C에서는 이러한 하한을 크기가 아닌 범위로 나타냅니다.예를 들어 언어에는 다음이 필요합니다.INT_MIN <= -32767,그리고.INT_MAX >= +3276716비트의 요건은 이것과 정수를 바이너리로 나타낸다는 요건에서 비롯됩니다.

C99 추가<stdint.h>그리고.<inttypes.h>다음과 같은 유형을 정의합니다.uint32_t,int_least32_t,그리고.int_fast16_t; 이들은 typedefs로 보통 미리 정의된 유형의 에일리어스로 정의됩니다.

(크기와 범위 사이에 반드시 직접적인 관계가 있는 것은 아닙니다.구현에 의해int32비트, 단, 예를 들어 범위만,-2**23 .. +2^23-1나머지 8비트(패딩 비트라고 함)는 값에 영향을 주지 않습니다.이론적으로는 가능성이 있다(그러나 실제로는 거의 없다).int보다 클 수 있다long,하는 한은long적어도 와 같은 범위가 있다int실제로 패딩 비트나 2의 보완 이외의 표현을 사용하는 현대 시스템은 거의 없지만, 표준에서는 여전히 이러한 기묘한 표현을 허용하고 있습니다.임베디드 시스템에서는, 이색적인 기능을 접할 가능성이 높아집니다).

looking for something completely unrelated and stumbled across this and needed to answer. Yeah, this is old, so for people who surf on in later...

Frankly, I think all the answers on here are incomplete.

The size of a long is the size of the number of bits your processor can operate on at one time. It's also called a "word". A "half-word" is a short. A "doubleword" is a long long and is twice as large as a long (and originally was only implemented by vendors and not standard), and even bigger than a long long is a "quadword" which is twice the size of a long long but it had no formal name (and not really standard).

Now, where does the int come in? In part registers on your processor, and in part your OS. Your registers define the native sizes the CPU handles which in turn define the size of things like the short and long. Processors are also designed with a data size that is the most efficient size for it to operate on. That should be an int.

On todays 64bit machines you'd assume, since a long is a word and a word on a 64bit machine is 64bits, that a long would be 64bits and an int whatever the processor is designed to handle, but it might not be. Why? Your OS has chosen a data model and defined these data sizes for you (pretty much by how it's built). Ultimately, if you're on Windows (and using Win64) it's 32bits for both a long and int. Solaris and Linux use different definitions (the long is 64bits). These definitions are called things like ILP64, LP64, and LLP64. Windows uses LLP64 and Solaris and Linux use LP64:

Model      ILP64   LP64   LLP64
int        64      32     32
long       64      64     32
pointer    64      64     64
long long  64      64     64

Where, e.g., ILP means int-long-pointer, and LLP means long-long-pointer

To get around this most compilers seem to support setting the size of an integer directly with types like int32 or int64.

long is not the same length as an int. According to the specification, long is at least as large as int. For example, on Linux x86_64 with GCC, sizeof(long) = 8, and sizeof(int) = 4.

long와 같은 사이즈가 아닙니다.int적어도 같은 크기입니다.intC++03 표준(3.9.1-2)을 인용하려면:

"signed char", "short int", "int" 및 "long int"의 4가지 부호 있는 정수 유형이 있습니다.이 목록에서 각 유형은 목록의 이전 유형만큼 저장 공간을 제공합니다.플레인 int는 실행 환경의 아키텍처에서 권장되는 자연 크기를 가집니다.다른 부호 있는 정수 유형은 특별한 요구를 충족하기 위해 제공됩니다.

이에 대한 저의 해석은 "그냥 사용"입니다.int하지만 어떤 이유로든 당신의 요구에 맞지 않고, 더 적합한 다른 필수 타입을 찾을 수 있다면, 그 대신 그 타입을 사용하세요.한 가지 방법으로는long그게 더 나을 수도 있어요. 만약 당신이 있는 건축물에 있다면...더 긴.

언급URL : https://stackoverflow.com/questions/7456902/long-vs-int-c-c-whats-the-point

반응형