Memory Allocation in C Programming

undefined
 
ק
ו
ר
ס
 
ת
כ
נ
ו
ת
 
ש
י
ע
ו
ר
 
ש
נ
י
ם
-
ע
ש
ר
:
נ
י
ה
ו
ל
 
ז
י
כ
ר
ו
ן
 
1
 
ה
ק
צ
א
ת
 
ז
י
כ
ר
ו
ן
 
ד
י
נ
א
מ
י
ת
 
ע
ד
 
ע
כ
ש
י
ו
 
ע
ש
י
נ
ו
 
"
ה
ק
צ
א
ה
 
ס
ט
ט
י
ת
"
ה
ג
ד
ר
נ
ו
 
א
ת
 
מ
ש
ת
נ
י
 
ה
ת
ו
כ
נ
י
ת
 
כ
ב
ר
 
כ
ש
כ
ת
ב
נ
ו
 
א
ו
ת
ה
ה
ק
ו
מ
פ
י
י
ל
ר
 
ה
ק
צ
ה
 
ע
ב
ו
ר
ם
 
מ
ק
ו
ם
 
ב
ז
י
כ
ר
ו
ן
ב
פ
ר
ט
,
 
ב
ה
ג
ד
ר
ת
 
מ
ע
ר
ך
 
ק
ב
ע
נ
ו
 
א
ת
 
ג
ו
ד
ל
ו
 
ו
ל
א
 
י
כ
ו
ל
נ
ו
ל
ש
נ
ו
ת
 
א
ו
ת
ו
 
ב
ז
מ
ן
 
ה
ר
י
צ
ה
ל
מ
ש
ל
:
 
 
ה
נ
ח
נ
ו
 
ח
ס
ם
 
ע
ל
י
ו
ן
 
ע
ל
 
כ
מ
ו
ת
 
ה
מ
ש
ת
נ
י
ם
 
ה
נ
ד
ר
ש
ת
int
 main()
{
    
int
 a[10];
    ...
};
 
2
 
ט
ע
ו
ת
 
נ
פ
ו
צ
ה
 
נ
י
ס
י
ו
ן
 
ל
ה
ג
ד
י
ר
 
מ
ע
ר
ך
 
ב
ג
ו
ד
ל
 
מ
ש
ת
נ
ה
 
 
 
 
ה
ק
ו
מ
פ
י
י
ל
ר
 
צ
ר
י
ך
 
ל
ד
ע
ת
 
כ
מ
ה
 
ז
י
כ
ר
ו
ן
 
ל
ה
ק
צ
ו
ת
 
ל
כ
ל
מ
ש
ת
נ
ה
 
3
int
 main()
{
    ...
    printf("enter number of elements\n");
    scanf("%d", &size);
    
int
 a[size];
    ...
};
 
×
 
ה
ק
צ
א
ת
 
ז
י
כ
ר
ו
ן
 
ד
י
נ
א
מ
י
ת
 
ל
א
 
ת
מ
י
ד
 
כ
ל
 
ה
מ
י
ד
ע
 
י
ד
ו
ע
 
ב
ז
מ
ן
 
כ
ת
י
ב
ת
 
ה
ת
כ
נ
י
ת
ל
ד
ו
ג
מ
א
,
 
ת
ל
ו
י
 
ב
ק
ל
ט
 
מ
ה
מ
ש
ת
מ
ש
 
נ
י
ת
ן
 
ל
ה
ק
צ
ו
ת
 
ז
י
כ
ר
ו
ן
 
ב
מ
ה
ל
ך
 
ר
י
צ
ת
 
ה
ת
כ
נ
י
ת
ה
ז
י
כ
ר
ו
ן
 
י
ו
ק
צ
ה
 
ב
א
ז
ו
ר
 
ה
"
ע
ר
י
מ
ה
"
 
(
h
e
a
p
)
ל
א
 
נ
ע
ל
ם
 
כ
א
ש
ר
 
ה
פ
ו
נ
ק
צ
י
ה
 
מ
ס
ת
י
י
מ
ת
ה
ק
צ
א
ה
 
ו
ש
ח
ר
ו
ר
 
ב
א
ח
ר
י
ו
ת
 
ה
מ
ת
כ
נ
ת
 
4
 
מ
ו
ד
ל
 
ה
ז
י
כ
ר
ו
ן
 
ש
ל
 
ה
ת
כ
נ
י
ת
 
ה
ק
ו
ד
פ
ק
ו
ד
ו
ת
 
ה
ת
כ
נ
י
ת
D
a
t
a
מ
ח
ר
ו
ז
ו
ת
 
ש
ה
ו
ג
ד
ר
ו
 
ע
"
י
 
ה
מ
ת
כ
נ
ת
מ
ח
ס
נ
י
ת
 
(
S
t
a
c
k
)
מ
ש
מ
ש
 
ל
א
י
ח
ס
ו
ן
 
מ
ש
ת
נ
י
ם
 
ל
ו
ק
א
ל
י
י
ם
L
a
s
t
 
I
n
 
F
i
r
s
t
 
O
u
t
 
(
L
I
F
O
)
ב
ש
ל
י
ט
ת
 
ה
ת
כ
נ
י
ת
ע
ר
י
מ
ה
 
(
H
e
a
p
)
ב
ש
ל
י
ט
ת
 
ה
מ
ת
כ
נ
ת
ת
 
(
ה
י
ו
ם
!
)
 
ק
ר
י
א
ה
ב
ל
ב
ד
 
ק
ר
י
א
ה
ו
כ
ת
י
ב
ה
 
5
ק
ו
ד
d
a
t
a
מ
ח
ס
נ
י
ת
(
s
t
a
c
k
)
ע
ר
י
מ
ה
(
h
e
a
p
)
 
ה
ק
צ
א
ה
 
ד
י
נ
א
מ
י
ת
 
ב
ש
פ
ת
 
C
 
C
 
מ
ס
פ
ק
ת
 
ל
נ
ו
 
מ
נ
ג
נ
ו
ן
 
ב
ס
י
ס
י
 
ל
ה
ק
צ
א
ה
 
ו
ש
ח
ר
ו
ר
 
ש
ל
ז
י
כ
ר
ו
ן
 
 
 
 
 
מ
ו
ג
ד
ר
ו
ת
 
