05 Allocation
05 Allocation
Alan L. Cox
[email protected]
Objectives
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)
addresses of
…(code from before)…
foo.c2 and
printf(“&foo.c2 = %p\n”, &foo.c2);
foo.i printf(“&foo.i = %p\n”, &foo.i);
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
Cox Memory Allocation 9
Allocation
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?
How will these be used???
• Will they point to already allocated memory (what we’ve
seen so far)?
• Will new memory need to be allocated (we haven’t seen this
yet)?
#include <stdlib.h>
*i = 3;
array[3] = 5;
Static time
Some local variables:
int f(…)
{
static int value;
…
One copy exists for
}
all calls – allocated
int main(void)
at compile-time
{
… Confusingly, local
} static has nothing to
do with global static!
Shared Libraries
Programmer controlled
(variable-sized objects)
Heap Dynamic size, dynamic allocation
0x000000000000
Cox Memory Allocation 17
Why are there different methods?
Date * Date *
create_date3(int month, create_date3(int month,
int day, int year) int day, int year)
{ {
Date *d; Date *d;
return (d);
}
return;
}
free(today); destroy_date(today);
return; return;
} }
/* Return “y = Ax”. */
int *matvec(int **A, int *x) {
int *y = malloc(N * sizeof(int));
Initialization int i, j;
loop for y[]
i=0 for (; i<N; i+=1)
j=0 for (; j<N; j+=1)
y[i] += A[i][j] * x[j];
return (y);
}
char **p;
int i;
p[0]
p = malloc(M * sizeof(char));
`
p[1]
p[0]
char **p;
int i;
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);
/*
* Search memory for a value.
* Assume value is present.
*/
int *search(int *p, int value) {
while (*p > 0 && *p != value)
p += sizeof(int); p += 1;
return (p);
}
x = malloc(N * sizeof(int));
…
free(x);
…
y = malloc(M * sizeof(int));
for (i = 0; i < M; i++) {
y[i] = x[i];
x[i] += 1;
}
Premature free()
Reads and writes deallocated memory
void foo(void) {
int *x = malloc(N * sizeof(int));
…
free(x);
return;
}
void foo(void) {
List list = cons(1, cons(2, cons(3, NULL)));
…
free(list);
return;
}
bools
strings
pointers
structs
malloc() calls
simple I/O
simple string operations
}; #include <stdlib.h>
void
#include <string.h>
action1(struct thing **yp, const char *stuff)
strcpy(x->stuff, stuff);
Cox Memory Allocation 41
What does action2() do?
struct thing {
char *stuff;
};
void
yp = &x->another_thing;
Cox Memory Allocation 42
What does action3() do?
struct thing {
char *stuff;
};
bool
else
char *stuff;
};
void
*yp = x->another_thing;
Cox Memory Allocation 44
Next Time
Lab: Debugging
Assembly
Alan L. Cox
[email protected]
Cox Assembly 47
Why Learn Assembly Language?
You’ll probably never write a complete
program in assembly
With a few exceptions, modern compilers are much
better at writing assembly than you are
But, understanding assembly is key to
understanding the machine-level execution
model
Behavior of programs in presence of bugs
• High-level language model breaks down
Tuning program performance
• Understanding sources of program inefficiency
Implementing system software
• Compiler has machine code as target
• Operating systems must manage process state
Cox Assembly 48
Assembly Language
Cox Assembly 49
Assembly Language (cont.)
Cox Assembly 50