typedef struct FloatBits FloatBits;
struct FloatBits {
unsigned int significand : 23;
unsigned int exponent : 8;
unsigned int sign : 1;
};
typedef union FloatIntBits FloatIntBits;
union FloatIntBits {
float the_float;
int the_int;
FloatBits the_bits;
};
int main(void) {
FloatIntBits fib = {.the_int = 0};
// Powers of 2
fib.the_bits.significand = 0;
fib.the_bits.exponent = 1;
fib.the_bits.sign = 0;
printf("Fields are (-1)^%u * 2^%i * 1+(%u/2^23); float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
fib.the_float = 1.0;
printf("Fields are (-1)^%u * 2^%i * 1+(%u/2^23); float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
fib.the_float = 2.0;
printf("Fields are (-1)^%u * 2^%i * 1+(%u/2^23); float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
fib.the_float = 8.0;
printf("Fields are (-1)^%u * 2^%i * 1+(%u/2^23); float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
fib.the_float = 1024.0;
printf("Fields are (-1)^%u * 2^%i * 1+(%u/2^23); float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
// Adding 1
fib.the_float = 3.0;
printf("Fields are (-1)^%u * 2^%i * 1+(%u/2^23); float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
fib.the_float = 9.0;
printf("Fields are (-1)^%u * 2^%i * 1+(%u/2^23); float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
fib.the_float = 1025.0;
printf("Fields are (-1)^%u * 2^%i * 1+(%u/2^23); float is %f\n", fib.the_bits.sign, fib.the_bits.exponent - 0x7F, fib.the_bits.significand, fib.the_float);
return 0;
}
```
---
## Output
Fields are (-1)^0 * 2^-126 * 1+(0/2^23); float is 0.000000
Fields are (-1)^0 * 2^0 * 1+(0/2^23); float is 1.000000
Fields are (-1)^0 * 2^1 * 1+(0/2^23); float is 2.000000
Fields are (-1)^0 * 2^3 * 1+(0/2^23); float is 8.000000
Fields are (-1)^0 * 2^10 * 1+(0/2^23); float is 1024.000000
Fields are (-1)^0 * 2^1 * 1+(4194304/2^23); float is 3.000000
Fields are (-1)^0 * 2^3 * 1+(1048576/2^23); float is 9.000000
Fields are (-1)^0 * 2^10 * 1+(8192/2^23); float is 1025.000000
---
## Summary
* This all makes sense
* We'll talk about why next lecture
---
## Review 2 Q1
Which of the following statements of the stack and heap are **false**?
a. Stack memory and heap memory grow from opposite ends of the memory space.
b. Heap memory is automatically freed.
c. Stack memory is automatically freed.
d. Variable length arrays may allocate memory on the stack or the heap.
---
```C
#include
#include
int main(int argc, char** argv) {
int value = atoi(argv[1]);
char* numbers = malloc(value);
for (char* iter = numbers; iter != numbers + value; ++iter) {
*iter = iter - numbers;
}
printf("%i\n", numbers[0]);
printf("%i\n", numbers[value-1]);
return 0;
}
```
If compiled and run with the argument "10",
a. This program will not compile.
b. The second value printed will be 9
c. The second value printed will be 36
d. This program will access out of bounds memory and crash.
---
```C
#include
#include
int main(int argc, char** argv) {
int value = atoi(argv[1]);
char* numbers = malloc(value);
for (char* iter = numbers; iter != numbers + value; ++iter) {
*iter = iter - numbers;
}
printf("%i\n", numbers[0]);
printf("%i\n", numbers[value-1]);
return 0;
}
```
What is true of this code?
a. This program will not compile.
b. If no value is provided on the command line, a default of 0 will be used.
c. If the user provides a value of 10 on the command line, this program will have a memory leak.
d. This program will access out of bounds memory and will crash.
---
```C
#include
#include
int main(int argc, char** argv) {
int value = atoi(argv[1]);
char* numbers = malloc(value);
for (char* iter = numbers; iter != numbers + value; ++iter) {
*iter = iter - numbers;
}
printf("%i\n", numbers[0]);
printf("%i\n", numbers[value-1]);
return 0;
}
```
If `char* numbers` is changed to `int* numbers` and `char* iter` is changed to `int* iter`:
a. This program will not compile.
b. This program will now compile, but it didn't before.
c. The second value printed will be 36.
d. This program will access out of bounds memory and will crash.
---
```C
typedef struct nicePtr nicePtr;
struct nicePtr {
unsigned long* memory;
size_t elements;
};
void someFunction(nicePtr p);
```
If an instance of the nicePtr struct is passed into someFunction:
a. The values of the array pointed to by memory will be copied automatically.
b. The value of the memory pointer will be copied, but not the elements of the array.
c. The typedef has an error and this will not compile.
d. Structs must be passed as pointers, so this will not compile.
---
Which of the following statements about recursion is **false**?
a. Some recursive functions cannot be rewritten to be tail recursive.
b. All recursive functions cause stack overflows.
c. All recursive functions can be rewritten as loops using a stack.
d. Compilers automatically optimize tail recursive functions to avoid repeated function calls.
---
Recall our stack:
```C
// For the size_t type definition
#include
typedef struct Stack Stack;
struct Stack {
char *memory;
size_t memsize;
size_t index;
};
Stack newStack(size_t size);
void freeStack(Stack s);
void push(Stack* s, char element);
char pop(Stack* s);
size_t len(Stack s);
```
```C
Stack s = newStack(10);
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 20; ++j) {
push(&s, i+j);
}
}
```
What is the next value returned by s.pop()? Or will this code fail to execute?
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
---
```C
typedef struct Bits Bits;
struct Bits {
bool a : 1;
bool b : 1;
bool c : 1;
bool d : 1;
bool e : 1;
bool f : 1;
bool g : 1;
bool h : 1;
};
typedef union LLUBits LLUBits;
union LLUBits {
// 8 bytes to a long long
Bits bits[8];
long long number;
};
```
Write a function accepts an LLUBits as its argument and prints out every bit of the long long number in LLUBits, from most significant to least.
---
## Quiz Review
* Review your Hw1
* Memory layout, MSB vs LSB
* 2's complement number
* And Hw2
* Or the stack lecture if you haven't finished your hw2