ב
 
s
t
d
l
i
b
.
h
 
6
 
ה
פ
ו
נ
ק
צ
י
ה
 
m
a
l
l
o
c
 
 
 
מ
ק
ב
ל
ת
 
א
ת
 
ג
ו
ד
ל
 
ה
ז
י
כ
ר
ו
ן
 
ש
נ
ר
צ
ה
 
ל
ה
ק
צ
ו
ת
 
(
ב
ב
ת
י
ם
)
מ
ח
ז
י
ר
ה
 
א
ת
 
ה
כ
ת
ו
ב
ת
 
ל
ז
י
כ
ר
ו
ן
 
ש
ה
ו
ק
צ
ה
N
U
L
L
 
א
ם
 
נ
כ
ש
ל
ה
ת
מ
י
ד
 
י
ש
 
ל
ב
ד
ו
ק
 
א
ם
 
ה
ק
צ
א
ת
 
ה
ז
י
כ
ר
ו
ן
 
ה
צ
ל
י
ח
ה
 
א
ו
נ
כ
ש
ל
ה
void
 *malloc(
int
 nBytes)
 
7
 
m
a
l
l
o
c
 
ד
ו
ג
מ
א
 
ה
ק
צ
א
ה
 
ד
י
נ
א
מ
י
ת
 
ש
ל
 
ז
י
כ
ר
ו
ן
 
ב
ג
ו
ד
ל
 
1
0
 
i
n
t
 
 
 
 
ה
א
ו
פ
ר
ט
ו
ר
 
s
i
z
e
o
f
 
מ
ח
ז
י
ר
 
א
ת
 
ה
ג
ו
ד
ל
 
(
ב
ב
ת
י
ם
)
 
ש
ל
 
ה
ט
י
פ
ו
ס
ה
מ
ב
ו
ק
ש
ה
פ
ו
נ
ק
צ
י
ה
 
מ
ח
ז
י
ר
ה
 
כ
ת
ו
ב
ת
 
(
v
o
i
d
*
)
 
ב
א
ח
ר
י
ו
ת
נ
ו
 
ל
ה
מ
י
ר
 
א
ת
ה
מ
צ
ב
י
ע
 
ל
ט
י
פ
ו
ס
 
ה
מ
ת
א
י
ם
int 
main()
{
    
int
 *a = (
int
*) malloc(10 * 
sizeof
(int));
    ...
    
return
 0;
}
 
8
 
ש
י
מ
ו
ש
 
ב
 
m
a
l
l
o
c
 
 
 
 
 
 
ה
ע
ר
ך
 
ה
מ
ו
ח
ז
ר
 
ה
ו
א
 
כ
ת
ו
ב
ת
 
כ
ל
ש
ה
י
ל
ל
א
 
צ
י
ו
ן
 
ה
ט
י
פ
ו
ס
כ
ד
י
 
ל
ה
ש
ת
מ
ש
 
ב
מ
צ
ב
י
ע
 
י
ש
 
ל
ב
צ
ע
 
ה
מ
ר
ה
 
(
c
a
s
t
i
n
g
)
type *var-name = (type*) malloc(amount * 
sizeof
(type));
מ
צ
ב
י
ע
 
ל
ת
ח
י
ל
ת
ה
ז
י
כ
ר
ו
ן
 
ה
מ
ו
ק
צ
ה
ה
מ
ר
ת
 
ה
ע
ר
ך
 
ה
מ
ו
ח
ז
ר
ל
ט
י
פ
ו
ס
 
ה
מ
ת
א
י
ם
ג
ו
ד
ל
 
ה
ט
י
פ
ו
ס
ב
ב
ת
י
ם
 
9
 
ה
פ
ו
נ
ק
צ
י
ה
 
f
r
e
e
 
 
ש
י
ח
ר
ו
ר
 
ז
י
כ
ר
ו
ן
 
 
ב
א
ח
ר
י
ו
ת
 
ה
מ
ת
כ
נ
ת
 
ל
ש
ח
ר
ר
 
ז
י
כ
ר
ו
ן
 
ש
ה
ו
ק
צ
ה
ד
י
נ
א
מ
י
ת
p
t
r
-
 
מ
צ
ב
י
ע
 
ל
ז
י
כ
ר
ו
ן
 
ש
ה
ו
ק
צ
ה
 
ד
י
נ
א
מ
י
ת
p
t
r
-
 
ע
ר
ך
 
מ
ו
ח
ז
ר
 
מ
ק
ר
י
א
ה
 
ל
-
m
a
l
l
o
c
,
 
c
a
l
l
o
c
 
א
ו
 
r
e
a
l
l
o
c
 
א
ח
ר
ת
ש
ג
י
א
ה
א
י
ן
 
ש
י
ח
ר
ו
ר
 
ח
ל
ק
י
 
ש
ל
 
ז
י
כ
ר
ו
ן
void
 free(
void
 *ptr)
 
10
10
 
ה
ק
צ
א
ת
 
מ
ע
ר
ך
 
ב
ג
ו
ד
ל
 
מ
ש
ת
נ
ה
 
11
11
int 
main()
{
    int 
*a, size, i;
 
    printf("Enter array size\n");
    scanf("%d", &size);
 
    a = (
int
*)
 
malloc(size * 
sizeof
(
int
));
    
if 
(a == NULL)
        return 1;
 
    
for
 (i = 0; i < size; i++)
        scanf("%d", &a[i]);
 
    
for 
(i = 0; i < size; i++)
        printf("%d", a[i]);
 
    free(a);
 
    return 
0;
}
ק
ר
א
 
ג
ו
ד
ל
 
מ
ע
ר
ך
 
מ
ה
מ
ש
ת
מ
ש
ה
ק
צ
א
ת
 
ז
י
כ
ר
ו
ן
 
ב
ג
ו
ד
ל
 
ה
מ
ב
ו
ק
ש
ב
ד
י
ק
ה
 
ה
א
ם
 
ה
ה
ק
צ
א
ה
 
ה
צ
ל
י
ח
ה
ק
ל
ט
 
מ
ה
מ
ש
ת
מ
ש
ה
ד
פ
ס
ת
 
ת
ו
כ
ן
 
ה
מ
ע
ר
ך
ש
י
ח
ר
ו
ר
 
