Free Science Education Website with Virtual Classrooms

undefined
 
PROGRAMMAZIONE
PROCEDURALE
 
A.A. 2024/2025
 
DEFINITION AND
DECLARATIONS
 
DEFINITION AND DECLARATION
 
Defining something means providing all of the necessary
information to create that thing in its entirety.
D
e
f
i
n
i
n
g
 
a
 
f
u
n
c
t
i
o
n
 
m
e
a
n
s
 
p
r
o
v
i
d
i
n
g
 
a
 
f
u
n
c
t
i
o
n
 
b
o
d
y
;
D
e
f
i
n
i
n
g
 
a
 
v
a
r
i
a
b
l
e
 
m
e
a
n
s
 
a
l
l
o
c
a
t
i
n
g
 
m
e
m
o
r
y
 
f
o
r
 
i
t
.
 
Definitions are also declarations
Not only for functions but also for variables
 
Most of the time, when you declare a variable, you are
also providing the definition. What does it mean to define
a variable, exactly? It means you are telling the compiler
where to create the storage for that variable.
IN SUMMARY
 
A declaration provides basic attributes of a symbol: its
type and its name.
A definition provides all of the details of that symbol--if
it's a function, what it does; if it's a variable, where that
variable is stored.
Often, the compiler only needs to have a declaration for
something in order to compile a file into an object file,
expecting that the linker can find the definition from
another file. If no source file ever defines a symbol, but it
is declared, you will get errors at link time complaining
about undefined symbols.
T
h
e
r
e
 
c
a
n
 
b
e
 
o
n
l
y
 
o
n
e
 
d
e
f
i
n
i
t
i
o
n
 
o
f
 
t
h
e
 
s
a
m
e
v
a
r
i
a
b
l
e
,
 
b
u
t
 
m
a
n
y
 
d
e
c
l
a
r
a
t
i
o
n
s
 
o
f
 
i
t
 
(
a
n
y
w
h
e
r
e
 
i
t
 
i
s
u
s
e
d
)
 
DIFFERENCE
 
A variable 
declaration
 says, "there is a variable with the
following name and type in the program".
A variable 
definition
 says, "Dear Mr. Compiler, please
allocate memory for a variable with the following name
and type now."
EXAMPLES
int func();
int main()
{
    int x = func();
}
 
func is declared
x is defined
 
int x;
int main()
{
    x = 3;
}
 
x is defined
 
int func()
{
    int x= 3;
}
 
x is defined
 
Of course,  since a definition is also a
declaration, they are also declared there
 
STORAGE DURATION
 
STORAGE CLASS SPECIFIERS
 
A
 
s
t
o
r
a
g
e
 
c
l
a
s
s
 
s
p
e
c
i
f
i
e
r
 
i
n
 
a
 
d
e
c
l
a
r
a
t
i
o
n
 
m
o
d
i
f
i
e
s
 
t
h
e
s
t
o
r
a
g
e
 
d
u
r
a
t
i
o
n
 
o
f
 
t
h
e
 
c
o
r
r
e
s
p
o
n
d
i
n
g
 
o
b
j
e
c
t
s
T
h
e
 
s
t
o
r
a
g
e
 
d
u
r
a
t
i
o
n
 
o
f
 
a
n
 
o
b
j
e
c
t
 
c
a
n
 
b
e
 
a
u
t
o
m
a
t
i
c
,
s
t
a
t
i
c
,
 
o
r
 
d
y
n
a
m
i
c
a
u
t
o
 
i
s
 
t
h
e
 
s
t
o
r
a
g
e
 
s
p
e
c
i
f
i
e
r
 
f
o
r
 
a
u
t
o
m
a
t
i
c
,
 
a
n
d
 
s
t
a
t
i
c
 
i
s
t
h
e
 
s
t
o
r
a
g
e
 
s
p
e
c
i
f
i
e
r
 
f
o
r
 
s
t
a
t
i
c
 
d
u
r
a
t
i
o
n
;
 
d
y
n
a
m
i
c
 
i
s
 
t
h
e
n
u
s
i
n
g
 
d
y
n
a
m
i
c
 
m
e
m
o
r
y
 
a
l
l
o
c
a
t
i
o
n
/
d
e
a
l
l
o
c
a
t
i
o
n
AUTO
a
u
t
o
:
 
