Giao Trinh BK Tech

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 69

C/C++

C++
Programming
C Author: Thai Van Sang

Version: 1.1.2023
C/C++ Course (18 sessions)
❖ Session 1 : Overview, Memory layout and Stack frame of C/C++ .
❖ Session 2 : Investigate about Primary variables and Scope of variables (const, static, extern, volatile).
❖ Session 3 : Investigate about Derived variables (Function, Default function, Variadic function, Array).
❖ Session 4 : Investigate about Derived variables (Pointer , Reference, Pointer & Array).
❖ Session 5 : Practice exercise 1.
❖ Session 6 : Type casting (Dynamic casting & Static casting) in C/C++.
❖ Session 7 : Investigate about Macro define and User defined variables (struct / enum / union).
❖ Session 8 : Investigate about User defined variables (Class).
❖ Session 9 : Practice exercise 2.
❖ Session 10 : Investigate about OOP (Class / Object / Encapsulation).
❖ Session 11 : Investigate about OOP (Polymorphism / Inheritance).
❖ Session 12 : Investigate about OOP (Abstract class / Friend class / Friend function).
❖ Session 13 : Investigate about OOP (Function overloading / Function overriding / Operator overloading).
❖ Session 14 : Practice exercise 3.
❖ Session 15 : Loops, Decision Making (For, While, Do-while, Switch case, If- Else,..).
❖ Session 16 : Vector, List, Set, Map.
❖ Session 17 : Implement project (Design virtual chip).
❖ Session 18 : Practice final exam.
Course Goals

G-01 G-04
01 Know and present 04 Know and practice
about C/C++ concepts basic OOP exercises

G-02 G-05
02 Know and practice 05 Know and practice
basic C/C++ exercises advanced OOP exercises

G-03 G-06
03 Know and present 06 Introduce C/C++ language
about OOP concepts application in embedded
system
Assessment

Q&A

Research Testing
Judgment
Learn online
Pass/Fail
Unit Design Coding

Pratice
How to compile a C/C++ program

I-1.0 : How to compile a C/C++ program


(Refer : https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html)
Lesson 1
C++ Install IDE

Visual Studio 2019 Visual Studio Code Dev C++

Lesson 1
How to write basic C/C++ program

Terminal:
sim.exe –Info class –Cmd 01

Lesson 1
Memory layout in C/C++

I-1.2 : Memory layout in C/C++