ה
ז
י
כ
ר
ו
ן
 
ה
ק
צ
א
ת
 
מ
ע
ר
ך
 
ב
ג
ו
ד
ל
 
מ
ש
ת
נ
ה
 
12
12
int 
main()
{
    int 
*a, size, i;
 
    printf("Enter array size\n");
    scanf("%d", &size);
 
    a = (
int
*)
 
malloc(size * 
sizeof
(
int
));
    
if 
(a == NULL)
        return 1;
 
    
for
 (i = 0; i < size; i++)
        scanf("%d", &a[i]);
 
    
for 
(i = 0; i < size; i++)
        printf("%d", a[i]);
 
    free(a);
 
    return 
0;
}
m
a
i
n
a
s
i
z
e
i
S
t
a
c
k
H
e
a
p
 
פ
ו
נ
ק
צ
י
ו
ת
 
נ
ו
ס
פ
ו
ת
 
 
 
מ
ק
צ
ה
 
מ
ע
ר
ך
 
ש
ל
 
n
 
א
י
ב
ר
י
ם
,
 
כ
ל
 
א
י
ב
ר
 
ב
ג
ו
ד
ל
 
s
i
z
e
_
e
l
 
ב
ת
י
ם
,
 
כ
ל
 
ב
י
ת
מ
א
ו
ת
ח
ל
 
ל
א
פ
ס
 
(
ו
ל
כ
ן
 
פ
ח
ו
ת
 
י
ע
י
ל
ה
)
.
 
 
מ
ק
ב
ל
ת
 
מ
צ
ב
י
ע
 
ל
ז
י
כ
ר
ו
ן
 
ש
ה
ו
ק
צ
ה
 
ד
י
נ
א
מ
י
ת
 
ו
מ
ס
פ
ר
 
ב
ת
י
ם
 
s
i
z
e
ה
פ
ו
נ
ק
צ
י
ה
 
מ
ק
צ
ה
 
ז
י
כ
ר
ו
ן
 
ב
ה
ת
א
ם
 
ל
ג
ו
ד
ל
 
ה
נ
ד
ר
ש
מ
ע
ת
י
ק
ה
 
א
ת
 
ת
כ
ו
ל
ת
 
ה
ז
י
כ
ר
ו
ן
 
ה
י
ש
ן
 
ל
ח
ד
ש
מ
ש
ח
ר
ר
ת
 
א
ת
 
ה
ז
י
כ
ר
ו
ן
 
ה
י
ש
ן
ב
ש
ת
י
 
ה
פ
ו
נ
ק
צ
י
ו
ת
 
ק
ר
י
א
ה
 
מ
ו
צ
ל
ח
ת
 
ת
ח
ז
י
ר
 
א
ת
 
כ
ת
ו
ב
ת
 
ת
ח
י
ל
ת
 
ה
ז
י
כ
ר
ו
ן
ה
מ
ו
ק
צ
ה
,
 
א
ח
ר
ת
 
י
ו
ח
ז
ר
 
N
U
L
L
void
* calloc( 
unsigned
 
int
 n, 
unsigned
 
int
 size_el )
void
* realloc( 
void 
*ptr, 
unsigned
 
int
 size )
 
13
13
 
c
a
l
l
o
c
 
-
 
ד
ו
ג
מ
א
 
14
14
int 
main()
{
    int 
*a, size, i;
 
    printf("Enter array size\n");
    scanf("%d", &size);
 
    a = (
int
*)
 
calloc(size, 
sizeof
(
int
));
    
if 
(a == NULL)
        return 1;
 
    
for
 (i = 0; i < size; i++)
        scanf("%d", &a[i]);
 
    
for 
(i = 0; i < size; i++)
        printf("%d", a[i]);
 
    free(a);
 
    return 
0;
}
ה
ק
צ
א
ת
 
s
i
z
e
 
א
ל
מ
נ
ט
י
ם
,
 
כ
ל
 
א
ל
מ
נ
ט
ב
ג
ו
ד
ל
 
s
i
z
e
o
f
(
i
n
t
)
ה
פ
ו
נ
ק
צ
י
ה
 
ת
א
ת
ח
ל
 
א
ת
 
ה
ז
י
כ
ר
ו
ן
 
ל
-
0
 
r
e
a
l
l
o
c
 
-
 
ד
ו
ג
מ
א
 
int
 *ptr = NULL;
int
 size = 0, new_size = 0;
 
scanf("%d", &size);
ptr = (
int
*) malloc( size * 
sizeof
(
int
) );
/* incomplete, must check if allocation succeeded */
...
 
scanf("%d", &new_size);
ptr = (
int
*) realloc( ptr, new_size*
sizeof
(
int
) );
/* incomplete, must check if allocation succeeded */
 
...
 
if
 (ptr != NULL)
 
free(ptr);
ה
ק
צ
א
ת
 
ז
י
כ
ר
ו
ן
ה
ק
צ
א
ת
 
ח
ד
ש
ה
ש
ח
ר
ו
ר
 
ז
י
כ
ר
ו
ן
 
15
15
ז
י
כ
ר
ו
ן
 
ש
ה
ו
ק
צ
ה
 
ד
י
נ
א
מ
י
ת
16
16
int
* func()
{
    
int
 *memPtr = NULL;
    memPtr = (
int
*) malloc(10 * 
sizeof
(
int
));
    ...
    
return
 memPtr;
}
int
 main()
{
    int
 * ptr = func();
    
if
 (ptr != NULL)
    {
        
// do something with ptr
        free(ptr);
        ptr = NULL;
    }
}
m
e
m
P
t
r
p
t
r
מ
ו
ת
ר
 
ל
ה
ח
ז
י
ר
 
כ
ת
ו
ב
ת
ל
ז
י
כ
ר
ו
ן
 
ש
ה
ו
ק
צ
ה
 
ד
י
נ
א
מ
י
ת
ה
ז
י
כ
ר
ו
ן
 
ש
י
י
ך
 
ל
ת
ו
כ
נ
י
ת
ו
ל
א
 
ל
פ
ו
נ
ק
צ
י
ה
 
ש
י
ח
ר
ו
ר
 
ז
י
כ
ר
ו
ן
 
א
פ
ש
ר
ו
ת
 