O
b
j
e
c
t
s
 
d
e
c
l
a
r
e
d
 
w
i
t
h
 
t
h
e
 
a
u
t
o
 
s
p
e
c
i
f
i
e
r
 
h
a
v
e
a
u
t
o
m
a
t
i
c
 
s
t
o
r
a
g
e
 
d
u
r
a
t
i
o
n
.
This specifier is permissible only in object declarations
within a function.
I
n
 
A
N
S
I
 
C
,
 
o
b
j
e
c
t
s
 
d
e
c
l
a
r
e
d
 
w
i
t
h
i
n
 
a
 
f
u
n
c
t
i
o
n
 
h
a
v
e
a
u
t
o
m
a
t
i
c
 
s
t
o
r
a
g
e
 
d
u
r
a
t
i
o
n
 
b
y
 
d
e
f
a
u
l
t
,
 
a
n
d
 
t
h
e
 
a
u
t
o
s
p
e
c
i
f
i
e
r
 
i
s
 
a
r
c
h
a
i
c
.
int main(void) {
    int a;
    {
       int count;
       auto int month;
    }
    a++;
}
 
auto is not used in programs
 
REGISTER
 
r
e
g
i
s
t
e
r
:
 
Y
o
u
 
c
a
n
 
u
s
e
 
t
h
e
 
s
p
e
c
i
f
i
e
r
 
r
e
g
i
s
t
e
r
 
w
h
e
n
d
e
c
l
a
r
i
n
g
 
o
b
j
e
c
t
s
 
w
i
t
h
 
a
u
t
o
m
a
t
i
c
 
s
t
o
r
a
g
e
 
d
u
r
a
t
i
o
n
.
The register keyword is a hint to the compiler that the
object should be made as quickly accessible as
possible—ideally, by storing it in a CPU register.
However, the compiler may treat some or all objects
declared with register the same as ordinary objects with
automatic storage duration.
In any case, programs 
must not 
use the address
operator on objects declared with the register specifier.
EXAMPLE
{
    register int miles;
}
 
{
    register int miles;
    i
nt* p= &miles;
}
variable miles is not in memory. It is in a register
 
STATIC
 
S
t
a
t
i
c
:
 
a
 
v
a
r
i
a
b
l
e
 
i
d
e
n
t
i
f
i
e
r
 
d
e
f
i
n
e
d
 
w
i
t
h
 
t
h
e
 
s
p
e
c
i
f
i
e
r
s
t
a
t
i
c
 
h
a
s
 
s
t
a
t
i
c
 
s
t
o
r
a
g
e
 
d
u
r
a
t
i
o
n
.
It is created as soon as the program starts, and
destroyed (removed from memory) only when the
program ends.
Global variables automatically have 
static storage
duration.
EXAMPLE
int x= 0;
int main()
{
    x = 3;
}
 
This is the definition of a global
variable (x), which has static
storage duration
int main()
{
    static int x = 3;
}
 
This is the definition of a local
variable (x), which has static
storage duration, because I used
storage specifier 
static
 
SCOPE AND STORAGE DURATION
 
There are two separate concepts here:
scope
, which determines where a name can be accessed,
and
storage duration
, which determines when a variable is
created and destroyed.
Automatic
 variables (pedantically, variables with
automatic storage duration
) are local variables whose
lifetime ends when execution leaves their scope, and are
recreated when the scope is reentered.
Static
 variables (pedantically, variables with 
static
storage duration
) have a lifetime that lasts until the end
of the program. If they are local variables, then their
value persists when execution leaves their scope.
EXAMPLE 1
void
 f() {
    
int
 i;
    i = 
1
; 
// OK: in scope
}
void
 g() {
    i = 
2
; 
// Error: not in scope
}
 
int
 i;
void
 f() {
    i = 
1
; 
// OK: in scope
}
void
 g() {
    i = 
2
; 
// OK: still in scope
}
Local
 variables (pedantically, variables with
block scope
) are only accessible within the
block of code in which they are declared:
automatic storage duration
 
Global
 variables (pedantically, variables
with 
file scope
) are accessible at any point
after their declaration: 
static storage
duration
EXAMPLE 2
 
// prints 1 2 3 4 5   - the value persists
// n has static storage duration
 
