-2

Consider this two variable declaration. both of these declarations have data types. What is the actual usage of these data types.

int a;
MyClass b;
  1. Is there a part of each declared memory to hold the data type?
  2. Do these data types for human usage?
  3. Do these data types not required beyond compiler(after compiled the program)?
  4. Any good resource to read about this?
Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147

2 Answers2

1
  1. It is used to allocate the needed memory. Also it is used for (strong) type checking.
  2. Also (but that is not the main reason).
  3. Both. The compiler uses them, but afterwards dynamic behavior might be used depending on the object type.
  4. ?
Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
  • after allocate the memory, where it hold the memory variable type in the program? – Nayana Adassuriya Dec 19 '14 at 09:23
  • user2393256 already answers these questions in more detail. The compiler only translates the types into a language that the CPU understands; the CPU does not know really about types. Also note that in your application you can better not bother with details about how memory is allocated and where every variable is stored, because due to the OS it might change (using virtual memory, paging etc). – Michel Keijzers Dec 19 '14 at 09:30
1
  1. The compiler is going to allocate memory on the stack for this variables. You cannot tell how much memory is allocated because this depends on the compiler and the system you are compiling your source code. Variables in c++ are always allocated on the stack unless you use pointer. In that case they are allocated on the heap.

  2. In general yes. You CPU doesn't understand data types, in the end your code is compiled into a binary format (set of CPU instructions) to run on a CPU. You could as well write your program as a set of these instructions instead of c++. Then you would be using Assembler. But even Assembler is kind of a commodity interface to machine code since it has to be compiled an linked as well.

  3. Based on your code the compiler can probably do some optimization of the code (for example copy elision).

  4. I am not sure what you are expecting or trying to learn but i guess you could look for some compiler architecture literature.

user2393256
  • 1,001
  • 1
  • 15
  • 26
  • So after the compilation, there are no more data types, right? It means the size of the data type(eg: int) is depend on the compiling platform not the ruining platform. am I right? – Nayana Adassuriya Dec 19 '14 at 09:36
  • Not directly. The CPU instructions still make a difference between some kinds of values. For example not all ARM platforms can handle floating point numbers (efficiently). The Arithmetic Logic Unit uses registers to store values and process them using it's very on set of instructions. What kind of data fits in to a [register](https://en.wikipedia.org/wiki/Processor_register) is defined by the hardware. – user2393256 Dec 19 '14 at 09:45