א
'
:
 
ה
פ
ו
נ
ק
צ
י
ה
 
ש
ה
ק
צ
ת
ה
ה
א
פ
ש
ר
ו
ת
 
ה
מ
ו
ע
ד
פ
ת
 
 
כ
ו
ת
ב
 
ה
פ
ו
נ
ק
צ
י
ה
 
א
ח
ר
א
י
 
ע
ל
 
ה
ז
י
כ
ר
ו
ן
ה
ד
י
נ
א
מ
י
 
א
פ
ש
ר
ו
ת
 
ב
'
:
 
ה
ח
ז
ר
ת
 
ה
כ
ת
ו
ב
ת
ה
א
ח
ר
י
ו
ת
 
ע
ו
ב
ר
ת
 
ל
מ
י
 
ש
ק
ר
א
 
ל
פ
ו
נ
ק
צ
י
ה
 
ה
מ
ק
צ
ה
ח
ו
ב
ה
 
ל
ת
ע
ד
 
ז
א
ת
,
 
א
ח
ר
ת
 
"
ד
ל
י
פ
ת
 
ז
י
כ
ר
ו
ן
"
 
ע
ל
י
נ
ו
 
ל
ה
י
מ
נ
ע
 
מ
מ
צ
ב
 
ש
ב
ו
 
י
ש
 
ה
ק
צ
א
ת
 
ז
י
כ
ר
ו
ן
 
א
ב
ל
ל
א
 
מ
ת
ק
י
י
ם
 
א
'
 
א
ו
 
ב
'
 
17
17
 
ק
ר
י
א
ת
 
ש
ו
ר
ת
 
ק
ל
ט
 
ב
ג
ו
ד
ל
 
ל
א
 
י
ד
ו
ע
 
18
18
char
* readLine()
{
   
int
 index = 0, c, capacity = INITIAL_SIZE;
   
char
 *buffer = (
char
*) malloc(capacity * 
sizeof
(
char
));
 
   
if
 (buffer == NULL)
      
return
 NULL;
 
   
for
 (c = getchar(); c != 
'\n'
; c = getchar())
   {
      
if
 (index == capacity – 1)
      {
         buffer = (char*) realloc(buffer, capacity * INCREMENT * 
sizeof
(
char
));
         
if
 (buffer == NULL)
            
return
 NULL;
         capacity = capacity * INCREMENT * sizeof(char);
      }
      buffer[index++] = c;
   }
   buffer[index] = 
'\0'
;
   
return
 buffer;
}
 
ק
ר
י
א
ת
 
ש
ו
ר
ת
 
ק
ל
ט
 
ב
ג
ו
ד
ל
 
ל
א
 
י
ד
ו
ע
 
19
19
char
* readLine()
{
   
int
 index = 0, c, capacity = INITIAL_SIZE;
   
char
 *buffer = (
char
*) malloc(capacity * 
sizeof
(
char
));
 
   
if
 (buffer == NULL)
      
return
 NULL;
 
   
for
 (c = getchar(); c != 
'\n'
; c = getchar())
   {
      
if
 (index == capacity – 1)
      {
         buffer = (char*) realloc(buffer, capacity * INCREMENT * 
sizeof
(
char
));
         
if
 (buffer == NULL)
            
return
 NULL;
         capacity = capacity * INCREMENT * sizeof(char);
      }
      buffer[index++] = c;
   }
   buffer[index] = 
'\0'
;
   
return
 buffer;
}
מ
ע
ר
ך
 
ב
ג
ו
ד
ל
 
ה
ת
ח
ל
ת
י
 
כ
ל
ש
ה
ו
 
ק
ר
י
א
ת
 
ש
ו
ר
ת
 
ק
ל
ט
 
ב
ג
ו
ד
ל
 
ל
א
 
י
ד
ו
ע
 
20
20
char
* readLine()
{
   
int
 index = 0, c, capacity = INITIAL_SIZE;
   
char
 *buffer = (
char
*) malloc(capacity * 
sizeof
(
char
));
 
   
if
 (buffer == NULL)
      
return
 NULL;
 
   
for
 (c = getchar(); c != 
'\n'
; c = getchar())
   {
      
if
 (index == capacity – 1)
      {
         buffer = (char*) realloc(buffer, capacity * INCREMENT * 
sizeof
(
char
));
         
if
 (buffer == NULL)
            
return
 NULL;
         capacity = capacity * INCREMENT * sizeof(char);
      }
      buffer[index++] = c;
   }
   buffer[index] = 
'\0'
;
   
return
 buffer;
}
י
ש
 
ל
ו
ו
ד
א
 
ש
ה
ק
צ
א
ת
 
ה
ז
י
כ
ר
ו
ן
 
ל
א
 
נ
כ
ש
ל
ה
 
ק
ר
י
א
ת
 
ש
ו
ר
ת
 
ק
ל
ט
 
ב
ג
ו
ד
ל
 
ל
א
 
י
ד
ו
ע
 
21
21
char
* readLine()
{
   
int
 index = 0, c, capacity = INITIAL_SIZE;
   
char
 *buffer = (
char
*) malloc(capacity * 
sizeof
(
char
));
 
   
if
 (buffer == NULL)
      
return
 NULL;
 
   
for
 (c = getchar(); c != 
'\n'
; c = getchar())
   {
      
if
 (index == capacity – 1)
      {
         buffer = (char*) realloc(buffer, capacity * INCREMENT * 
sizeof
(
char
));
         
if
 (buffer == NULL)
            
return
 NULL;
         capacity = capacity * INCREMENT * sizeof(char);
      }
      buffer[index++] = c;
   }
   buffer[index] = 
'\0'
;
   
return
 buffer;
}
ק
ר
י
א
ת
 
ק
ל
ט
 
מ
ה
מ
ש
ת
מ
ש
 
ע
ד
 
ל
ס
ו
ף
 
ה
ש
ו
ר
ה
 
ק
ר
י
א
ת
 
ש
ו
ר
ת
 
ק
ל
ט
 
ב
ג
ו
ד
ל
 
ל
א
 
י
ד
ו
ע
 