// prints 1 1 1 1 1  - the previous value is lost
// n has automatic storage duration
// it is created anew for every loop iteration
// and destroyed at the end
for
 (
int
 i = 
0
; i < 
5
; ++i) {
    
int
 n = 
0
;
    printf(
"%d "
, ++n);
}
for
 (
int
 i = 
0
; i < 
5
; ++i) {
    
static
 
int
 n = 
0
;
    printf(
"%d "
, ++n);
}
EXAMPLE 3
for
 (
int
 i = 
0
; i < 
5
; ++i) {
    
static
 
int
 n = 
0
;
    printf(
"%d "
, ++n);
}
printf(
"%d "
, n);
 
// error: out of scope
 
Being forever in memory does not mean that a variable is always
accessible: it is accessible in its scope
EXAMPLE 4
#include<stdio.h>
void func(void);
int count=10;
i
nt main() 
{
   while (count--) 
         func();
}
void func( void )
{
    
static int i = 5;
    i++;
    printf("i is %d and count is %d\n", i, count);
}
   i is 6 and count is 9
   i is 7 and count is 8
   i is 8 and count is 7
   i is 9 and count is 6
   i is 10 and count is 5
   i is 11 and count is 4
   i is 12 and count is 3
   i is 13 and count is 2
   i is 14 and count is 1
   i is 15 and count is 0
 
Static storage is the default for
variables outside functions
(global variables)
EXAMPLE 5
#include<stdio.h>
void func(void);
int count=10;
i
nt main() 
{
   while (count--) 
         func();
    i++;
}
void func( void )
{
    
static int i = 5;
    i++;
    printf("i is %d and count is %d\n", i, count);
}
 
MacBook-Francesco:ProgrammI francescosantini$ gcc -std=c99 -o test test.c
test.c:11:4: error: use of undeclared identifier 'i'
   i++;
   ^
1 error generated.
 
i static but not visible in main
EXAMPLE 6
#include<stdio.h>
void func(void);
i
nt main() 
{
   while (count--) 
         func();
}
int count=10;
void func( void )
{
    
static int i = 5;
    i++;
    printf("i is %d and count is %d\n", i, count);
}
 
MacBook-Francesco:ProgrammI francescosantini$ gcc -std=c99 -o test test.c
test.c:8:11: error: use of undeclared identifier 'count'
   while (count--)
          ^
1 error generated.
 
count visible from here
STATIC STORAGE
G
l
o
b
a
l
 
v
a
r
i
a
b
l
e
s
,
 
o
r
 
v
a
r
i
a
b
l
e
s
 
w
i
t
h
i
n
 
a
 
f
u
n
c
t
i
o
n
 
a
n
d
 
w
i
t
h
t
h
e
 
s
t
o
r
a
g
e
 
c
l
a
s
s
 
s
p
e
c
i
f
i
e
r
 
s
t
a
t
i
c
,
 
h
a
v
e
 
s
t
a
t
i
c
 
s
t
o
r
a
g
e
d
u
r
a
t
i
o
n
.
All objects with static storage duration are initialized to 0
automatically.
Objects with automatic storage duration are not
initialized to 0 by default, their value is undefined
 It is better to always initialize variables to 0 (especially
variables with automatic storage duration)
 
int main() {
    static int a;
}
 
int main() {
    int a;
}
 
// initialized to 0
 
// not initialized
 
DIFFERENT KINDS OF
MEMORIES
 
DIFFERENT ZONES IN MEMORY
 
All your variables (and instructions) are in memory
 
CHARACTERISTICS
 
Permanent area contains both global and static variables
(and instructions): variables with 
static storage duration
.
S
u
c
h
 
v
a
r
i
a
b
l
e
s
 
a
r
e
 
c
r
e
a
t
e
d
 
w
h
e
n
 
t
h
e
 
p
r
o
g
r
a
m
 
s
t
a
r
t
s
 
e
n
d
d
e
s
t
r
o
y
e
d
 
w
h
e
n
 
t
h
e
 
p
r
o
g
r
a
m
 
e
n
d
s
Local variables are memorized in the stack: it contains
variables with 
automatic storage duration
.
T
h
e
i
r
 
d
u
r
a
t
i
o
n
 
i
n
 
m
e
m
o
r
y
 
b
e
g
i
n
s
 
