What are the three memory allocation functions and how do each of them work?
malloc - allocates the number of bytes that you pass to is as an argument and returns a pointer to the allocated space in memory; does not initialize the space
calloc - allocates space in memory for num amount of "things", with each "thing" being a size of bytes that you pass as an argument, and returns a pointer to the allocated space in memory; initializes all bytes to null
realloc - given a pointer to allocated memory and size of bytes you want the allocated memory to be changed to, if the size given is less than the current size, the allocated space is truncated and the original pointer is returned. If the size given is greater than the current size, if there is enough free space, the allocated space is expanded and the original pointer is returned. Otherwise new space is allocated with the new size and all the data from the old space is copied to the new before the old space is deallocated and a new pointer is returned
What are the three characteristics of all data
Type, value, and location
What are the two types of file streams and what are the differences between them
Binary steams - byte-oriented with no interpretation of the data done
Text streams - files have zero or more lines, with lines containing zero or more characters and are terminated with EOLN sequence
Arrays and Structures are both aggregate data types; What is the difference between the data stored in each
Arrays - Homogeneous collections of elements
Structures - Heterogeneous collections of elements
What is the difference between a right logical bitwise shift and a right arithmetic bitwise shift
logical - always fills with zeros
arithmetic - fills with copies of the leftmost bit
What specifies the location/address of a variable? How do we get the value/contents of a variable?
a) reference
b) dereferencing
Follow up: What is the difference between dereferencing a variable that is a primitive data type versus a variable that is a pointer?
what are the four type categories and what data types fall into each category
Simple Types - what the hardware provides(integers, floating point)
Primitive Types - discrete and scalar values (discrete types and scalar types)
Structured types - arrays, structs, types that are built from any of the four categories
User-defined types - types that are created by the user, ADTs
What do the system call read and write functions return if there is no error
The amount of bytes transferred
struct example contains an int named a, a char named b, and a float named c.
struct example var2;
var2 = {.c=12.5, 'x', 42};
These are the two errors with the code above.
Requires compound literal
Requires designated initializers
var2 = (struct example){ .c=12.5, .b='c', .a=42};
What are the three language paradigms and what best defines each of them
Imperative - program is a sequence of statements
Object-Oriented - program is collection of classes
Functional - computation is done through a series of function applications that produce reliable output from input
What does it mean to have a dangling pointer and what are the potential dangers?
A dangling pointer is a pointer that pointed to allocated memory, but the space was deallocated and the variable is left unchanged. This is dangerous because the pointer still points to the location in allocated memory that it originally did, so if you dereference that pointer, what is at that location is unknown. How can you prevent a dangling pointer?
What are the five segments of a programs address space and what is stored in each
text - executable code
data - initialized global variables
bss - uninitialized global variables
stack - local variables and function call information
heap - dynamic storage
What is different about how input/output using the terminal is buffered compared to how input/output using a file or a pipe is buffered
I/O from the terminal is line buffered; For input, nothing is sent to the program until a newline is entered; For output, nothing is sent to the terminal until a newline is printed.
I/O using a file or a pipe is block buffered. Buffer size is a block of bytes. For input, buffer will be sent to the program once it is full. For output, buffer will be written to the file or pipe once it is full.
What is the difference between the interface and the implementation with respect to ADTs
Interface - tells the user what the Abstract Data Type does. It is the only thing that the user of an ADT has available to them. Allows the user to use the ADT but not manipulate any of the underlying data and implementation of the ADT.
Implementation - How the Abstract Data Type actually works. This is designed by the creator of the Abstract Data Type and is encapsulated so that a user of the ADT could not have access to the underlying workings.
how would you open a file named test.dat to do binary reading and writing.
fopen("test.dat", "rwb");
char *c[] = {"This", "Question", "Was", "Rude"};
char **cp[] = {c+3, c+2, c+1, c};
char ***cpp = cp;
printf("%s\n", &((*(cpp[2])+9)[-12]));
What is the output:
Output: is
cpp points at c+3, cpp[2] gets c+1; c+1 points at "Question", *(c+1) gets "Question"; pointer is pointing at 'Q' in "Question\0", +9 moves the pointer past the the characters of "Question\0" and points at 'W' in "Was\0". [-12] moves pointer from 'W' in "Was\0" to 'i' in "This\0", and then gets 'i' in this. & creates a pointer to the location 'i' is, and then %s moves to each character following 'i' and prints it out until it finds the NUL byte. this results in "is"
What does adding the static modifier to a local variable do? How about adding it to a global variable?
local - variable value becomes persistent throughout function calls. It's scope is still limited to the function but it is moved to the data section of program memory.
global - the scope of the variable is limited to the file it is declared in.
For a program utilizing standard I/O, what buffers are used and where are these buffers stored in memory?
Program Buffer - Program Address Space
Standard i/o buffer - Program Address Space
OS buffer - OS address space
Data File - Secondary Storage
Follow Up: Why is i/o buffering used?
Create a structure that has the tag example and contains the following data members: an integer named a, a character named b, and a float named c. Then redefine this structure to be a type called myExample.
struct example {
int a;
char b;
float c;
};
typedef struct example myExample;
Write the lines of a Makefile that take support.c, defs.h, and commands.h and compiles them into a single object file.
support.o: support.c defs.h commands.h
gcc -std-c99 -Wall -Wextra -pedantic -c support.c
Follow up: Write how you would create macros for the compiler and the flags and a .c.o transformation rule, and how would you update your previous live to utilize these
Given:
Thread Creation Syntax: int pthread_create(pthread_t *id, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Threading Function: void *foobar(void *arg)
Thread: pthread_t t1
Part 1: Create a function pointer for foobar named funcpt to pass as the function pointer argument of pthread_create()
Part 2: foobar() requires an array and the length of that array. Create a structure that can store both of these requirements and pass the structure as the arg argument of pthread_create(). Write how you would retrieve the data of the structure once inside of foobar().
struct test{
int array[3];
int length;
};
void *(funcpt)(void *) = foobar;
struct test storage = {.array[0]=1,.array[1]=2,.array[2]=3,.length=3};
pthread_create(&t1, NULL, funcpt, (void *)&storage);
void *foobar(void *arg){
struct test *storage = (struct test *)arg;
OR
struct test storage = *(struct test *)arg;
What is the difference between strongly typed and weakly typed? What is the difference between static typing and Dynamic typing?
Strongly typed languages prohibit using operations on a type that isn't supposed to support that operation, while there are ways to get around typing restrictions in a weakly typed language, such as type casting.
Static typing means that the type of a variable is determined during compilation time, while Dynamic typing means that the type of a variable is determined during runtime
The program buffer, stdio buffer, and OS buffer are empty. What is done to put more data into the OS buffer and subsequently the stdio buffer and program buffer?
A physical read from the data file
Follow up: The program buffer and the stdio buffer is empty; What is done to put more data into the stdio buffer and subsequently in the program buffer?
How does a Union work
All data members of a union share the same space. When a value is assigned to one of the data members, a section of the bits of the union are manipulated to represent that value based on the type of the data member that is assigned to. When a value is retrieved from the union using one of the data members, the type of that data member determines how many bits of union are selected and how those bits are represented.
What is stored in each functions stack frame on the runtime stack
Parameters for this function, return address to the caller function, and local variables (sometimes the frame pointer for the previous stack frame is stored, but that is only if it is used)