22
22
char
* readLine()
{
   
int
 index = 0, c, capacity = INITIAL_SIZE;
   
char
 *buffer = (
char
*) malloc(capacity * 
sizeof
(
char
));
 
   
if
 (buffer == NULL)
      
return
 NULL;
 
   
for
 (c = getchar(); c != 
'\n'
; c = getchar())
   {
      
if
 (index == capacity – 1)
      {
         buffer = (char*) realloc(buffer, capacity * INCREMENT * 
sizeof
(
char
));
         
if
 (buffer == NULL)
            
return
 NULL;
         capacity = capacity * INCREMENT * sizeof(char);
      }
      buffer[index++] = c;
   }
   buffer[index] = 
'\0'
;
   
return
 buffer;
}
ה
א
ם
 
נ
ג
מ
ר
 
ה
מ
ק
ו
ם
?
 
ק
ר
י
א
ת
 
ש
ו
ר
ת
 
ק
ל
ט
 
ב
ג
ו
ד
ל
 
ל
א
 
י
ד
ו
ע
 
23
23
char
* readLine()
{
   
int
 index = 0, c, capacity = INITIAL_SIZE;
   
char
 *buffer = (
char
*) malloc(capacity * 
sizeof
(
char
));
 
   
if
 (buffer == NULL)
      
return
 NULL;
 
   
for
 (c = getchar(); c != 
'\n'
; c = getchar())
   {
      
if
 (index == capacity – 1)
      {
         buffer = (char*) realloc(buffer, capacity * INCREMENT * 
sizeof
(
char
));
         
if
 (buffer == NULL)
            
return
 NULL;
         capacity = capacity * INCREMENT * sizeof(char);
      }
      buffer[index++] = c;
   }
   buffer[index] = 
'\0'
;
   
return
 buffer;
}
נ
ק
צ
ה
 
מ
ע
ר
ך
 
ג
ד
ו
ל
 
י
ו
ת
ר
 
ק
ר
י
א
ת
 
ש
ו
ר
ת
 
ק
ל
ט
 
ב
ג
ו
ד
ל
 
ל
א
 
י
ד
ו
ע
 
24
24
char
* readLine()
{
   
int
 index = 0, c, capacity = INITIAL_SIZE;
   
char
 *buffer = (
char
*) malloc(capacity * 
sizeof
(
char
));
 
   
if
 (buffer == NULL)
      
return
 NULL;
 
   
for
 (c = getchar(); c != 
'\n'
; c = getchar())
   {
      
if
 (index == capacity – 1)
      {
         buffer = (char*) realloc(buffer, capacity * INCREMENT * 
sizeof
(
char
));
         
if
 (buffer == NULL)
            
return
 NULL;
         capacity = capacity * INCREMENT * sizeof(char);
      }
      buffer[index++] = c;
   }
   buffer[index] = 
'\0'
;
   
return
 buffer;
}
ל
א
 
ל
ש
כ
ו
ח
 
ל
ב
ד
ו
ק
 
ק
ר
י
א
ת
 
ש
ו
ר
ת
 
ק
ל
ט
 
ב
ג
ו
ד
ל
 
ל
א
 
י
ד
ו
ע
 
25
25
char
* readLine()
{
   
int
 index = 0, c, capacity = INITIAL_SIZE;
   
char
 *buffer = (
char
*) malloc(capacity * 
sizeof
(
char
));
 
   
if
 (buffer == NULL)
      
return
 NULL;
 
   
for
 (c = getchar(); c != 
'\n'
; c = getchar())
   {
      
if
 (index == capacity – 1)
      {
         buffer = (char*) realloc(buffer, capacity * INCREMENT * 
sizeof
(
char
));
         
if
 (buffer == NULL)
            
return
 NULL;
         capacity = capacity * INCREMENT * sizeof(char);
      }
      buffer[index++] = c;
   }
   buffer[index] = 
'\0'
;
   
return
 buffer;
}
ל
א
 
ל
ש
כ
ו
ח
 
ל
ע
ד
כ
ן
 
א
ת
 
c
a
p
a
c
i
t
y
 
ק
ר
י
א
ת
 
ש
ו
ר
ת
 
ק
ל
ט
 
ב
ג
ו
ד
ל
 
ל
א
 
י
ד
ו
ע
 
26
26
char
* readLine()
{
   
int
 index = 0, c, capacity = INITIAL_SIZE;
   
char
 *buffer = (
char
*) malloc(capacity * 
sizeof
(
char
));
 
   
if
 (buffer == NULL)
      
return
 NULL;
 
   
for
 (c = getchar(); c != 
'\n'
; c = getchar())
   {
      
if
 (index == capacity – 1)
      {
         buffer = (char*) realloc(buffer, capacity * INCREMENT * 
sizeof
(
char
));
         
if
 (buffer == NULL)
            
return
 NULL;
         capacity = capacity * INCREMENT * sizeof(char);
      }
      buffer[index++] = c;
   }
   buffer[index] = 
'\0'
;
   
return
 buffer;
}
נ
ש
מ
ו
ר
 
א
ת
 
ה
ת
ו
 
ש
ק
ר
א
נ
ו
 
ב
מ
ק
ו
ם
 
ה
מ
ת
א
י
ם
 
ק
ר
י
א
ת
 
ש
ו
ר
ת
 
ק
ל
ט
 
ב
ג
ו
ד
ל
 
ל
א
 
י
ד
ו
ע
 
27
27
char
* readLine()
{
   
int
 index = 0, c, capacity = INITIAL_SIZE;
   
char
 *buffer = (
char
*) malloc(capacity * 
sizeof
(
char
));
 
   
if
 (buffer == NULL)
      
return
 NULL;
 
   
for
 (c = getchar(); c != 
'\n'
; c = getchar())
   {
      
if
 (index == capacity – 1)
      {
         buffer = (char*) realloc(buffer, capacity * INCREMENT * 
sizeof
(
char
));
         
if
 (buffer == NULL)
            
return
 NULL;
         capacity = capacity * INCREMENT * sizeof(char);
      }
      buffer[index++] = c;
   }
   buffer[index] = 
'\0'
;
   
return
 buffer;
}
ב
ס
ו
ף
 
נ
ו
ס
י
ף
 
\
0
 
ק
ר
י
א
ת
 
ש
ו
ר
ת
 
ק
ל
ט
 
ב
ג
ו
ד
ל
 
