CS50 Week 5 Flashcards
a hexadecimal memory address is prefixes with what?
0x
What is the base number for hexadecimal, and what symbols are used?
Base 16
0-9 and A-F
What does == compare for pointers?
The memory addresses, not the content of those addresses.
This is especially important when comparing strings.
How are pointers often represented in pseudocode?
As arrows.
What is the easiest way to compare strings?
the functions strcmp( )
What are two causes of segmentation faults?
Iterating past the bounds of an array.
Dereferencing a pointer that hasn’t been initialized.
What information exists in memory before it is initialized?
Who know!? It is just a garbage value before you put something there.
In what way is an array a pointer?
An array is a pointer with a different syntax than using a *. The two are the same thing.
- c is equivalent to c[0]
- (c+i) is equivalent to c[i]
Visualize a graphical representation of memory:
text ---------------- initialized data ------------------ uninitialized data -------------------- heap | V
^ | stack ---------------------------- environmental variables
What are the four steps of compiling?
pre-processing
compiling
assembling
linking
What happens during pre-processing?
The header files are pasted to the top of the program, and constants in the body are replaced with their values.
What happens during the compiling phase?
The source code is translated into assembly language.
What happens during assembling?
The assembly language is translated into machine code.
What happens during linking?
The machine code is combined with whatever other machine code has been specified.
What is in the “text” portion of the memory diagram?
The 0s and 1s of the program.
What is in the initialized data and uninitialized data portions of the memory diagram?
global variables.
What is the danger of not checking the bounds of our arrays?
A stack overflow or buffer overrun attack.
What happens when if the stack runs into the heap?
A segmentation fault.
What is the tool for finding memory leaks?
Valgrind
What is a memory leak?
Memory that has been allocated to the heap that is never freed.
What is the command to start Valgrind
Valgrind ./file-name
What is the command-line argument for a more detailed description by Valgrind?
–leak-check-full
Valgrind can also detect some errors where an array is overstepped.
Valgrind can also detect some errors where an array is overstepped.
What does typedef do?
It creates an alias for a type.
What is a sentinel value?
A value that is returned when a program doesn’t work correctly.
What is the format for sscanf()?
sscanf(characterArray, “Conversion specifier”, address of variables);
What does struct do?
It creates a new data structure.
What is the format for a struct?
struct name
{
data housed = data;
}
How would a struct be given a new name?
typedef struct x { data; } name;
How do you access a variable inside a struct?
with . notation. Example?
typedef student { char* name; } student;
student prime;
prime.name = “James”;