w
h
e
n
 
t
h
e
i
r
 
d
e
f
i
n
i
t
i
o
n
 
i
s
e
n
c
o
u
n
t
e
r
e
d
 
a
n
d
 
e
n
d
s
 
a
t
 
t
h
e
 
e
n
d
 
o
f
 
t
h
e
i
r
 
s
c
o
p
e
The memorization of objects in the heap area is fully
under the control of the programmer
T
h
e
i
r
 
d
u
r
a
t
i
o
n
 
i
n
 
m
e
m
o
r
y
 
s
t
a
r
t
s
 
w
h
e
n
 
a
 
p
r
o
g
r
a
m
m
e
r
a
l
l
o
c
a
t
e
s
 
i
t
 
(
e
.
g
.
 
m
a
l
l
o
c
(
)
)
,
 
a
n
d
 
e
n
d
s
 
t
h
e
 
a
 
p
r
o
g
r
a
m
m
e
r
d
e
a
l
l
o
c
a
t
e
s
 
i
t
 
(
f
r
e
e
(
)
)
EXAMPLE
int main () {
    int a1[3];
    int * a2 = (int*) malloc(3 * sizeof(int));
}
 
malloc(3 * sizeof(int));
 
a2 == 1000
EXAMPLE
#include <stdio.h>
int size= 5;
void fun(int* b, int n) {
    for (int i = 0; i < n; ++i ) {
      static int c= 0;
      b[i] = ++c;
    }
}
int main(){
    int* a = (int *) malloc(sizeof(int) * size);
    fun(a, size);
    fun(a, size);
}
EXAMPLE (CONT.)
#include <stdio.h>
int size= 5;
void fun(int* b, int n) {
    for (int i = 0; i < n; ++i ) {
      static int c= 0;
      b[i] = ++c;
    }
}
int main(){
    int* a = (int *) malloc(sizeof(int) * size);
    fun(a, size);
    fun(a, size);
    free(a);    // REMEMBER TO FREE ALLOCATED MEMORY
}
 
STACK AND FUNCTIONS
 
CALL STACK
 
I
n
 
c
o
m
p
u
t
e
r
 
s
c
i
e
n
c
e
,
 
a
 
c
a
l
l
 
s
t
a
c
k
 
i
s
 
a
 
s
t
a
c
k
 
d
a
t
a
s
t
r
u
c
t
u
r
e
 
t
h
a
t
 
s
t
o
r
e
s
 
i
n
f
o
r
m
a
t
i
o
n
 
a
b
o
u
t
 
t
h
e
 
a
c
t
i
v
e
s
u
b
r
o
u
t
i
n
e
s
 
o
f
 
a
 
c
o
m
p
u
t
e
r
 
p
r
o
g
r
a
m
.
This kind of stack is also known as an execution stack,
program stack, control stack, run-time stack, or machine
stack, and is often shortened to just "the stack".
Since the call stack is organized as a stack.
 
STACK
I
n
 
c
o
m
p
u
t
e
r
 
s
c
i
e
n
c
e
,
 
a
 
s
t
a
c
k
 
i
s
 
a
n
 
a
b
s
t
r
a
c
t
 
d
a
t
a
 
t
y
p
e
 
t
h
a
t
s
e
r
v
e
s
 
a
s
 
a
 
c
o
l
l
e
c
t
i
o
n
 
o
f
 
e
l
e
m
e
n
t
s
,
 
w
i
t
h
 
t
w
o
 
p
r
i
n
c
i
p
a
l
o
p
e
r
a
t
i
o
n
s
:
 
p
u
s
h
,
 
w
h
i
c
h
 
a
d
d
s
 
a
n
 
e
l
e
m
e
n
t
 
t
o
 
t
h
e
c
o
l
l
e
c
t
i
o
n
,
 
a
n
d
 
p
o
p
,
 
w
h
i
c
h
 
r
e
m
o
v
e
s
 
t
h
e
 
m
o
s
t
 
r
e
c
e
n
t
l
y
a
d
d
e
d
 
e
l
e
m
e
n
t
 
t
h
a
t
 
w
a
s
 
n
o
t
 
y
e
t
 
r
e
m
o
v
e
d
.
T
h
e
 
o
r
d
e
r
 
i
n
 