ל
א
 
י
ד
ו
ע
 
28
28
char
* readLine()
{
   
int
 index = 0, c, capacity = INITIAL_SIZE;
   
char
 *buffer = (
char
*) malloc(capacity * 
sizeof
(
char
));
 
   
if
 (buffer == NULL)
      
return
 NULL;
 
   
for
 (c = getchar(); c != 
'\n'
; c = getchar())
   {
      
if
 (index == capacity – 1)
      {
         buffer = (char*) realloc(buffer, capacity * INCREMENT * 
sizeof
(
char
));
         
if
 (buffer == NULL)
            
return
 NULL;
         capacity = capacity * INCREMENT * sizeof(char);
      }
      buffer[index++] = c;
   }
   buffer[index] = 
'\0'
;
   
return
 buffer;
}
מ
ח
ז
י
ר
י
ם
 
ז
י
כ
ר
ו
ן
 
ש
ה
ו
ק
צ
ה
 
ד
י
נ
א
מ
י
ת
 
ב
א
ח
ר
י
ו
ת
 
ה
ל
ק
ו
ח
 
ל
ש
ח
ר
ר
 
(
צ
ר
י
ך
 
ל
ת
ע
ד
)
 
ש
י
מ
ו
ש
 
ב
 
r
e
a
d
L
i
n
e
 
29
29
int
 main()
{
    
char
 *line = readLine();
 
    
if
 (line == NULL)
    {
        printf(
"Fatal error: memory allocation failed!\n"
);
        
return
 1;
    }
    printf(
"%s\n"
, line);
 
    free(line);
 
    
return
 0;
}
ק
ר
י
א
ת
 
ש
ו
ר
ת
 
ק
ל
ט
 
מ
ה
מ
ש
ת
מ
ש
 
ש
י
מ
ו
ש
 
ב
 
r
e
a
d
L
i
n
e
 
30
30
int
 main()
{
    
char
 *line = readLine();
 
    
if
 (line == NULL)
    {
        printf(
"Fatal error: memory allocation failed!\n"
);
        
return
 1;
    }
    printf(
"%s\n"
, line);
 
    free(line);
 
    
return
 0;
}
א
ם
 
ה
י
י
ת
ה
 
ב
ע
י
ה
 
נ
ד
פ
י
ס
 
ה
ו
ד
ע
ת
 
ש
ג
י
א
ה
ו
נ
ס
י
י
ם
 
ש
י
מ
ו
ש
 
ב
 
r
e
a
d
L
i
n
e
 
31
31
int
 main()
{
    
char
 *line = readLine();
 
    
if
 (line == NULL)
    {
        printf(
"Fatal error: memory allocation failed!\n"
);
        
return
 1;
    }
    printf(
"%s\n"
, line);
 
    free(line);
 
    
return
 0;
}
ש
י
מ
ו
 
ל
ב
 
ל
ע
ר
ך
 
ה
מ
ו
ח
ז
ר
 
ש
י
מ
ו
ש
 
ב
 
r
e
a
d
L
i
n
e
 
32
32
int
 main()
{
    
char
 *line = readLine();
 
    
if
 (line == NULL)
    {
        printf(
"Fatal error: memory allocation failed!\n"
);
        
return
 1;
    }
    printf(
"%s\n"
, line);
 
    free(line);
 
    
return
 0;
}
נ
ש
ת
מ
ש
 
ב
ק
ל
ט
 
ש
י
מ
ו
ש
 
ב
 
r
e
a
d
L
i
n
e
 
33
33
int
 main()
{
    
char
 *line = readLine();
 
    
if
 (line == NULL)
    {
        printf(
"Fatal error: memory allocation failed!\n"
);
        
return
 1;
    }
    printf(
"%s\n"
, line);
 
    free(line);
 
    
return
 0;
}
כ
ש
א
י
נ
נ
ו
 
צ
ר
י
כ
י
ם
 
י
ו
ת
ר
 
א
ת
 
ה
ק
ל
ט
,
 
נ
ש
ח
ר
ר
א
ת
 
ה
ז
י
כ
ר
ו
ן
 
ה
ק
צ
א
ת
 
ז
י
כ
ר
ו
ן
 
-
 
ב
ע
י
ו
ת
 
נ
ד
י
ר
 
ש
ה
ק
צ
א
ת
 
ה
ז
י
כ
ר
ו
ן
 
נ
כ
ש
ל
ת
א
פ
ש
ר
ו
י
ו
ת
 
ל
ט
י
פ
ו
ל
:
ה
ח
ז
ר
ת
 
ה
ע
ר
ך
 
N
U
L
L
 
מ
ה
פ
ו
נ
ק
צ
י
ה
 
-
 
כ
א
ש
ר
ת
פ
ק
י
ד
 
ה
פ
ו
נ
ק
צ
י
ה
 
ה
ו
א
 
ה
ק
צ
א
ת
 
ה
ז
י
כ
ר
ו
ן
ש
י
מ
ו
ש
 
ב
פ
ו
נ
ק
צ
י
ה
 
e
x
i
t
 
ל
ס
י
ו
ם
 
ה
ת
כ
נ
י
ת
 
ה
פ
ו
נ
ק
צ
י
ה
 
e
x
i
t
 
 
מ
ו
ג
ד
ר
ת
 
ב
 
s
t
d
l
i
b
.
h
מ
ס
י
י
מ
ת
 
א
ת
 
ה
ת
כ
נ
י
ת
 
ב
א
ו
פ
ן
 
מ
י
י
ד
י
נ
ש
ת
מ
ש
 
ר
ק
 
כ
א
ש
ר
 
מ
ת
ר
ח
ש
ת
 
ב
ע
י
ה
 
ש
א
י
ן
 
ל
נ
ו
 
ש
ו
ם
 
ד
ר
ך
ל
ט
פ
ל
 
ב
ה
 
ב
ד
ר
ך
 
כ
ל
ל
 
נ
ד
פ
י
ס
 
ה
ו
ד
ע
ת
 
ש
ג
י
א
ה
 
ל
מ
ש
ת
מ
ש
 
ו
נ
ס
י
י
ם
א
ת
 
ת
כ
נ
י
ת
void exit(int 
status
);
 
ד
ו
ג
מ
ה
 
