Introduction to C – Zeiger, Pointer,Credentials – Part2


A pointer is a variable, the memory address of the (RAMs) another variable contains.

int i = 100;
int* pi = &i;
cout << pi; //Speicheradresse: z.B. 0x000026

Dereferencing pointers: *Operator

  • access the value, on the one hand(Pointer) shows
int i = 100;
int* pi = &i;
cout << *pi; //100
cout << pi; //Speicheradresse: z.B. 0x000026

void pointer

  • Pointer to unknown type -> dynamic
  • must be cast in access
int i=10;
void* pi;
pi=&i;
cout << *(int*) pi;

NULL pointer

  • Free memory of the pointer, e.g.. to prevent zombie hands, pointing to non-existent objects
int i = 100;
int* pi = &i;
pi = NULL;

Pointers to constants

  • must also be const, not so changeable
const int i = 100;
const int* pi = &i;

Pointers to Functions

int f(float a){return 1;};
int (*pf)(float) = &f;
int x = (*pf)(1,5);

Pointers to functions

Call by Value

  • Function values ​​are copied, Transfer parameters are not permanently changed only within the function
  • values ​​may need to change references are passed (Call by Reference)
Bsp. char* strcpy(char* dest, const char* src));

Call By Reference

  • Parameter is permanently changed in the function
  • no copy of the parameters on the stack
  • if references are not to be changed, const set
Bsp. void f(int& a, const int& b);

Dynamic memory allocation

  • Memory allocation at runtime, when the memory requirement is unknown beforehand, e.g.. upon generation of dynamic arrays with unknown length

malloc()

  • returns pointer to void, must be cast
  • Number of bytes transferred
  • without initialization before
char* s = (char*)malloc(5*sizof(char)); //char mit Platz für 5 Zeichen

free()

  • dynamic memory should be freed
  • or automatically at the end of the program
free(s); //Speicher freigeben
s = NULL; //Referent auf leeren Speicher entfernen (Zombie)

Calloc()

  • initializes the memory previously
char* s = (char*)calloc(5*sizof(char)); //char mit Platz für 5 Zeichen

Realloc()

  • zoom in / out dynamic memory
char* sNew = (char*)realloc(s, 10*sizof(char)); //char mit Platz für 10 Zeichen von vorher 5

new-Operator

  • comfortable, no size calculation in bytes
  • Free the memory with delete[]
char* s = new char(5);
delete[] s;

Where are the components of the program stored in the RAM?

  1. Stack: Function variables, increased / decreased automatically when calling the next function
  2. Stack Pointer: next free address
  3. Heap: globale variableN und Code, dynamically created memory, to this memory should be cared for themselves

The order of the memory addresses is different, depending on the CPU hardware. Intel CPU Small Endian, other Big Endian.

int i = 100; //int werden als 2 Byte dargestellt, hexadezimal: 4*1 + 6*16 = 100

Big Endian dump:

Speicheradresse / Wert
0x2000004 / 0x64
0x2000008 / 0x00

Small Endian dump:

Speicheradresse / Wert
0x2000004 / 0x00
0x2000008 / 0x64

Contents:

Introduction to C - Advantages, Compiler, Namespaces - Teil1

Introduction to C - Pointer, Pointer,References - Part2

Introduction to C - Data Types, Casting, Classes - Part 3

Introduction to C - Functions and Operator Overloading (Overloading) - Part4

Introduction to C - Classes: Heredity, Adjustment, Friends, abstract - Part5