memory and pointers

Memory

Int Main()

what is memory?

int main()
{
  int x = 3;
  int y = 2;
  int z = 5;
}
int x
int z
int y
3
2
5
???
???
???

memory

Memory

Int Main()

what is memory?

int main()
{
  int x = 3;
  int y = 2;
  int z = 5;
}
int x
int z
int y
3
2
5
???
???
???

memory

Memory

Int Main()

what is memory?

int foo()
{
  int n = 3;
}

int main()
{
  int x = 3;
  int y = 2;
  foo();
  int z = 5;
}
int x
int y
3
2
???
???

Int FOO()

int n
3
???

memory

Memory

Int Main()

what is memory?

int foo()
{
  int n = 3;
}

int main()
{
  int x = 3;
  int y = 2;
  foo();
  int z = 5;
}
int x
int y
3
2
???
???

Int FOO()

int n
3
???
int z
5
???

memory

Memory

Int Main()

what is memory?

int foo()
{
  int n = 3;
}

int main()
{
  int x = 3;
  int y = 2;
  foo();
  int z = 5;
}
int x
int z
int y
3
2
5
???
???
???

memory

the stack

what is memory?

ebp + 0x00
ebp + 0x01
ebp + 0x02
ebp + 0x03
ebp + 0x04
ebp + 0x05
ebp + 0x06
ebp + 0x07
ebp + 0x08
ebp + 0x09
ebp + 0x0a
ebp + 0x0b
ebp + 0x0c
ebp + 0x0d
ebp + 0x0e
ebp + 0x0f
ebp + 0x10
ebp + 0x11
ebp + 0x12
ebp + 0x13
ebp + 0x14
int main()
{
  int x = 3;
  int y = 2;
  int z = 5;
}
???
???
???
???
???
???
???
???
???
???
???
???
???
???
???
???
???
???
???
???
???

*Assuming big-endian

*x86 register names

esp

memory

what is memory?

ebp + 0x00
ebp + 0x01
ebp + 0x02
ebp + 0x03
ebp + 0x04
ebp + 0x05
ebp + 0x06
ebp + 0x07
ebp + 0x08
ebp + 0x09
ebp + 0x0a
ebp + 0x0b
ebp + 0x0c
ebp + 0x0d
ebp + 0x0e
ebp + 0x0f
ebp + 0x10
ebp + 0x11
ebp + 0x12
ebp + 0x13
ebp + 0x14
int main()
{
  int x = 3;
  int y = 2;
  int z = 5;
}
0
0
0
3
???
???
???
???
???
???
???
???
???
???
???
???
???
???
???
???
???

*Assuming big-endian

*x86 register names

esp

the stack

memory

what is memory?

ebp + 0x00
ebp + 0x01
ebp + 0x02
ebp + 0x03
ebp + 0x04
ebp + 0x05
ebp + 0x06
ebp + 0x07
ebp + 0x08
ebp + 0x09
ebp + 0x0a
ebp + 0x0b
ebp + 0x0c
ebp + 0x0d
ebp + 0x0e
ebp + 0x0f
ebp + 0x10
ebp + 0x11
ebp + 0x12
ebp + 0x13
ebp + 0x14
int main()
{
  int x = 3;
  int y = 2;
  int z = 5;
}
0
0
0
3
0
0
0
2
???
???
???
???
???
???
???
???
???
???
???
???
???

*Assuming big-endian

*x86 register names

esp

the stack

memory

what is memory?

ebp + 0x00
ebp + 0x01
ebp + 0x02
ebp + 0x03
ebp + 0x04
ebp + 0x05
ebp + 0x06
ebp + 0x07
ebp + 0x08
ebp + 0x09
ebp + 0x0a
ebp + 0x0b
ebp + 0x0c
ebp + 0x0d
ebp + 0x0e
ebp + 0x0f
ebp + 0x10
ebp + 0x11
ebp + 0x12
ebp + 0x13
ebp + 0x14
int main()
{
  int x = 3;
  int y = 2;
  int z = 5;
}
0
0
0
3
0
0
0
2
0
0
0
5
???
???
???
???
???
???
???
???
???

*Assuming big-endian

*x86 register names

esp

the stack

memory

memory

what is memory?

ebp + 0x00
ebp + 0x01
ebp + 0x02
ebp + 0x03
ebp + 0x04
ebp + 0x05
ebp + 0x06
ebp + 0x07
ebp + 0x08
ebp + 0x09
ebp + 0x0a
ebp + 0x0b
ebp + 0x0c
ebp + 0x0d
ebp + 0x0e
ebp + 0x0f
ebp + 0x10
ebp + 0x11
ebp + 0x12
ebp + 0x13
ebp + 0x14
int main()
{
  int x = 3;
  int y = 2;
  int z = 5;
}
0
0
0
3
0
0
0
2
0
0
0
5
???
???
???
???
???
???
???
???
???

*Assuming big-endian

*x86 register names

esp

the stack

endianness

what is endianness?