ל
ש
י
מ
ו
ש
 
ב
-
e
x
i
t
 
נ
ש
נ
ה
 
א
ת
 
ג
ו
ד
ל
ו
 
ש
ל
 
מ
ע
ר
ך
 
ש
ל
 
i
n
t
 
ב
ע
ז
ר
ת
 
r
e
a
l
l
o
c
א
ם
 
ה
ש
י
נ
ו
י
 
נ
כ
ש
ל
,
 
נ
ס
י
י
ם
 
א
ת
 
ה
ת
כ
נ
י
ת
 
36
36
void
 resize(
int
 *array, 
int 
newSize)
{
    
if
 (array == NULL)
        
return
;
 
    array = (
int
*) realloc(array, newSize * 
sizeof
(
int
));
    
if
 (array == NULL)
    {
        printf(
"Fatal error: memory allocation failed!\n"
);
        
exit
(1);
    }
}
 
ה
ק
צ
א
ה
 
ד
י
נ
א
מ
י
ת
 
ש
ל
 
מ
ב
נ
י
ם
 
ב
ד
י
ו
ק
 
כ
מ
ו
 
ב
ט
י
פ
ו
ס
י
ם
 
ב
ס
י
ס
י
י
ם
 
37
37
typedef struct 
{
    
char 
*title, *author;
} Book;
 
Book* newBook(
const char
 *title, 
const char 
*author)
{
    Book *result = (Book*) malloc(
sizeof
(Book));
    
/* incomplete, must check if allocation succeeded */
 
    result->title = strdup(title);
    result->author = strdup(author);
    
/* incomplete, must check if allocation succeeded */
 
    
return
 result;
}
 
מ
ש
ת
נ
י
ם
 
ב
ש
ו
ר
ת
 
ה
פ
ק
ו
ד
ה
 
38
38
 
C
o
m
m
a
n
d
 
L
i
n
e
 
A
r
g
u
m
e
n
t
s
 
ב
ע
ת
 
ה
ר
צ
ת
 
ה
ת
ו
כ
נ
י
ת
 
נ
י
ת
ן
 
ל
ה
ע
ב
י
ר
 
ל
ה
 
מ
ש
ת
נ
י
ם
ב
ש
ו
ר
ת
 
ה
פ
ק
ו
ד
ה
מ
ש
ת
נ
י
ם
 
א
ל
ו
 
י
ו
ע
ב
ר
ו
 
כ
א
ר
ג
ו
מ
נ
ט
י
ם
 
ל
-
m
a
i
n
כ
ל
 
ה
מ
ש
ת
נ
י
ם
 
ה
ם
 
מ
ח
ר
ו
ז
ו
ת
 
39
39
int 
main(
int 
argc, 
char 
*argv[])
{
 
}
מ
ס
פ
ר
 
ה
מ
ש
ת
נ
י
ם
מ
ע
ר
ך
 
ש
ל
 
מ
ח
ר
ו
ז
ו
ת
 
(
ה
מ
ש
ת
נ
י
ם
)
 
מ
י
 
מ
ע
ב
י
ר
 
א
ת
 
ה
ע
ר
כ
י
ם
?
 
ס
ב
י
ב
ת
 
ה
ר
י
צ
ה
 
(
מ
ע
ר
כ
ת
 
ה
ה
פ
ע
ל
ה
)
 
א
ח
ר
א
י
ת
ל
ה
ע
ב
י
ר
 
א
ת
 
ה
מ
ש
ת
נ
י
ם
 
ל
-
m
a
i
n
ה
מ
ש
ת
נ
י
ם
 
י
ק
י
י
מ
ו
 
א
ת
 
ה
ת
נ
א
י
ם
 
ה
ב
א
י
ם
:
a
r
g
c
 
י
ה
י
ה
 
ח
י
ו
ב
י
a
r
g
v
[
a
r
g
c
]
 
=
=
 
N
U
L
L
a
r
g
v
[
0
]
 
ה
י
א
 
מ
ח
ר
ו
ז
ת
 
ה
מ
כ
י
ל
ה
 
א
ת
 
ש
ם
 
ה
ת
כ
נ
י
ת
 
40
40
 
ד
ו
ג
מ
ה
 
ה
ת
כ
נ
י
ת
 
מ
ד
פ
י
ס
ה
 
א
ת
 
ע
ר
כ
י
 
ה
מ
ש
ת
נ
י
ם
 
41
41
#include 
<stdio.h>
#include 
<stdlib.h>
 
int 
main(
int 
argc, 
char 
*argv[])
{
    
int
 i;
    
for
 (i = 0; i < argc; i++)
        printf("argv[%d] = %s\n", i, argv[i]);
 
    
return 
EXIT_SUCCESS;
}
 
ד
ו
ג
מ
ת
 
ה
ר
צ
ה
 
 
42
42
 
מ
ע
ר
ך
 
ש
ל
 
א
ר
ג
ו
מ
נ
ט
י
ם
 
43
43
argc
4
argv
NULL
 
“1”
 
“abcde”
 
“100”
 
“hello world”
 
מ
ח
ש
ב
ו
ן
 
פ
ש
ו
ט
 
44
44
int main(int argc, char *argv[])
{
    double leftOperand, rightOperand, result;
    char operator;
 
    /* incomplete, make sure all arguments are present */
 
    leftOperand = atof(argv[1]);
    rightOperand = atof(argv[3]);
    operator = *argv[2];
 
    switch (operator) {
    case '+':
        result = leftOperand + rightOperand;
        break;
    case '-':
        result = leftOperand - rightOperand;
        break;
    case '*':
        result = leftOperand * rightOperand;
        break;
    case '/':
        result = leftOperand / rightOperand;
        break;
    }
 
    printf("%g %c %g = %g\n", leftOperand, operator, rightOperand, result);
    return EXIT_SUCCESS;
}
Slide Note
Embed
Share

This content provides insights into memory allocation in C programming. It covers concepts like malloc, calloc, free, and realloc, illustrating how memory can be dynamically allocated and deallocated. The code snippets demonstrate how to work with arrays and pointers, along with explanations on stack and heap memory. The slides present a visual guide to understanding these memory management techniques.

  • Memory Management
  • C Programming
  • Dynamic Allocation
  • Pointers
  • Stack Memory