(Refer : https://youtu.be/9890mLEpECg)
DS - Memory layout in C/C++

I-1.3 : DS - Memory layout in C/C++


(Refer : https://youtu.be/9890mLEpECg)
BSS - Memory layout in C/C++

I-1.4 : BSS - Memory layout in C/C++


(Refer : https://youtu.be/9890mLEpECg)
HEAP - Memory layout in C/C++

I-1.5 : HEAP - Memory layout in C/C++


(Refer : https://youtu.be/9890mLEpECg)
STACK - Memory layout in C/C++

I-1.6 : Stack - Memory layout in C/C++


(Refer : https://youtu.be/9890mLEpECg)
Summary session 1
❑ Cách tạo một project bằng visual studio :
• File -> New -> Project - > Empty Project -> New -> Config your project (Name Project, Location Project,…)
-> Create.
❑ Cấu trúc một chương trình trong C/C++:
• Header file : #include <iostream>,…
• Body file : int main(int argc, char* argv[]). //project.e
❑ Memory layout trong c/c++:
• Text : Lưu trữ lệnh của chương trình.
• DS : Lưu biến global (toàn cục) và biến static được lập trình viên khởi tại với giá trị khác 0.
• BSS : Lưu biến global (toàn cục) và biến static được lập trình viên khởi tại với giá trị bằng 0 hoặc không được khởi
tạo.
• Heap : Là một nhớ động, bộ nhớ cho phép lập trình viên cấp phát và thu hồi.
Note: Khi thao tác với bộ nhớ Heap, cần lưu ý khi cấp phát buộc phải thu hồi, nếu không thì sẽ xảy ra “Memory
leak”.
• Stack : Là bộ nhớ để lưu trữ các biến local, stack frame của chương trình trong quá trình chạy (run_time).
C/C++ Variables

I-1.1 : Data types in C/C++


(Refer : https://www.geeksforgeeks.org/data-types-in-c/)
C/C++ Variables - Primary
Data Type Size (bytes) Range Format Specifier
short int 2 -32,768 to 32,767 %hd

unsigned short int 2 0 to 65,535 %hu

unsigned int 4 0 to 4,294,967,295 %u

int 4 -2,147,483,648 to 2,147,483,647 %d

long int 4 -2,147,483,648 to 2,147,483,647 %ld

unsigned long int 4 0 to 4,294,967,295 %lu

long long int 8 -(2^63) to (2^63)-1 %lld

unsigned long long int 8 0 to 18,446,744,073,709,551,615 %llu

signed char 1 -128 to 127 %c

unsigned char 1 0 to 255 %c

(Refer : https://www.geeksforgeeks.org/data-types-in-c/)
C/C++ Variables - Primary
Predict the output of source code
C/C++ Variables - Derived
Variables Derived

Function Array Reference

Stack frame Static Array Pointer

Local variable Dynamic Array Reference

Global variable Const Array

Static variable Reference Array

Const variable
Syntax - Function

I-1.7 : Function in C/C++


(Refer : https://youtu.be/9890mLEpECg)
Stack Frame- Function

I-1.7 : Stack Frame in C/C++


(Refer : https://youtu.be/9890mLEpECg)
Variadic function
Variadic functions are functions that can take a variable number of arguments. In C programming, a
variadic function adds flexibility to the program. It takes one fixed argument and then any number of
arguments can be passed. The variadic function consists of at least one fixed variable and then an
ellipsis(…) as the last parameter.
Variadic function
Methods Description
va_start(va_list This enables access to variadic function arguments.
ap, argN) where *va_list* will be the pointer to the last fixed argument in the variadic function
*argN* is the last fixed argument in the variadic function.
From the above variadic function (function_name (data_type variable_name, …);),
variable_name is the last fixed argument making it the argN. Whereas *va_list ap*
will be a pointer to argN (variable_name)

va_arg(va_list ap, This one accesses the next variadic function argument.
type) *va_list ap* is the same as above i.e a pointer to argN
*type* indicates the data type the *va_list ap* should expect (double, float, int etc.)

va_end(va_list ap) This ends the traversal of the variadic function arguments.

va_list Holds the information needed by va_start, va_arg, va_end, and va_copy.
Array

HEAP
Allocate dynamic

STACK
Allocate static
Syntax - Array
Syntax - Array
Static allocate & Dynamic allocate
Pointer & Reference
Pointers : A pointer is a variable that holds the memory address of another variable. A pointer needs to
be dereferenced with the * operator to access the memory location it points to.
References : A reference variable is an alias, that is, another name for an already existing variable. A
reference, like a pointer, is also implemented by storing the address of an object.

No References Pointers

The variable cannot be reassigned in


Reassignment The variable can be reassigned in Pointers.
Reference.

It shares the same address as the original


Memory Address Pointers have their own memory address.
variable.

Work It is referring to another variable. It is storing the address of the variable.

Null Value It does not have null value. It can have value assigned as null.

This variable is referenced by the method pass The pointer does it work by the method
Arguments
by value. known as pass by reference.
Pointer & Reference

01 Const Pointer

Pointer 02 Pointer to Const

03 Reference pointer
Pointer & Reference
Pointer & Reference

0x11223344
0x000000AA

0x000000AA mVar
0x0000FFFC

0x0000FFFC mP1

0xF000F000

mP2
Address of variable Value of variable
Pointer & Reference

0x0000FFFF
0xAAAA0000

8
ptr
0x0000FFFF

i,ref
Pointer & Reference

Pointer with “Const”


Pointer & Reference

Double Pointer
Pointer & Reference

Reference Pointer
Practice exercise 1
❖ 1. Write a C/C++ program to allocate a block of memory for an array.
Test Data and Expected Output :
Input the number of elements to be stored in the array :
+ 5 Input 5 elements in the array :
• Element 1 : 25
• Element 2 : 30
• Element 3 : 35
• Element 4 : 20
• Element 5 : 40
❑ Values entered in the array are : 25 30 35 20 40
❖ 2. Write a C/C++ program to reverse the elements of an array.
+ Array before reverse : 25 30 35 20 40
+ Array after revese : 40 20 35 30 25
❖ 3. Write a C/C++ program to sort the elements of an array.
+ Array before sort : 25 30 35 20 40
+ Array after sort : 20 25 30 35 40
❖ 4. Write a C/C++ program to perform a binary search in an array.

Refer : https://www.w3resource.com/c-programming-exercises/array/index.php
C/C++ Variables – User Defined

Variables User Defined

Class / Structure Union Enum / Typedef / Define


Typedef / Define
Struct / Union
Example – Typedef/ enum/ define
Class
01 02 03

Class/ Object Encapsulation Abstraction

08 04
Function Polymorphism
overloading

07 06 05
Operator
Friend function Inheritance
overloading
Class / Object
Syntax

Class ClassName

Access Modifiers

Private

Protected

Public
Class / Object
Constructor
Constructors are special class members which are called by the compiler every time an object of that
class is instantiated. Constructors have the same name as the class and may be defined inside or outside
the class definition.
Destructor
Destructor is another special member function that is called by the compiler when the scope of the object
ends.
Encapsulation / Abstraction

Abstraction Encapsulation
Encapsulation / Abstraction
Inheritance
Inheritance
Inheritance
Polymorphism
Polymorphism
Function overloading
Friend function
Operator overloading
Operators that can be overloaded Examples

Binary Arithmetic +, -, *, /, %

Unary Arithmetic +, -, ++, —

Assignment =, +=,*=, /=,-=, %=

Bitwise & , | , << , >> , ~ , ^

Function call ()

Subscript []

Relational >, < , = =, <=, >=


Example
Loops
Loops
Decision Making
Vector
List
Map
Design RAM

CPU RAM Memory

Register
APB bus AXI bus 4096
bytes

Data Move Controller

Overview block diagram


RAM Function
0xFFFF5000 0x11 0xFFFF5000 0x11
0xFFFF5001 0x22 0xFFFF5001 0x22
0xFFFF5002 0x33 0xFFFF5002 0x33
0xFFFF5003 0x44 0xFFFF5003 0x44
0xFFFF5004 0x55 0xFFFF5004 0x55
0xFFFF5005 0x66 0xFFFF5005 0x66
Move
0xFFFF5006 0x77 0xFFFF5006 0x77
0xFFFF5007 0x88 0xFFFF5007 0x88

0xFFFF5100 0x0 0xFFFF5100 0x11


0xFFFF5101 0x0 0xFFFF5101 0x22
0xFFFF5102 0x0 0xFFFF5102 0x33

Memory Memory
RAM Register & Memory

RAM base address : 0xFFFF0000 / Memory base address : 0xFFFF5000

Symbol Offset R/W Access size Description

CR xxx_base + 0x0 R/W 32 Control register

SRC xxx_base + 0x4 R/W 32 Source address register

DST xxx_base + 0x8 R/W 32 Destination address register


RAM Control Register (CR)
Bit : 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
- - - - - - - - - - LEN

Initial value : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
R/W : R R R R R R R R R R R/W R/W R/W R/W R/W R/W

Bit : 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
- - - - - - - - - - - INT - - - EN

Initial value : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
R/W : R R R R R R R R R R R R R R R R/W

Bit Bit Name Initial Value R/W Description


21 to 16 LEN 0 R/W Size move data

4 INT 0 R Status interrupt

0 EN 0 R/W Start move data


0 : Disable
1 : Enable. Auto set to 0 after finishing move operation.
RAM Source Address Register (SRC)
Bit : 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
SRC

Initial value : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
R/W : R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W

Bit : 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
SRC

Initial value : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
R/W : R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W

Bit Bit Name Initial Value R/W Description


31 to 0 SRC 0 R/W Source address
RAM Destination Address Register (DST)
Bit : 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
DST

Initial value : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
R/W : R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W

Bit : 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DST

Initial value : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
R/W : R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W

Bit Bit Name Initial Value R/W Description


31 to 0 DST 0 R/W Destination address
Operation

o SRC = source address


o DST = destination address

Check CR.EN == 1 ?
yes

no

Start operation
Dump message not CR.EN = 0
start operation

Received interrupt
FILE STRUCTURE
UT_Top

Register

Test Pattern

Bus
Ram Memory

Input data Inheritance Association


FILE TEST PATTERN
Example :

Explanation :
Command Argument 1 Argument 2
[WRITE] Address register Write data

[READ] Address register None

[CHECK] Expected data None

[PASS] TRUE or FALSE None

[END] none none

You might also like