Midterm III Flashcards
how to combine multiple files for compiling?
The make utility
– Manages the compilation and linking of multi-file software
– Reads a makefile (named makefile or Makefile) that specifies:
– The targets to be built
– Commands used to build them
– How the modules of a software system depend on each other
dependencies in makefile
– A directed acyclic graph (DAG)
– Object file (*.o): a file containing machine instructions of one module
– We typically generate one object file for each *.c file
makefile syntax
– Important to use Tab character; e.g., in hello: hello.c gcc -o hello hello.c – it must be a Tab character before gcc: hello: hello.c tab gcc -o hello hello.c – Another way which make allows: hello: hello.c; gcc -o hello hello.c – Use of gcc option: -c (compilation without linking)
how to use make from the command line
– make – Makes the first target – In our example target ‘help’ which prints makefile help – make target; for example: – make decimal2binary – make all – make clean
how to use gdb for large programs
– In Makefile use -g for all gcc commands
– Use break filename:line_number to set a breakpoint
– Use break filename:function_name (filename can be omitted)
how to declare a structure
struct student { int number; char name[26]; char username[11]; } x, y;
how to assign values to structure attributes
p = &z; (*p).number = 222333; p->number = 222333;
what is malloc?
This function returns a pointer to an unused memory block of size bytes, or the NULL pointer if the system is out
of memory. In this prototype, void * is a “generic” pointer, which is just a memory address. To use malloc
properly, we need always check whether it succeeded in allocating a block of memory, and write code similar to:
int p = (int) malloc(10000*sizeof(int));
how to free dynamically allocated memory?
void free(void *ptr);
how to create a linked list in C
struct node { int value; struct node *next; }; To create an empty list, we can use the following definition: struct node *list = NULL;
steps to implement mergesort of linkedlist
- Divide: Divide the n-element linked list to be sorted into two sub-lists of n/2 elements each
- Conquer: Sort the two sub-lists recursively using mergesort
- Combine: Merge the two sorted sub-lists to produce the sorted answer
what might happen to the heap after many calls of malloc and free?
After many executions of malloc and free, the free memory on
heap may become very fragmented, and malloc may need to spend significant time in searching for a free block
of sufficient size. There are different strategies how to make this more efficient. During execution of a process,
all heap memory may be used up, or there may just be no free block of sufficient size when malloc is called.
In this case, malloc normally makes a system call to the operating system kernel to obtain more heap memory.
This request usually asks for larger chunks of memory, called memory pages. The function malloc then adds
these pages to the pool of free blocks and completes request. If even after this system call malloc cannot find a
sufficiently large block, it will return NULL.
what are some of the disadvantages of using memory from the heap
First, since the heap is a large pool of
memory, large data, such as large arrays and structures, should be allocated in the heap. The stack may not have
enough memory for these data
Second, dynamically allocated memory stays allocated until it is deallocated explicitly or the process terminates.
Finally, heap allocation is slower than stack allocation
what does char* getline() do?
h reads a line from the standard input, including the final new-line character, and returns a dynamically
allocated string containing this line, including the new-line character at the end. If an EOF is encountered before
reaching end of line, then there is no new-line character to be included.
what is calloc?
The function calloc is used to allocate memory of an array of objects, and its prototype is:
void *calloc(size_t nmemb, size_t size);
where nmemb is the size of the array, and size of one object in the array