Uploaded on Aug 05, 2024 | 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. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. : - 1

  2. " " , : int main() { int a[10]; ... }; 2

  3. int main() { ... printf("enter number of elements\n"); scanf("%d", &size); int a[size]; ... }; 3

  4. , " ( " heap ) 4

  5. Data data ( Stack ) Last In First Out (LIFO) Heap ) ( )! " stack ) ( heap ( ) ( 5

  6. C C malloc calloc free realloc - 0 stdlib.h 6

  7. malloc void *malloc(int nBytes) ) ( NULL 7

  8. malloc int 10 int main() { int *a = (int*) malloc(10 * sizeof(int)); ... return 0; } ) sizeof ( void* ) ( 8

  9. malloc type *var-name = (type*) malloc(amount * sizeof(type)); casting ) ( 9

  10. free void free(void *ptr) ptr - malloc, calloc - realloc ptr - 10

  11. int main() { int *a, size, i; printf("Enter array size\n"); scanf("%d", &size); a = (int*) malloc(size * sizeof(int)); if (a == NULL) return 1; for (i = 0; i < size; i++) scanf("%d", &a[i]); for (i = 0; i < size; i++) printf("%d", a[i]); free(a); return 0; } 11

  12. int main() { int *a, size, i; main a size i Stack printf("Enter array size\n"); scanf("%d", &size); a = (int*) malloc(size * sizeof(int)); if (a == NULL) return 1; for (i = 0; i < size; i++) scanf("%d", &a[i]); for (i = 0; i < size; i++) printf("%d", a[i]); Heap free(a); return 0; } 12

  13. void* calloc( unsigned int n, unsigned int size_el ) size_el n ( , , .) void* realloc( void *ptr, unsigned int size ) size NULL , 13

  14. calloc - int main() { int *a, size, i; printf("Enter array size\n"); scanf("%d", &size); a = (int*) calloc(size, sizeof(int)); if (a == NULL) return 1; size , sizeof(int) - 0 for (i = 0; i < size; i++) scanf("%d", &a[i]); for (i = 0; i < size; i++) printf("%d", a[i]); free(a); return 0; } 14

  15. realloc - int *ptr = NULL; int size = 0, new_size = 0; scanf("%d", &size); ptr = (int*) malloc( size * sizeof(int) ); /* incomplete, must check if allocation succeeded */ ... scanf("%d", &new_size); ptr = (int*) realloc( ptr, new_size*sizeof(int) ); /* incomplete, must check if allocation succeeded */ ... if (ptr != NULL) free(ptr); 15

  16. int* func() { int *memPtr = NULL; memPtr = (int*) malloc(10 * sizeof(int)); ... return memPtr; } memPtr int main() { int * ptr = func(); if (ptr != NULL) { // do something with ptr free(ptr); ptr = NULL; } } ptr 16

  17. ' : " " ' : , ' ' 17

  18. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } 18

  19. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } 19

  20. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } 20

  21. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } 21

  22. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } ? 22

  23. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } 23

  24. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } 24

  25. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } capacity 25

  26. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } 26

  27. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } \0 27

  28. char* readLine() { int index = 0, c, capacity = INITIAL_SIZE; char *buffer = (char*) malloc(capacity * sizeof(char)); if (buffer == NULL) return NULL; for (c = getchar(); c != '\n'; c = getchar()) { if (index == capacity 1) { buffer = (char*) realloc(buffer, capacity * INCREMENT * sizeof(char)); if (buffer == NULL) return NULL; capacity = capacity * INCREMENT * sizeof(char); } buffer[index++] = c; } buffer[index] = '\0'; return buffer; } ( ) 28

  29. readLine int main() { char *line = readLine(); if (line == NULL) { printf("Fatal error: memory allocation failed!\n"); return 1; } printf("%s\n", line); free(line); return 0; } 29

  30. readLine int main() { char *line = readLine(); if (line == NULL) { printf("Fatal error: memory allocation failed!\n"); return 1; } printf("%s\n", line); free(line); return 0; } 30

  31. readLine int main() { char *line = readLine(); if (line == NULL) { printf("Fatal error: memory allocation failed!\n"); return 1; } printf("%s\n", line); free(line); return 0; } 31

  32. readLine int main() { char *line = readLine(); if (line == NULL) { printf("Fatal error: memory allocation failed!\n"); return 1; } printf("%s\n", line); free(line); return 0; } 32

  33. readLine int main() { char *line = readLine(); if (line == NULL) { printf("Fatal error: memory allocation failed!\n"); return 1; } printf("%s\n", line); , free(line); return 0; } 33

  34. - : NULL exit -

  35. exit void exit(int status); stdlib.h

  36. exit - realloc int , void resize(int *array, int newSize) { if (array == NULL) return; array = (int*) realloc(array, newSize * sizeof(int)); if (array == NULL) { printf("Fatal error: memory allocation failed!\n"); exit(1); } } 36

  37. typedef struct { char *title, *author; } Book; Book* newBook(const char *title, const char *author) { Book *result = (Book*) malloc(sizeof(Book)); /* incomplete, must check if allocation succeeded */ result->title = strdup(title); result->author = strdup(author); /* incomplete, must check if allocation succeeded */ return result; } 37

  38. 38

  39. Command Line Arguments main - ) ( int main(int argc, char *argv[]) { } 39

  40. ? ) - main ( : argc argv[argc] == NULL argv[0] 40

  41. #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int i; for (i = 0; i < argc; i++) printf("argv[%d] = %s\n", i, argv[i]); return EXIT_SUCCESS; } 41

  42. 42

  43. NULL hello world argc 100 4 abcde argv 1 43

  44. int main(int argc, char *argv[]) { double leftOperand, rightOperand, result; char operator; /* incomplete, make sure all arguments are present */ leftOperand = atof(argv[1]); rightOperand = atof(argv[3]); operator = *argv[2]; switch (operator) { case '+': result = leftOperand + rightOperand; break; case '-': result = leftOperand - rightOperand; break; case '*': result = leftOperand * rightOperand; break; case '/': result = leftOperand / rightOperand; break; } printf("%g %c %g = %g\n", leftOperand, operator, rightOperand, result); return EXIT_SUCCESS; } 44

More Related Content

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