ebp + 0x00
ebp + 0x01
ebp + 0x02
ebp + 0x03
ebp + 0x04
ebp + 0x05
ebp + 0x06
ebp + 0x07
ebp + 0x08
ebp + 0x09
ebp + 0x0a
ebp + 0x0b
ebp + 0x0c
ebp + 0x0d
ebp + 0x0e
ebp + 0x0f
ebp + 0x10
ebp + 0x11
ebp + 0x12
ebp + 0x13
ebp + 0x14
3
0
0
0
2
0
0
0
5
0
0
0
???
???
???
???
???
???
???
???
???

little endian

memory

ebp + 0x00
ebp + 0x01
ebp + 0x02
ebp + 0x03
ebp + 0x04
ebp + 0x05
ebp + 0x06
ebp + 0x07
ebp + 0x08
ebp + 0x09
ebp + 0x0a
ebp + 0x0b
ebp + 0x0c
ebp + 0x0d
ebp + 0x0e
ebp + 0x0f
ebp + 0x10
ebp + 0x11
ebp + 0x12
ebp + 0x13
ebp + 0x14
0
0
0
3
0
0
0
2
0
0
0
5
???
???
???
???
???
???
???
???
???

big endian

memory

Size and Alignment

alignment

size

char
short
int
long long

memory

Size and Alignment

alignment

size

char
short
int
long long

memory

sizeof(char) == 1
alignof(char) == 1

Size and Alignment

alignment

size

char
short
int
long long

memory

sizeof(short) == 2
alignof(short) == 2

Size and Alignment

alignment

size

char
short
int
long long

memory

sizeof(int) == 4
alignof(int) == 4

Size and Alignment

alignment

size

char
short
int
long long

memory

sizeof(long long) == 8
alignof(long long) == 8

Size and Alignment

alignment

size

memory

struct my_data
{
  short s1;
  short s2;
  int x;
} my_object;
sizeof(my_data) == ...
alignof(my_data) == ...

Size and Alignment

alignment

size

memory

struct my_data
{
  short s1;
  short s2;
  int x;
} my_object;
sizeof(my_data) == 8
alignof(my_data) == ...
my_object.s1
my_object.s2
my_object.x

Size and Alignment

alignment

size

memory

struct my_data
{
  short s1;
  short s2;
  int x;
} my_array[2];
sizeof(my_data) == 8
alignof(my_data) == 8
my_array[0].s1
my_array[0].s2
my_array[0].x
my_array[1].s1
my_array[1].s2
my_array[1].x

Size and Alignment

alignment

size

memory

struct my_data
{
  short s1;
  int x;
  short s2;
} my_object;
sizeof(my_data) == ...
alignof(my_data) == ...

Size and Alignment

alignment

size

memory

struct my_data
{
  short s1;
  int x;
  short s2;
} my_object;
sizeof(my_data) == ...
alignof(my_data) == ...
my_object.s1
my_object.s2
my_object.x

Size and Alignment

alignment

size

memory

struct my_data
{
  short s1;
  int x;
  short s2;
} my_object;
sizeof(my_data) == 10???
alignof(my_data) == ...
padding
my_object.s1
my_object.s2
my_object.x

memory

struct my_data
{
  short s1;
  int x;
  short s2;
} my_array[2];

Size and Alignment

alignment

size

padding
sizeof(my_data) == 10???
alignof(my_data) == ...
my_array[0].s1
my_array[0].s2
my_array[0].x

memory

Size and Alignment

alignment

size

struct my_data
{
  short s1;
  int x;
  short s2;
} my_array[2];
sizeof(my_data) == 10???
alignof(my_data) == ...
my_array[0].s1
my_array[0].s2
my_array[0].x
my_array[1].s1
my_array[1].s2
my_array[1].x

memory

Size and Alignment

alignment

size

struct my_data
{
  short s1;
  int x;
  short s2;
} my_array[2];
sizeof(my_data) == 12
alignof(my_data) == 4
my_array[1].s1
my_array[1].s2
my_array[1].x
my_array[0].s1
my_array[0].s2
my_array[0].x

memory

Size and Alignment

alignment

size

struct my_data
{
  short s1;
  int x;
  short s2;
} my_object;
sizeof(my_data) == 12
alignof(my_data) == 4
my_object.s1
my_object.s2
my_object.x

Size and Alignment

alignment

size

sizeof(type)

sizeof(expression)

//sizeof(int) == 4

short s;
//sizeof(s) == 2

//sizeof('a') == 1
//alignof(int) == 4

short s;
//alignof(s) == 2

//alignof('a') == 1

alignof(type)

alignof(expression)

alignas(type)

alignof(aligment)

alignas (int) short x;
// alignof(x) == 4
// alignof(decltype(x)) == 2

alignas (128) int n;
// alignof(n) == 128
// alignof(decltype(n)) == 4

bitfields

what are bitfields?

#include <cstdint>

