Midterm III Flashcards

1
Q

how to combine multiple files for compiling?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

dependencies in makefile

A

– 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

makefile syntax

A
– 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how to use make from the command line

A
– 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how to use gdb for large programs

A

– 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how to declare a structure

A
struct student {
int number;
char name[26];
char username[11];
} x, y;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how to assign values to structure attributes

A
p = &z;
(*p).number = 222333;
p->number = 222333;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is malloc?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

how to free dynamically allocated memory?

A

void free(void *ptr);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how to create a linked list in C

A
struct node {
int value;
struct node *next;
};
To create an empty list, we can use the following definition:
struct node *list = NULL;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

steps to implement mergesort of linkedlist

A
  1. Divide: Divide the n-element linked list to be sorted into two sub-lists of n/2 elements each
  2. Conquer: Sort the two sub-lists recursively using mergesort
  3. Combine: Merge the two sorted sub-lists to produce the sorted answer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what might happen to the heap after many calls of malloc and free?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what are some of the disadvantages of using memory from the heap

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what does char* getline() do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what is calloc?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what is realloc?

A

realloc is used to reallocate additional or minimize existing malloc/alloc blocks of memory

17
Q

what is the psuedo code for dynamically allocating an arraylist

A

If array is full
Resize the array to twice its current capacity using realloc
Store the new element