Week 3 Flashcards
What are some general characteristics of C?
General purpose (you can code any coding task)
Compiled (turns text into binary)
Strongly typed (types cannot be manipulated).
What does it mean for a language to be an interpreted language?
Languages like Bash/Python/Node.js are interpreted.
A program called an interpreter reads your code and executes it.
What is a compiled language?
Source code is not executed by an interpreter.
Instead it is passed to a compiler which turns it into executable machine code.
The output of the compiler is an executable file which can be executed directly.
How do we execute a program with gcc
example: gcc -o executable file name source files
This looks like gcc -o prog file1.c file2.c
What do these gcc options do?
~~~
-Wall
-O<n>
-O1, -O2, -O3
-O0
~~~</n>
-Wall: generates more warnings -O<n>: optimization level (how hard should the compiler try and make the code faster. -O1, -O2, -O3: increasingly aggressive. May not always produce expected results. -O0: disable optimizations (useful for debugging/disassembly).
What do these options do?
-I/my/folder
-S
-l/my/library
-I/my/folder: search for header files in my/folder
-S: generate assembly code
-l/my/library: link dynamic library
What is the difference between:
~~~
int myvar;
int *myvar;
~~~
The first is a variable to an int val
The 2nd is a pointer to an integer
What happens here?
~~~
int* ptr = 0;
*ptr = 42;
~~~
There will be a segmentation fault
What is the difference between memory allocation in CPU registers and main memory?
CPU Registers (fast access, but only a handful of those)
Main memory (RAM) (slow, but can store lots of variables)
What is wrong with this line?
~~~
gcc -o myprogram.c myprogram.c
~~~
It passes the input file (myprogram.c) as an output file.
C, unlike many other languages allows you to directly access memory locations where variables are stored. What are some caveats to this?
Those are not physical memory locations.
Those are not absolute memory locations.
Those locations may not remain the same across executions.
How do we get a memory location?
We can store the address of a variable using &.
~~~
int* myptr = &val;
~~~
How are [] and * related?
They are both pointers. But the first guarantees that there is a 3 integer memory region allocated starting at the memory location pointed by var.
ptr is just a pointer. You can store a location in it, but there is no functional guarantee that it going to point to a useful or reasonable place in memory.
What do these keywords mean?
volatile
register
volatile: tell the compiler that a variable can change at any given time w/o being explicitly modified.
register: ask the compiler to always store a variable in a register
Where is a non pointer variable stored?
Each time you define a non pointer variable in a C program, memory is allocated for it on the stack.