struct ipv4_header
{
  unsigned version : 4;
  unsigned ihl : 4;
  unsigned dscp : 6;
  unsigned ecn : 2;
  uint16_t total_length;
  uint16_t identification;
  unsigned flags : 3;
  unsigned fragment_offset : 13;
  uint8_t time_to_live;
  uint8_t protocol;
  uint16_t header_checksum;
  uint32_t source_ip_address;
  uint32_t destination_ip_address;
  uint64_t options_upper;
  uint64_t options_lower;
}
int8_t x1; // 8 bits
int16_t x2; // 16 bits
int32_t x3; // 32 bits
int64_t x4; // 64 bits

int b1 : 9; // 9 bits
int b2 : 13; // 13 bits;
int b3 : 120; // error: 120 bits > #int_bits

bitfields

what are bitfields?

#include <cstdint>

struct ipv4_header
{
  unsigned version : 4;
  unsigned ihl : 4;
  unsigned dscp : 6;
  unsigned ecn : 2;
  uint16_t total_length;
  uint16_t identification;
  unsigned flags : 3;
  unsigned fragment_offset : 13;
  uint8_t time_to_live;
  uint8_t protocol;
  uint16_t header_checksum;
  uint32_t source_ip_address;
  uint32_t destination_ip_address;
  uint64_t options_upper;
  uint64_t options_lower;
}

object

=
Data

object

sizeof(name)
alignof(name)
.................
decltype(name)
name
#name
.................
size
aligment
storage class
type
value
name
lifetime
(different than typeid(name))
(optional)
(may be undefined)
sizeof(name)
alignof(name)
.................
decltype(name)
name
#name
.................
size
aligment
storage class
type
value
name
lifetime
(different than typeid(name))
(optional)
(may be undefined)

object

object

object

"          " : {
  "memory_locations" : [
    "location_1" : {
      "subobject" : { ... } or "bitfields" : [...]
    },
    "location_2" : {
      "subobject" : { ... } or "bitfields" : [...]
    },
    "location_3" : {
      "subobject" : { ... } or "bitfields" : [...]
    },
    ...,
    "location_N" : {
      "subobject" : { ... } or "bitfields" : [...]
    },
  ]
}

object

struct my_type {
  int x;
  char b;
  int f0 : 3;
  int f1 : 2;
  int f2 : 7;
  int : 0;
  int f3 : 27;
  std::string s;
} data;

object

object

data

has

data

Is

   sizeof(data) ==
&& alignof(data) ==
&& std::is_same_v<decltype(data), my_data>()

data

x
b
f0
f1
f2
s
f3

memory location

object

data

x
b
f0
f1
f2
s
f3

memory location

address

object

address

object

pointers

(addresses)

Prototype

Address operator

Dereference operator

Member access operator

Type*
&object
*pointer
pointer->member
struct type
{
  int member1;
  bool member2;
}

type object = {1, true};

type* obj_ptr = &object;
(*obj_ptr) = {0, false};
obj_ptr->member12 = true;

pointers

(addresses)

Prototype

Address operator

Dereference operator

Member access operator

Type*
&object
*pointer
pointer->member
int arr[3] = {1, 2, 3};

int* arr_ptr = arr;
arr_ptr + 2 == &arr[2];
*(&arr[10] - 9) == 2;

Arrays as

Array

arr[0]
arr[1]
arr[2]

pointers

(addresses)

Prototype

Address operator

Dereference operator

Member access operator

Type*
&object
*pointer
pointer->member
const char* greeting = "Hi!";

greeting[0] == 'H';
greeting[3] == '\0';
*(greeting + 2) == '!';
*(&greeting[1]) == *(greeting + 1);

STRings as

"Hi!"
char[4]{'H', 'i', '!', '\0'}
=

pointers

(addresses)

Prototype

Address operator

Dereference operator

Member access operator

Type*
&object
*pointer
pointer->member
int arr[5] = {1, 2, 3, 4, 5};
int* ptr_to_array = arr;
int** ptr_to_ptr_to_array = &ptr_to_array;
int*** ptr_to_ptr_to_ptr_to_array = &ptr_to_array;
int**** ptr_to_ptr_to_ptr_to_ptr_to_array = &ptr_to_array;
...

pointers to

pointers

(addresses)
#include <iostream>

int* find_number_in_array(int* arr, int size, int number)
{
  for (int i = 0; i < size; ++i)
  {
    if (arr[i] == number)
    {
      return arr + i;
    }
  }
  return nullptr;
}

int main()
{
  int arr[5] = {1, 2, 3, 4, 5};
  
  int desired;
  std::cin >> desired;
  
  int* found = find_number_in_array(arr, 5, desired);
  if (found) // found != nullptr
  {
    std::cout << "Found number at position " << found - arr;
  }
  else
  {
    std::cout << "Number not found";
  }
}

null

memory and pointers

Memory and Pointers

By Jan Bielak

Memory and Pointers

A presentation about the memory and pointers in C++. It is presented here: https://www.youtube.com/watch?v=CSKsKfSJq6w .

  • 610