Final Exam Flashcards
Describe the “heap” region of computer memory used by the C programming language and its relation to dynamic memory allocation
Heap memory: Starting address x4000 - x6FFF. When you define a variable, the starting address of that variable is the starting address. Like Assembly Labels.
a large pool of memory that can be used dynamically. This is memory that is not automatically managed – you have to explicitly allocate (using functions such as malloc), and deallocate (e.g. free) the memory. This is why a programmer has control over.
Accessible at runtime and in the user data memory
Dynamic means at a run time. Not at compile time.
limited by the physical memory available and must use pointers.
Describe the basic functions that interact with the heap in the standard C library
Uses malloc() and free()
void* malloc(size_t size) ; // request memory allocation from heap
Returns a pointer to a region on the heap of size size bytes
void* is “generic pointer” (C for “just an address”).
Can pass it but cannot dereference
Making dinner reservations. This secures you reservation
void free(void* ptr) ; Clears reservation for address pointed to be ptr Manage by user-level C runtime library called libc
Interface function declarations found in stdlib.h
Discuss memory leaks and other dynamic memory errors
If you do not free the memory you used in heap when you are finished with it will result in what is known as a memory leak – memory that is still “being used”, and not available to other processes. Variables created on the heap are accessible anywhere in the program
Summarize the two basic file types in C: binary and ASCII
ASCII: Also known as the test files. Encoded in ASCII.
Can’t open an ASCII file in its true binary form with a text editor. Text editor needs to convert it to ASCII.
Example of ASCII files: .c, .txt, .JAVA, .ASM
Read by human
PennSIm takes in an ASCII file and converts to .OBJ file
Binary files: Files are not in ascii code (1s and 0s) created by a programmer. It is
Example of binary files: .OBJ / .DOC / .PPT files.
Cannot read by humans
Discuss the connection between file access (read/write) and I/O in assembly language
Specially files that are opened automatically for all C programs. Not files but I/O devices. These represent the keyboard and the ASCII display.
stdin: standard input (console) ->
stdout: standard output (console, for output)
stderr: standard error (console, for error message)
Differentiate between a character array and a string in C
In C, there are not an official string data type. There is a character type. If you want a string, then you need to add a null at the end of your char string.
No need to add null when you want an array of string.
Character array: Does not have the NULL character terminator.
Char name[ ] = {‘S’, ‘a’, ‘r’, ‘a’}; == [S, a, r, a]
String: Must end with a null character terminator ‘\0’
Char name[ ] = {‘S’, ‘a’, ‘r’, ‘a’, ‘\0’}; == Sara
Describe basic data types in C and how they are related to computer memory
char* my_char_ptr == 1 byte == %s
int* my_int_ptr == 4 bytes == %d
float* my_float_ptr == 4 bytes == %f
double* my_double ptr == 8 bytes == %lf
Describe arrays in C
Any data can be made in an array
Ex: char name[4] ; int my_array [5] ; float my_array[5] ; double my_array[5];
Name of the array is the starting address where the element is stored and follows sequential order. .
Like label
uses Index operator [ ]
Describe the notion of a pointer in C
A pointer is a variable that can only contain the memory address of another variable
Pointers are typed meaning they can be dereferenced.
Help pass address to functions instead of data
& gets the address of a pointer or other variables
Describe the connection between pointers and arrays in C
Difference:
Pointers are variables that contains a memory address of a variable that contains data
Arrays are labels for an address in daa memory that contains data
You can do pointer arithmetic on a pointer but you cannot do pointer arithmetic on the array.
You can change the address held onto by a pointer.
You cannot change the address of an array because it is a label.
Connection: :
Use the index operator [ ] as a dereferencing operator on a pointer if pointer points to an array
Use the ++ operator on a pointer to have it point to the next element it points to in memory.
Discuss storage classes in the C programming language
The stack (local variables, arguments and returns) Global regions (global vars and static vars) The heap - dynamic space.
There are 2 storage classes: automatic and static
Automatic variables: create a new each time when program’s execution enters in the function and destroys when leaves. lose their values when their block terminates (since they are on the stack)
Types: arguments, return types and local variables
Static variables: retain values between invocations. remains in memory while the program is running.
Types: global variables
Define the high-level language of C
High - level language means that the program language is user friendly.
Procedural which means C focuses on the program’s flow. Runs the program in main
File oriented
Portability which means same C code can be compiled for different ISAs
Explain how high-level languages are connected to assembly and machine languages
C code files I write are “compiled” using the compiler into assembly code. Then assembly code is “assembled” into machine code.
C Code(text files like .c or .h) -> ASM code (text files .asm) -> Machine code (binary files .obj)
Explain what a compiler is and what it does
Compiler converts a high-level language (like C) to low-level language (assembly), for example .c to .asm
Discuss how high level languages use the “stack” to arrange memory in an organized way
C uses stack to store data in data memory.
Holds local variables, return values and arguments (automatic variables )
Stacks work like a pile of dishes. First dish is the last and the last dish is the first dish you are going to take out. FILO (First in last out) So first value has the last address (high address) and last value is the first address(low address). Stack grows backwards in memory
C uses stack because the register file is too small. If more than 8 variables or if we need to call other subroutines.