06-memory-allocation
06-memory-allocation
Memory Allocation
COMP 222: Introduction to Computer Organization
Instructor:
Alan L. Cox
foo.c1 = ’a’;
foo.c2 = ’b’;
foo.i = 0xDEADBEEF;
c1 c2 padding i
61 62 EF BE AD DE
x86 uses “little-endian” representation
p = &a; p = &a;
p += 1; In each, p now points to b p += 1;
(Assuming compiler doesn’t
reorder variables in memory)
c1 c2 padding i
61 62 EF BE AD DE
c1 c2 padding i
61 62 7F EF BE AD DE
x86 uses “little-endian” representation
c1 c2 padding i
61 62 FF 7F EF BE AD DE
x86 uses “little-endian” representation
Read/Write Data
Loaded from the executable
Read-only Code and Data
Unused
0x000000000000
Two questions:
When do we know the size to allocate?
When do we allocate?
Two possible answers for each:
Compile-time (static)
Run-time (dynamic)
Sometimes not:
Is this going to point to one
char *c; character or a string?
int *array;
How big will this array be?
#include <stdlib.h>
*i = 3;
array[3] = 5;
Shared Libraries
Programmer controlled
(variable-sized objects)
Heap Dynamic size, dynamic allocation
0x000000000000
return (d);
}
return;
}
free(today); destroy_date(today);
return; return;
} }
void foo(void)
{
struct list *list = makeNE(1, makeNE(2, makeNE(3, NULL)));
...
void foo(void)
{
struct list *list = makeNE(1, makeNE(2, makeNE(3, NULL)));
...
list:
1 pad
2 pad
3 pad NULL
void foo(void)
{
struct list *list = makeNE(1, makeNE(2, makeNE(3, NULL)));
recur_print(list);
...
void foo(void)
{
struct list *list = makeNE(1, makeNE(2, makeNE(3, NULL)));
iter_print(list);
...
void foo(void)
{
struct list *list = makeNE(1, makeNE(2, makeNE(3, NULL)));
list = iter_append(list, 4);
...
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 32
Removal
struct list {
int first;
struct list *rest;
};
void foo(void)
{
struct list *list = makeNE(1, makeNE(2, makeNE(3, NULL)));
...
}
list:
1 pad
2 pad
two:
3 pad NULL
list:
1 pad
2 pad
two:
3 pad NULL
list:
1 pad
two:
3 pad NULL
char **p;
int i;
`
p[i] = malloc(N * sizeof(char));
p[0]
p = malloc(M * sizeof(char));
p[1]
`
p[0]
for (i = 0; i <= M; i += 1)
p[i] = malloc(N * sizeof(char));
Off-by-1 error
Uses interval 0…M instead of 0…M-1
Leads to writing unallocated memory
char *s = “1234567”;
…
char *t = malloc((strlen(s) + 1) * sizeof(char));
strcpy(t, s);
Premature free()
Reads and writes deallocated memory
void foo(void)
{
struct list *list = makeNE(1, makeNE(2, makeNE(3, NULL)));
...
free(list);
pointers
structs
malloc() calls
simple I/O
}; #include <stdlib.h>
void
#include <string.h>
action1(struct thing **yp, const char *stuff)
strcpy(x->stuff, stuff);
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 53
What does action2() do?
struct thing {
char *stuff;
};
void
yp = &x->another_thing;
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 54
What does action3() do?
struct thing {
char *stuff;
};
bool
else
yp = &x->another_thing;
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 55
What does action4() do?
struct thing {
char *stuff;
};
void
*yp = x->another_thing;
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 56