int의 크기는 컴파일러나 프로세서에 따라 달라집니까?
정수 크기는 컴파일러, OS 및 프로세서에 따라 달라집니까?
이 질문에 대한 답은 우리가 얼마나 실제적인 고려사항과 거리가 먼가에 달려있다.
궁극적으로, 이론적으로 C와 C++의 모든 것은 컴파일러에 의존하며 컴파일러에만 의존합니다.하드웨어/OS는 전혀 중요하지 않습니다.컴파일러는 모든 두께의 하드웨어 추상화 레이어를 자유롭게 구현하고 모든 것을 에뮬레이트할 수 있습니다.C 또는 C++의 실장에서는, C++ 의 실장을 막을 수 있는 것은 없습니다.int
언어 표준에 명시된 최소 요건을 충족할 수 있을 만큼 충분히 큰 경우, 모든 크기 및 표현을 포함하는 유형.이러한 추상화 수준의 실용적인 예는 쉽게 구할 수 있습니다. 예를 들어 Java와 같은 "가상 머신" 플랫폼을 기반으로 하는 프로그래밍 언어입니다.
단, C와 C++는 매우 효율적인 언어입니다.효율을 최대화하기 위해서는 C 또는 C++의 실장은 기반이 되는 하드웨어에서 파생된 특정 고려사항을 고려해야 합니다.따라서 각 기본 유형이 하드웨어에서 직접(또는 거의 직접) 지원되는 표현에 기반하는지 확인하는 것이 매우 중요합니다.그런 의미에서 기본 유형의 크기는 하드웨어에 따라 달라집니다.
즉, 64비트 하드웨어/OS 플랫폼을 위한 특정 C 또는 C++ 구현은 완전히 무료입니다.int
이는 메모리 128비트를 차지하는 71비트 1의 부호화된 통합형으로서, 다른 57비트를 항상 컴파일러 작성자의 여자친구 생년월일을 저장하는 데 필요한 패딩 비트로 사용합니다.이 실장은 C/C++ 프로그램의 이식성을 런타임 테스트하는 데에도 사용할 수 있습니다.그러나, 거기서 실장의 실용성은 끝납니다.일반 C/C++ 컴파일러에서는 이런 것을 볼 수 없습니다.
네, 프로세서(구체적으로는 ISA, 명령어 세트 아키텍처(x86, x86-64 등)와 프로그래밍 모델을 포함한 컴파일러에 의존합니다.예를 들어, 16비트 시스템에서 size of (int)는 2바이트였습니다. 32비트 시스템에는 4바이트가 있습니다.int
. It has been considered int
was the native size of a processor, i.e., the size of register. However, 32-bit computers were so popular, and huge number of software has been written for 32-bit programming model. So, it would be very confusing if 64-bit computer would have 8 bytes for int
. Both Linux and Windows remain 4 bytes for int
. But, they differ in the size of long
.
Please take a look at the 64-bit programming model like LP64 for most *nix and LLP64 for Windows:
- http://www.unix.org/version2/whatsnew/lp64_wp.html
- http://en.wikipedia.org/wiki/64-bit#64-bit_data_models
Such differences are actually quite embarrassing when you write code that should work both on Window and Linux. So, I'm always using int32_t
or int64_t
, rather than long
, via stdint.h.
http://www.agner.org/optimize/calling_conventions.pdf
"3 Data representation" contains good overview of what compilers do with integral types.
Yes, it would. Did they mean "which would it depend on: the compiler or the processor"? In that case the answer is basically "both." Normally, int
won't be bigger than a processor register (unless that's smaller than 16 bits), but it could be smaller (e.g. a 32-bit compiler running on a 64-bit processor). Generally, however, you'll need a 64-bit processor to run code with a 64-bit int.
Based on some recent research I have done studying up for firmware interviews:
The most significant impact of the processors bit architecture ie, 8bit, 16bit, 32bit, 64bit is how you need to most efficiently store each byte of information in order to best compute variables in the minimum number of cycles.
The bit size of your processor tells you what the natural word length the CPU is capable of handling in one cycle. A 32bit machine needs 2 cycles to handle a 64bit double if it is aligned properly in memory. Most personal computers were and still are 32bit hence the most likely reason for the C compiler typical affinity for 32bit integers with options for larger floating point numbers and long long ints.
Clearly you can compute larger variable sizes so in that sense the CPU's bit architecture determines how it will have to store larger and smaller variables in order to achieve best possible efficiency of processing but it is in no way a limiting factor in the definitions of byte sizes for ints or chars, that is part of compilers and what is dictated by convention or standards.
I found this site very helpful, http://www.geeksforgeeks.org/archives/9705, for explaining how the CPU's natural word length effects how it will chose to store and handle larger and smaller variable types, especially with regards to bit packing into structs. You have to be very cognizant of how you chose to assign variables because larger variables need to be aligned in memory so they take the fewest number of cycles when divided by the CPU's word length. This will add a lot of potentially unnecessary buffer/empty space to things like structs if you poorly order the assignment of your variables.
The simple and correct answer is that it depends on the compiler. It doesn't mean architecture is irrelevant but the compiler deals with that, not your application. You could say more accurately it depends on the (target) architecture of the compiler for example if its 32 bits or 64 bits.
Consider you have windows application that creates a file where it writes an int
plus other things and reads it back. What happens if you run this on both 32 bits and 64 bits windows? What happens if you copy the file created on 32 bits system and open it in 64 bits system?
You might think the size of int will be different in each file but no they will be the same and this is the crux of the question. You pick the settings in compiler to target for 32 bits or 64 bits architecture and that dictates everything.
Size of the int is equal to the word-length that depends upon the underlying ISA. Processor is just the hardware implementation of the ISA and the compiler is just the software-side implementation of the ISA. Everything revolves around the underlying ISA. Most popular ISA is Intel's IA-32 these days. it has a word length of 32bits or 4bytes. 4 bytes could be the max size of 'int' (just plain int, not short or long) compilers. based on IA-32, could use.
Data Types Size depends on Processor, because compiler wants to make CPU easier accessible the next byte. for eg: if processor is 32bit, compiler may not choose int size as 2 bytes[which it supposed to choose 4 bytes] because accessing another 2 bytes of that int(4bytes) will take additional CPU cycle which is waste. If compiler chooses int as 4 bytes CPU can access full 4 bytes in one shot which speeds your application.
Thanks
size of data type basically depends upon the type of compiler and compilers are designed on the basis of architecture of processors so externally data type can be considered to be compiler dependent.for ex size of integer is 2 byte in 16 bit tc compiler but 4 byte in gcc compiler although they are executed in same processor
Yes , I found that size of int in turbo C was 2 bytes where as in MSVC compiler it was 4 bytes.
Basically the size of int is the size of the processor registers.
ReferenceURL : https://stackoverflow.com/questions/2331751/does-the-size-of-an-int-depend-on-the-compiler-and-or-processor
'programing' 카테고리의 다른 글
python의 경우 오류 괄호 ''' 근처에 있는 MariaDB SQL 구문 (0) | 2022.09.13 |
---|---|
일반 ES6 클래스 메서드에서 스태틱 메서드를 호출합니다. (0) | 2022.09.13 |
Java에서 두 숫자를 곱하면 오버플로가 발생하는지 어떻게 확인할 수 있습니까? (0) | 2022.09.13 |
jQuery $(문서)ready 및 Update Panels? (0) | 2022.09.13 |
Conda를 통해 Python OpenCV를 설치하려면 어떻게 해야 하나요? (0) | 2022.09.13 |