C variable types and that keep track of this

I am taking a course from the MOOC CS50 from Harvard. One of the first lectures we learned about variables of different data types int, charetc.

I understand that a command (for example, inside a function main) int a = 5reserves several bytes (for the most part) of memory on the stack and places a sequence of zeros and ones that represent it 5.

The same sequence of zeros and ones can also mean a specific character. Therefore, someone needs to make sure that the sequence of zeros and ones in memory reserved for ashould be read as a whole (and not as a symbol).

The question is, who is tracking him? The computer’s memory by inserting a tag into this place in memory, saying “hey, whatever you find in these four bytes read as a whole”? Or a C compiler that knows (looking at the type intof a) that when my code asks it to do something (more precisely, to create machine code to do something) with a value a, does it need to consider this value as an integer?

I would really appreciate the answer tailored for beginner C.

+4
source share
5 answers

With the C language, this is a compiler.

At run time, there are only 32 bits = 4 bytes in the stack.

" , ...": ( - @Ivan). 8 (0 1) . , .

(, LISP, Java #), 32 , , . , , 6 32- . C . , - . , .

+3

C, .

, . , , ,... ( ); .

, , . .

, , . , .

C . , int double. .

( ) ( ), .

. , int to double:

int    i = 5;
double d = i; // Conversion.

, .

C, . , C . .

C , , , . , , .

+3

C ; , , . ( , , ..). , ( ) . , , , . " " ( ), ( ?) . , C : .

++ , ( ) . , - ( ). ++, C.

+2

, .

, , , . , , , , .

, , , . , .

+1

.

:

#include <stdio.h>

int main( void )
{
  long long x, y, z;

  x = 5;
  y = 6;
  z = x + y;

  printf( "x = %ld, y = %ld, z = %ld\n", x, y, z );
  return 0;
}

gcc -S , :

    movq    $5, -24(%rbp)
    movq    $6, -16(%rbp)
    movq    -16(%rbp), %rax
    addq    -24(%rbp), %rax
    movq    %rax, -8(%rbp)
    movq    -8(%rbp), %rcx
    movq    -16(%rbp), %rdx
    movq    -24(%rbp), %rsi
    movl    $.LC0, %edi
    movl    $0, %eax
    call    printf
    movl    $0, %eax
    leave
    ret

movq - 64- ( "quadwords" ). %rax - 64- , . .

, , long short s:

#include <stdio.h>

int main( void )
{
  short x, y, z;

  x = 5;
  y = 6;
  z = x + y;

  printf( "x = %hd, y = %hd, z = %hd\n", x, y, z );
  return 0;
}

, gcc -S , et voila:

    movw    $5, -6(%rbp)
    movw    $6, -4(%rbp)
    movzwl  -6(%rbp), %edx
    movzwl  -4(%rbp), %eax
    leal    (%rdx,%rax), %eax
    movw    %ax, -2(%rbp)
    movswl  -2(%rbp),%ecx
    movswl  -4(%rbp),%edx
    movswl  -6(%rbp),%esi
    movl    $.LC0, %edi
    movl    $0, %eax
    call    printf
    movl    $0, %eax
    leave
    ret

- movq movw movswl, %eax, 32 %rax ..

, :

#include <stdio.h>

int main( void )
{
  double x, y, z;

  x = 5;
  y = 6;
  z = x + y;

  printf( "x = %f, y = %f, z = %f\n", x, y, z );
  return 0;
}

gcc -S :

    movabsq $4617315517961601024, %rax
    movq    %rax, -24(%rbp)
    movabsq $4618441417868443648, %rax
    movq    %rax, -16(%rbp)
    movsd   -24(%rbp), %xmm0
    addsd   -16(%rbp), %xmm0
    movsd   %xmm0, -8(%rbp)
    movq    -8(%rbp), %rax
    movq    -16(%rbp), %rdx
    movq    -24(%rbp), %rcx
    movq    %rax, -40(%rbp)
    movsd   -40(%rbp), %xmm2
    movq    %rdx, -40(%rbp)
    movsd   -40(%rbp), %xmm1
    movq    %rcx, -40(%rbp)
    movsd   -40(%rbp), %xmm0
    movl    $.LC2, %edi
    movl    $3, %eax
    call    printf
    movl    $0, %eax
    leave
    ret

(movsd), (%xmm0).

So, in principle, after the translation there is no need to mark data with type information; this information is baked in the machine code itself.

0
source

Source: https://habr.com/ru/post/1691301/


All Articles