w
h
i
c
h
 
e
l
e
m
e
n
t
s
 
c
o
m
e
 
o
f
f
 
a
 
s
t
a
c
k
 
g
i
v
e
s
 
r
i
s
e
t
o
 
i
t
s
 
a
l
t
e
r
n
a
t
i
v
e
 
n
a
m
e
,
 
L
I
F
O
 
(
f
o
r
 
l
a
s
t
 
i
n
,
 
f
i
r
s
t
 
o
u
t
)
.
a
 
push
b
 
pop
 
push
c
d
CALL TO FUNCTIONS AND STACK
include<stdio.h>
void f2() {
  int c;
  puts(“bye f2”);
}
void f1() {
  int b= 0;
  f2();
  puts(“bye f1”);
}
int main() {
  int a= 0;
  f1();
  puts(“bye main”);
}
a
b
c
 
bye f2
 
bye f1
 
bye main
 
1000
 
1004
 
1008
EXAMPLE 2
include<stdio.h>
void f1();
void f2() {
  int c;
  f1();
  puts(“bye f2”);
}
void f1() {
  int b= 0;
  f2();
  puts(“bye f1”);
}
int main() {
  int a= 0;
  f1();
  puts(“bye main”);
}
a
b
c
b
c
 
.
.
.
 
MacBook-Francesco:ProgrammI francescosantini$ ./test
Segmentation fault: 11
 
STACK OVERFLOW
 
Also the size of the stack is limited (as the heap)
It cannot grow infinite
In the example before, the stack grows up until it reaches
a zone of memory that it cannot access anymore
Segmentation fault
ALSO MEMORY ADDRESS
int fun(int p1, int p2, int p3) {
    int res= 0;
    res= p1 + p2 + p3;
    return res;
}
int main() {
    int a= 4, b= 5, c= 7; 
    a= fun(a,b,c);
}
The stack grows from bottom to top, heap is different
return
address
res
 
SU LIBRO E RIFERIMENTI
 
Sezione 5.12
Sezione 5.7
 
Altri riferimenti
https://shorturl.at/fknPY
https://profrizzo.altervista.org/memoria-ram-stack-vs-heap-
memoria-statica-vs-memoria-dinamica/
https://www.riochierego.it/docs/10-
Allocazione_dinamica_della_memoria.pdf
Slide Note
Embed
Share

Science Prof Online (SPO) provides fully-developed Virtual Science Classrooms, science-related PowerPoints, articles, and images. Explore practice test questions, review materials, and more. The site is a valuable resource for students, educators, and science enthusiasts. SPO's PowerPoints are available in various formats for easy access and learning. Stay updated by following SPO on social media platforms. Discover engaging content on metabolism, cellular respiration, and everyday biology. Dive into aerobic cellular respiration processes and understand the role of O2.

  • Science Education
  • Virtual Classrooms
  • PowerPoints
  • Metabolism
  • Cellular Respiration

