1/74
Looks like no tags are added yet.
Basic linux commands
cd - change directory
ls - list directory contents
mkdir - make directory
rmdir - remove directory (must be empty)
rm - removes file
mv - move file or rename
When listing files in linux, what option will show hidden files in a directory, along with the rest of the files?
-a
What best describes what a path is?
The roadmap from one location to another in a file system
What two words best describe the types of paths?
Relative and absolute
What symbol represents the users home directory?
~
What is an example of a type that is not built in to C
int32_t (as opposed to float, int, char, etc.)
What are the selection structures that C offers?
Switch statements and if statements
What are the pre-test loops in C?
While loop and for loops
What function allows for output in C?
fprintf or printf
Format flag for integers
d
Format flag for strings
s
Format flag for double
lf
Format flag for char
c
What stream is connected to the keyboard
stdin
What stream(s) is/are connected to the terminal or console
stderr and stdout
What is the first step in the compilation process
Preprocessor
What is the second step in the compilation process
Compiler
What is the third step in the compilation process
Assembler
What is the fourth step in the compilation process
Linker
What is true about the linker?
- The linker is responsible for resolving functions used in one file but created in another
- The linker creates an executable from object files (.o)
How do you add the debugging symbols to your executable when compiling
-g
True or false: All functions must be declared before they can be called
True
True or false: When passing a parameter to a function, by default, any changes to that parameter will always be reflected in the calling function?
False
When an array is passed to a function, what is the passing mechanism that is used?
pass-by-pointer
Rules for naming identifiers
1. Identifiers can be a combination of letters in lowercase or uppercase or digits or an underscore
2. An identifier cannot start with a digit.
3. Keywords cannot be used as identifiers.
4. We cannot use special symbols like !, @, #, $, %
etc. in our identifier.
5. Identifier can be of any length
What is the meaning of the scope of a variable
Where it is legal to use a variables name
What is the definition of the lifetime of a variable
How long the variable stays in memory
True or false: You can pass an array to a function and changes to that array will be communicated to the caller?
True
How to create a pointer
int * name;
Assuming you have a pointer name ptr that has not been given a target, what happens when you try to dereference it?
Most likely its a memory access violation
Assuming you have a pointer named ptr that has been given a target of NULL, what happens when you try to dereference it?
It will be a null pointer dereference also known as a segmentation fault
In a Makefile, what character must come first one the lines below the targets?
A tab
Parts of a Makefile
Rule, Dependency, Target
When running a Makefile, when does make determine to compile a file?
When the dependency is newer than the target
What is allowed as a rule in a Makefile?
Bash commands, linking commands, compiler commands
In a bash script, what must you do to it so that you can run it?
Make it executable
In bash, if you want to see if the last command was a success, what command will return true?
if [[ $? -eq 0 ]]; then
Best way to compile the files main.c and project.c into an executable named program?
gcc -Wall -Wextra -std=c11 -o program main.c project.c
In gdb, what command will set a breakpoint on line 32 in the file zipData.c
break zipData.c:32
In gdb, if you want to see the contents of a variable named var, which command will do that?
display var
What is the resulting output if you try to run gdb without compiling with -g
GDB will run, but trying to set a breakpoint or display variables, etc. will result in "No symbol table loaded"
What is a string in C
Character array
What do all strings end with
Null terminator character
strlen( )
Returns the length of a string
strcpy( )
Accepts two C-strings or pointers to two C-strings as arguments. The function copies the second C-string to the first C-string. The second C-string is left unchanged.
strcat( )
Concatenates string values and saves the result to a specified field.
strcmp(a, b)
Returns 0 if the strings are equal, negative if a comes before b, and positive if b comes before a
puts( ) and gets( )
Gets allows the user to inou stuff after the key, puts is similar to printf( ) and prints to terminal
What makes C strings so dangerous
Have to manually allocate and free memory, violation of the null terminator character causes issues
What does free( ) do
Frees the block of memory that the pointer was using, does NOT make the pointer NULL. Note that for strings and array, if the pointer is not pointing to the first location of the array, then free will not work and a segmentation fault will occur.
What is a memory leak
Kind of resource leak that happens when a computer software or program poorly handles memory allocations and fails to free memory that is no longer required
What tool is used to detect memory leak
valgrind
How do you use realloc( )
list = realloc(list, size*sizeof(int)) *int is whatever you want the size of, if its a list of chars use sizeof(char) etc.*
What is realloc used for
Resizes the memory block pointed to by the pointer that was previously allocated with a call to malloc or calloc. Returns newly allocated memory or null if the request fails.
How can const targets be used with pointers
Can either a.) create a pointer to a constant in C, which means that the pointer would point to a constant variable, or b.) create a constant pointer, meaning the pointer can not point to anything else
What is pointer casting?
When you change the type of an object to another type:
int A = 150;
Pa = &A;
double D, *Pd;
char ch, *Pc;
Can't do Pd = Pa because they don't have the same types, but we can do:
Pd = (int*)Pd;
Now we can do Pd = Pa.
Same is possible for Pc.
How does the size of the data type impact the range of numbers that can be represented?
Each data type has a certain amount of memory that it can hold. This means that some data types have the inabilty to store data of certain sizes. In most machines an int in 32 bits, meainging it can represent 2^32 bit patterns (4,294,967,296). This means if the int is unsigned it can be any int from 0 to 4,294,967,296, if it is signed then we "split" it, meaning the variable can store -2,147,483,648 --> +2,147,483,648.
How are negative integers stored in a computer?
Using two's complement
What is big endian? What affect does it have on data?
It indicates that the larger bytes (most significant value) are represented first from left to right. For example, the two bytes required to store hexadecimal number 4F52 would be stored as 4F52, with 4F being at storage location N and 52 being at the location N+1
What is little endian? What affect does it have on data?
Similar to big endian, it indicates that the smaller bytes are represented on the left and the biggest ones are on the right. The two bytes required to store the number 4F52 would be represented as 524F, with 52 being at location N and 4F being at N+1
What are the bitwise operators in C?
& - Binary AND operator copies a bit to the result if it exists in both operands (defaults to zero if not true for both)| - Binary OR operator copies a bit if it exists in either operand (referring to the ones, if there are no ones then it is a 0)^ - Binary exclusive OR, copies the bit if it is set in one of but not both operands (Again, only true for the ones)~ - Binary Ones Complement Operator is unary and has the effect of flipping bits<< - Binary left shift operator. The left operands value is moved left by the number of bits specified by the right operand>> - Binary right shift operator. The left operands value is moved right by the number of bits specified by the right operand.
What happens when you shift a signed integer to the right? To the left?
When shifting to the right, it is the equivalent of dividing the number by 2^N (throwing out remainders) where N is the number of bits shifted.
Ex.) 11010010 >> 2 = 00110100
210/(2^2) = 52
Shifting to the left is the opposite, and is therefore equivalent to multiplying the decimal number by 2^N
Ex.) 0010 << 2 = 1000
2 * (2^2) = 8
Big ended vs little ended
In a big-endian system, the most significant byte (i.e. the byte containing the highest order bits) is stored at the lowest memory address, while the least significant is stored at the higest memory address.In a little-endian system we see the opposite, where the least significant is stored at the lowest memory and the most significant is stored at the highest memory.
Pros/Cons:
Little endian is less intuitive while reading, but it also means that when new data is added you don't need to shift everything over in order to add, you can just add the data to the end. For big-endian, it is more intuitive to read left to right, but you need to shift everything down in order to add more data to the front.
Ex.) For hexadecimal number 0X1234
Big-endian:
Address: Contents:
0X0000 0X12
0X0001 0X34
Little-endian:
Address: Contents:
0X0000 0X34
0X0001 0X12
Setting or unsetting bits
To set a bit you would use the OR bitwise operator, and to unset a bit you would use the AND operator
Ex.)
Setting:
x |= (1<
Accessing fields of a struct
If you have a struct variable, you can access by doing:
struct.field
If you have a pointer to a struct, you have to access by doing:
struct->field
What are the 32-bit registers?
Same as 16 bit registers, but have more space (32 bits as opposed to 32) and are denoted by an E in front of the name (EAX, EBX, ESI, EBP, etc.). EFLAGS is also an example of a 32 bit register, contains status flags.
What are the 64-bit registers?
Same as 16 bit and 32 bit registers, but are denoted by an R in front of the name (RAX, RBX, RSI, RBP, RFLAGS, etc.). Note the size difference as well.
What do the single letter suffixes on our commands mean?
They indicate the size of the data being manipulated:
b = byte (8 bits)
w = word (16 bits)
l = longword (32 bits)
q = quadword (64 bits)
t = ten bytes (80 bits)
What is the %rsp? %rbp?
%rsp is a register that holds the address of the current position of the stack pointer. The stack pointer is a special register that keeps track of the current top of the stack, which stores temporary data during function calls and other operations. %rbp is a register that holds the address of the current position of the base pointer. It is a reference point for accessing data on the stack, particularly local variavles in a function.
What direction does the stack grow in?
It grows towards lower addresses, the top element has the lowest address and vice versa. It grows "downwards" as more stuff is added
What does the call function do that is "hidden" in the call command?
The call instruction automatically handles pushing the return address onto the stack and setting the IP to the called function's address, making it easier to call function from higer-level languages
What happens when a function is entered? What happens to the base pointer? What happens to the stack pointer? How many bytes are in the stack frame?
When a function is entered, the current value of the base pointer (BP or RBP) is pushed onto the stack to create a stack frame. Then the stack pointer (SP or RSP) is moved down by the amount of space needed for local variables and other function call information, such as the return address. The new value of the base pointer is then set to the current value of the stack pointer, which establishes a fixed reference point for accessing local variables and parameters.The amount of space allocated for the stack frame depends on the number and type of local variables and the amount of space required for the function call information. The total size of the stack frame is usually a multiple of the word size of the processor (e.g., 4 bytes on a 32-bit processor, 8 bytes on a 64-bit processor).When the function returns, the stack pointer is restored to its original value, the base pointer is popped off the stack, and control is transferred back to the calling function.
What registers are used for passing parameters? If all of those are used, where do the extra parameters go?
1: %rdi
2: %rsi
3: %rdx
4: %rcx
5: %r8
6: %r9
If there are more than 6 arguments, the additional arguments are passed on the stack in reverse order (right to left) before the function is called. The caller is responsible for pushing the arguments onto the stack and for clearing them up after the function is returned.
What is the general formula for computing an address
0xbase address + (index scale fator) + displacement
What is leaq? How is it used?
It is used to load the effective address of a memory operand into a register. It effectively makes a pointer.