Data Types

2020-07-23 |

Data Types

  • Primitive: supported by programming languages built-in: numeric,
  • Numerics:
    • Integer
    • Real Numbers:
    • Fixed Point
    • Floating Point ==> fraction x base^exponent (IEEE 754) [We can't express every number with base 2]
    • Decimal: Banking applications (Binary coded decimals) ==> each decimal encoded with binaries
  • Binary
  • Character: We have different type of encoding, ASCII (8 bits), UTF-8, UTF-16
  • Strings: sequence of characters
  • Static Length String (Fortran): char asd [8] = "asdasd"; ==> asdasd__
  • Limited Dynamic Length (C / C++): char asd [8] = "asdasd"; ==> asdasd\0 end of the string.
  • Dynamic Lenght String: string asd = "asda";

User Defined Data Types

Enumerated

  • typedef, enum,
  • Example: enum Days {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
  • Make programs more readable

Array

array is a type constructor.

  • subscript binding a[8] verdiğimiz zaman 8 subscript oluyor
  • Static Array: global int A[5]; it will fiex
  • Fixed Stack Dynamic: array declared in function or subprograms int foo() { int A[10] };
  • Stack Dynamic: int foo(int size) { int A[size] };
  • Heap Dynamic: allocation done through heap memory int A[3], subscript ranges can be larged dynamically. int A[6] = 0; we can assign something like that.

Array Implementation

  • Array elements stored sequentially.
-------
100 - A[0]
104 - A[1]
108 - A[2]
112 - A[3]
116 - A[4]
-------

Multi-dimensional Array

Adress calculation will depend on row approach.

a : [ 1 2 3
      4 5 6 ]

a[0,0] = 1
a[1,0] = 4
  • Row Major: Go with row by row

  • Column Major: Go with column by column

Formulas:

$$ address(A[j, k]) = address(A[0, 0]) + (“no of rows before j” ∗ “size of a row”) + (k ∗ element size) $$

$$ address(A[j, k]) = address(A[0, 0]) + (j ∗ element size ∗ R) + (k ∗ element size) $$

Records (structs)

  • Aggregate of data elements.
  • Memory allocated for struct variables will be consecutive
struct foo()
{
  int a;
  char b;
  double d;
}

struct foo x;
x.a = 12;

Union

  • In a struct all fields are active.
  • Only one of the field is active.
  • Largest element will be selected for size.
  • We can use only one of the element from union.
  • We don't know which field is active <= programmers responsibility
union MyUnion {
  int i;
  char c;
}
MyUnion x;
x.i 

Pointer

  • Values are addresses.
  • Pointer arithmetic
a[0]
a[1]
a[2]
a[3]
a[4]

Example:

int a[5];
int *p;
int b;
p = a; // ==> a[0]
p = p+1 // ==> a[1] increment number of bytes 

Reference Types: Automaticcly deference


This was the end of the blog post. You can reach me via email umusasadik at gmail com