Uploaded on Mar 02, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. PROGRAMMAZIONE PROCEDURALE A.A. 2024/2025

  2. DEFINITION AND DECLARATIONS

  3. DEFINITION AND DECLARATION Defining something means providing all of the necessary information to create that thing in its entirety. Defining a function means providing a function body; Defining a variable means allocating memory for it. Definitions are also declarations Not only for functions but also for variables Most of the time, when you declare a variable, you are also providing the definition. What does it mean to define a variable, exactly? It means you are telling the compiler where to create the storage for that variable.

  4. IN SUMMARY A declaration provides basic attributes of a symbol: its type and its name. A definition provides all of the details of that symbol--if it's a function, what it does; if it's a variable, where that variable is stored. Often, the compiler only needs to have a declaration for something in order to compile a file into an object file, expecting that the linker can find the definition from another file. If no source file ever defines a symbol, but it is declared, you will get errors at link time complaining about undefined symbols. There can be only one definition of the same variable, but many declarations of it (anywhere it is used)

  5. DIFFERENCE A variable declaration says, "there is a variable with the following name and type in the program". A variable definition says, "Dear Mr. Compiler, please allocate memory for a variable with the following name and type now."

  6. EXAMPLES x is defined int x; int main() { x = 3; } int func(); int main() { int x = func(); } int func() { int x= 3; x is defined func is declared x is defined } Of course, since a definition is also a declaration, they are also declared there

  7. STORAGE DURATION

  8. STORAGE CLASS SPECIFIERS A storageclass specifier in a declaration modifies the storage duration of the corresponding objects The storage duration of an object can be automatic, static, or dynamic auto is the storage specifier for automatic, and static is the storage specifier for static duration; dynamic is then using dynamic memory allocation/deallocation

  9. AUTO auto: Objects declared with the auto specifier have automatic storage duration. This specifier is permissible only in object declarations within a function. In ANSI C, objects declared within a function have automatic storage duration by default, and the auto specifier is archaic. int main(void) { int a; { int count; auto int month; } a++; } auto is not used in programs

  10. REGISTER register: You can use the specifier register when declaring objects with automatic storage duration. The register keyword is a hint to the compiler that the object should be made as quickly accessible as possible ideally, by storing it in a CPU register. However, the compiler may treat some or all objects declared with register the same as ordinary objects with automatic storage duration. In any case, programs must not use the address operator on objects declared with the register specifier.

  11. EXAMPLE { register int miles; } { register int miles; int* p= &miles; } variable miles is not in memory. It is in a register

  12. STATIC Static: a variable identifier defined with the specifier static has static storage duration. It is created as soon as the program starts, and destroyed (removed from memory) only when the program ends. Global variables automatically have static storage duration.

  13. EXAMPLE int x= 0; int main() { x = 3; } This is the definition of a global variable (x), which has static storage duration int main() { static int x = 3; } This is the definition of a local variable (x), which has static storage duration, because I used storage specifier static

  14. SCOPE AND STORAGE DURATION There are two separate concepts here: scope, which determines where a name can be accessed, and storage duration, which determines when a variable is created and destroyed. Automatic variables (pedantically, variables with automatic storage duration) are local variables whose lifetime ends when execution leaves their scope, and are recreated when the scope is reentered. Static variables (pedantically, variables with static storage duration) have a lifetime that lasts until the end of the program. If they are local variables, then their value persists when execution leaves their scope.

  15. EXAMPLE 1 Local variables (pedantically, variables with block scope) are only accessible within the block of code in which they are declared: automatic storage duration void f() { int i; i = 1; // OK: in scope } void g() { i = 2; // Error: not in scope } Global variables (pedantically, variables with file scope) are accessible at any point after their declaration: static storage duration int i; void f() { i = 1; // OK: in scope } void g() { i = 2; // OK: still in scope }

  16. EXAMPLE 2 for (int i = 0; i < 5; ++i) { int n = 0; printf("%d ", ++n); } // prints 1 1 1 1 1 - the previous value is lost // n has automatic storage duration // it is created anew for every loop iteration // and destroyed at the end for (int i = 0; i < 5; ++i) { static int n = 0; printf("%d ", ++n); } // prints 1 2 3 4 5 - the value persists // n has static storage duration

  17. EXAMPLE 3 for (int i = 0; i < 5; ++i) { static int n = 0; printf("%d ", ++n); } printf("%d ", n); // error: out of scope Being forever in memory does not mean that a variable is always accessible: it is accessible in its scope

  18. EXAMPLE 4 #include<stdio.h> void func(void); Static storage is the default for variables outside functions (global variables) int count=10; int main() { while (count--) func(); } i is 6 and count is 9 i is 7 and count is 8 i is 8 and count is 7 i is 9 and count is 6 i is 10 and count is 5 i is 11 and count is 4 i is 12 and count is 3 i is 13 and count is 2 i is 14 and count is 1 i is 15 and count is 0 void func( void ) { static int i = 5; i++; printf("i is %d and count is %d\n", i, count); }

  19. EXAMPLE 5 #include<stdio.h> void func(void); int count=10; int main() { while (count--) func(); i++; } i static but not visible in main void func( void ) { static int i = 5; i++; printf("i is %d and count is %d\n", i, count); } MacBook-Francesco:ProgrammI francescosantini$ gcc -std=c99 -o test test.c test.c:11:4: error: use of undeclared identifier 'i' i++; ^ 1 error generated.

  20. EXAMPLE 6 #include<stdio.h> void func(void); int main() { while (count--) func(); } count visible from here int count=10; void func( void ) { static int i = 5; i++; printf("i is %d and count is %d\n", i, count); } MacBook-Francesco:ProgrammI francescosantini$ gcc -std=c99 -o test test.c test.c:8:11: error: use of undeclared identifier 'count' while (count--) ^ 1 error generated.

  21. STATIC STORAGE Global variables, or variables within a function and with the storage class specifier static, have static storage duration. All objects with static storage duration are initialized to 0 automatically. Objects with automatic storage duration are not initialized to 0 by default, their value is undefined It is better to always initialize variables to 0 (especially variables with automatic storage duration) int main() { static int a; } int main() { int a; } // initialized to 0 // not initialized

  22. DIFFERENT KINDS OF MEMORIES

  23. DIFFERENT ZONES IN MEMORY All your variables (and instructions) are in memory

  24. CHARACTERISTICS Permanent area contains both global and static variables (and instructions): variables with static storage duration. Such variables are created when the program starts end destroyed when the program ends Local variables are memorized in the stack: it contains variables with automatic storage duration. Their duration in memory begins when their definition is encountered and ends at the end of their scope The memorization of objects in the heap area is fully under the control of the programmer Their duration in memory starts when a programmer allocates it (e.g. malloc()), and ends the a programmer deallocates it (free())

  25. EXAMPLE malloc(3 * sizeof(int)); int main () { int a1[3]; int * a2 = (int*) malloc(3 * sizeof(int)); } a2[0] a2[1] a2[2] 1012 1004 1008 1000 a2 == 1000

  26. EXAMPLE size c #include <stdio.h> int size= 5; void fun(int* b, int n) { for (int i = 0; i < n; ++i ) { static int c= 0; b[i] = ++c; } } a a[0] a[1] int main(){ int* a = (int *) malloc(sizeof(int) * size); fun(a, size); fun(a, size); } a[2] a[3] a[4]

  27. EXAMPLE (CONT.) #include <stdio.h> int size= 5; void fun(int* b, int n) { for (int i = 0; i < n; ++i ) { static int c= 0; b[i] = ++c; } } a[0] 1 6 a[1] 2 7 a[2] 3 8 a[3] 4 9 a[4] 5 10 a[0] a[1] a[2] a[3] a[4] int main(){ int* a = (int *) malloc(sizeof(int) * size); fun(a, size); fun(a, size); free(a); // REMEMBER TO FREE ALLOCATED MEMORY }

  28. STACK AND FUNCTIONS

  29. CALL STACK In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, program stack, control stack, run-time stack, or machine stack, and is often shortened to just "the stack". Since the call stack is organized as a stack.

  30. STACK In computer science, a stack is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the most recently added element that was not yet removed. The order in which elements come off a stack gives rise to its alternative name, LIFO (for last in, first out). d push pop push b c a

  31. CALL TO FUNCTIONS AND STACK include<stdio.h> bye f2 bye f1 bye main void f2() { int c; puts( bye f2 ); } void f1() { int b= 0; f2(); puts( bye f1 ); } c 1008 b 1004 int main() { int a= 0; f1(); puts( bye main ); } a 1000

  32. EXAMPLE 2 . . . include<stdio.h> void f1(); c void f2() { int c; f1(); puts( bye f2 ); } b c void f1() { int b= 0; f2(); puts( bye f1 ); } b a int main() { int a= 0; f1(); puts( bye main ); } MacBook-Francesco:ProgrammI francescosantini$ ./test Segmentation fault: 11

  33. STACK OVERFLOW Also the size of the stack is limited (as the heap) It cannot grow infinite In the example before, the stack grows up until it reaches a zone of memory that it cannot access anymore Segmentation fault

  34. ALSO MEMORY ADDRESS The stack grows from bottom to top, heap is different int fun(int p1, int p2, int p3) { int res= 0; res= p1 + p2 + p3; return res; } res return address p3 p2 int main() { int a= 4, b= 5, c= 7; a= fun(a,b,c); } p1 c b a

  35. SU LIBRO E RIFERIMENTI Sezione 5.12 Sezione 5.7 Altri riferimenti https://shorturl.at/fknPY https://profrizzo.altervista.org/memoria-ram-stack-vs-heap- memoria-statica-vs-memoria-dinamica/ https://www.riochierego.it/docs/10- Allocazione_dinamica_della_memoria.pdf

Related


More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#