Lecture 23

Overloading

What would happen if we wanted to compile

int f(int a) { ... }
int f(int a, int* b) { ... }

Get duplicated labels for \(f\)

How do we fix this?

Name mangling

Encode the types of parameters as part of the label.

Example Convention:

F+type info + _ + name
int f() { ... } //F_f:
int f(int a) { ... } //Fi_f:
int f(int a, int b) { ... } //Fip_f:

C++ compilers will do this because C++ has overloading.

No standard mangling convention, all compilers different

C does not have overloading (no mangling)

Call C from C++

extern "C" int f(int n); // Tells C++ f will not be mangled.

Call C++ from C, need to tell C++ not to mangle the functions

extern "C" int g(int x) { ... } //Do not mangle g

or

extern "C" {
    ...
    ...
    ...
}

Memory Management and the Heap

WLP4, C, C++

Java, Scheme

How do new/delete or malloc/free work? -- variety of implementation

  1. List of free blocks:
      +-----------------------+
free->|1024|entire heap (1020)|
      +---------1024----------+
+---+----------------+
| 20|16 bytes memory|
+-4-+-------16-------+
     ^
pointer returned
      +-----------------------+
free->|1004|entire heap (1000)|
      +---------1004----------+
+---+----------------+  +---+----------------+        +-----------------------+
| 20|16 bytes memmory|  | 32|28 bytes memmory|  free->|972|entire heap (968)  |
+-4-+-------16-------+  +-4-+-------16-------+        +----------972----------+
            __________________________
           |                          |
           |                          V
      +---+----------------+        +-----------------------+
free->| 20|16 bytes memory|        |972|entire heap (968)  |
      +-4-+-------16-------+        +----------972----------+
            ____________________    ____________________
           |                    |  |                    |
           |                    V  |                    V       
      +---+----------------+  +---+----------------+  +-----------------------+
free->| 20|16 bytes memory|  | 32|28 bytes memory|  |972|entire heap (968)  |
      +-4-+-------16-------+  +-4-+-------16-------+  +----------972----------+

notice: \(p+*p == q\) thus contiguous, so merge

            __________________________
           |                          |
           |                          V
      +---+----------------+        +-----------------------+
free->| 52|48 bytes memory|        |972|entire heap (968)  |
      +-4-+-------48-------+        +----------972----------+
      
      +-----------------------+
free->|1024|entire heap (1020)|
      +---------1024----------+      

Note: repeated allocation and deallocations creates "holes" in heap

alloc 20
+--+---------------------+
|20|                     |
+--+---------------------+
alloc 40
+--+---------------------+
|20|40|                  |
+--+---------------------+
free 20
+--+---------------------+
|  |40|                  |
+20+---------------------+
alloca 5
+-+--+-------------------+
|5|  |40|                |
+-+15+-------------------+
etc...

Call fragmentation means even if \(n\) bytes are free, you may not be able to allocate \(n\) bytes.

To reduce fragmentation:

+-+--+-+--+-+---+
| |20| |15| |100|
+-+--+-+--+-+---+

alloc 10 - where do you put it?

Searching the heap