progweek2 Flashcards
the program starts to be executed with which function?
main()
Everytime program control encounters a function name, this function
will be _
called or invoked.
explain: The arguments are passed ”called-by-value”.
This means that each argument is evaluated, and the value is used locally for the corresponding formal parameter. The value is copied into the local variable.
If a variable is passed to a function by value, the original value of the
variable will (or will not) be changed?
not
What is scope?
It refers to the area where a function or variable is visible and
accessible to other codes.
”Identifiers are accessible only within the block in which they are
declared”
Be aware that the same identifier can be used in different declarations.
Parallel blocks are _
blocks defined at the same level.
text files:
It is a file that contains ASCII characters, used to store a stream of
characters.
key points of text files:
1
2
3
4
- Text files have no format, they are plain characters,
whitespaces and line breaks. - Each line in a text file ends with \n
- Example are files with .txt extension.
- They are a general and standard type to share information since they can be read or written by any text editor.
The basic file operations that we can perform in C are:
Creating a new file
Opening an existing file
Reading from a file
Writing to a file
Moving to a specific location in a file
Closing a file
Creating a new file;
fopen() in mode ”a”/”a+” or ”w”/”w+”
Opening an existing file;
fopen()
Reading from a file;
fscanf() or fgets()
Writing to a file;
fprintf() or fputs()
Moving to a specific location in a file;
fseek()
Closing a file;
fclose()
A file pointer is
a reference to a
particular position in the opened file.
It is used in file handling to perform all file operations.
In C, we make use of the _ macro to declare the file pointer. The
macro is already define inside the _ header file.
FILE
stdio.h
calloc and malloc in which library?
stdlib.h
CPU :
(Central Processing Unit): The brain of the computer where most calculations take place
ALU :
(Arithmetic Logic Unit): Performs arithmetic and logic operations.
Control Unit:
Directs operations within the CPU, coordinating how data moves through it.
General Registers:
Small storage locations within the CPU that hold data temporarily during execution.
Memory:
Stores data and instructions that the CPU can quickly access. It is typically volatile, meaning it loses its contents when power is turned off.
Disc:
Refers to the storage devices like hard drives or SSDs that provide non-volatile storage, meaning they retain data when powered off.
MMU :
(Memory Management Unit): A hardware component responsible for handling virtual and physical address translations.
BUS:
A communication system that transfers data between components inside a computer, or between computers.
what are the Other I/O Devices:
Monitor
Keyboard
Mouse
Printer
physical memory:
any physical support that can be
used to store data. The physical memory of modern computer is organized
in a hierarchy (from fast-small to slow-big).
Registers:
Is space that is on the CPU and
is the working memory that allows the
processor to do basic operations.
Cache(s):
Is on the same chip of the CPU
or very close to it, and is therefore usually
very small but also very fast.
Primary Memory (RAM):
Is on a
separate component, medium size, slower
than cache memory.
Secondary Memory (Disks):
Your hard
drive, it is usually very large, but also much
slower than cache and RAM.
Instead of dealing with the physical memory
programmers work on _. This means that __.
virtual memory
they can assume that there is only one memory unit consisting of an array of contiguous memory cells as big as the programs need (max 4 GB for 32-bit systems).
Stack:
Portion of memory managed
by the runtime support of C.
Heap:
Portion of memory managed
by the programmer.
Static:
Portion of memory that
contains external (global) variables,
the code, constant values and any
static data.
Runtime support :
is a collection of system software services that enable the execution of programs, managing necessary tasks like memory allocation and input/output operations behind the scenes.
Stacks :
are arrays of data managed with a LIFO (Last in, first out) policy. The stack is where all the local variables of our program are stored.
When we call a function in C:
- The _ of the CPU and other
information needed to
restore the current state of the
program is pushed. - The _ is pushed.
- The _ are pushed and the values
of the actual parameters are
copied (pass-by-value). - Then _ are pushed.
- When we exit the function all the previous information is _.
- This information is usually called _.
- current state
- address of where the return value should be stored
- formal parameters
- local variables
- popped and
used to restore the correct status of the caller - activation record of the function
Pointers are :
variables that store the memory addresses of other variables, allowing direct access and manipulation of the memory allocated to those variables in a C program.
If x is a variable then &x denotes _ of x.
the address
If x is of type T , then &x is of type _, e.g., if x is an integer then
&x is of type _.
T *
int* integer pointer
One can therefore declare variables that are pointers to a type, e.g.,
int * p; declares _. We can therefore initialize it using _
a pointer to an integer
p = &x
To access the content of the cell whose address is p we can use the __
operator, i.e., if x is 1 and p is &x then _ is 1.
*
*p
_ is a special value that is used for empty pointers (pointers to
nothing).
NULL
Pointers allow to implement a different type of parameter passing
called _.
call-by-reference
The idea is that instead of passing the value to a function we pass
__. In this way we have direct access to the cell
containing the actual parameter and can modify its content.
a pointer to that value
Arrays :
are the simplest way of grouping data. An array is a sequence of
homogeneous data that can be accessed using their position.
The size of the array should be known at _.
compile time
Arrays are actually _.
addresses to the first element of the sequence
A is :
the address of A[0]
*A is just another way of writing _
A[0]
p + 1 is evaluated to the arithmetic operation _
p + sizeof(T )
p − 1 is evaluated to the arithmetic operation _
p − sizeof(T )
A[n] becomes _
∗ (A + n)
For arrays declared as T A[N], A is _. So, we cannot modify __, e.g., doing A + +, this also means that _.
not a variable (just a name)
A
&A = A
But remember that parameter passing is still _ for the array itself
by-value
arrays issues:
We cannot return arrays in functions.
We cannot have large arrays.
The size of the array must be known at compile time.
The solution to all three problems of arrays is to _.
use dynamic
allocation of memory
characteristics oh the heap:
The heap is much bigger than the stack.
Values on the heap do not disappear unless we explicitly free the
memory.
Allocating portions of the heap at run time is not problematic since it does not interfere with the working of functions.
The _ offers functions to manage the heap
standard library (stdlib.h)
calloc
(n, size): allocates n many contiguous cells of memory each of size size and initializes all bits to 0
malloc
(n*size): allocates n many contiguous cells of memory of size
size in total.
free(p):
takes a pointer to a piece of memory in the heap and
deallocates the pointed space.
In C _ are used to implement strings. A string is _
arrays
just an array of
characters.
char *s=”this is a string.” → __
char s[ ]= “this is a string” == _
immutable, like a constant
char A[ ]={‘t’,’h’,’i’,’s’,…} !!!
Note that every string must end with a “hidden” special character:
′\0′ !!! pas oublier les ‘ ‘ !!!!
any constant string is
_.
a constant array of characters whose elements cannot be modified
The standard library offers many functions that can help deal with strings.
To use them include _.
string.h
How to concatenate the string in
s1 and s2 and put the result in s1?
The programmer has to ensure
that _
strcat(char ∗ s1, const char ∗ s2)
s1 has enough space to contain the concatenation
__: compares s1 and s2
and returns an integer. The return value is :
<0 if s1 is __ than s2,
0 if _
a number > 0 if s1 is __ than s2
strcmp(const char ∗ s1, const char ∗ s2)
lexicographically smaller
the two strings are equal
lexicographically bigger
__: the characters of s2 are
copied in s1.
strcpy(char ∗ s1, const char ∗ s2)
__: returns the number of characters in s.
strlen(const char * s)
Important: If you need to pass a matrix as a parameter, the formal
parameter must include the size of the number of _.
columns