C Family Exam Q's Flashcards
(30 cards)
What does the statement #include stdlib.h do in a C or C++ program?
At compile time, the C preprocessor replaces this statement with the contents of the file called stdlib.h.
define min(a, b) ( (a) < (b) ) ? (a) : (b)
Why are a and b enclosed in brackets in the macro construction:
To prevent unintended effects through strength of operator binding.
What is an advantage of using the macro min(a, b)?
Faster as no function call is involved.
What is a disadvantage of using the macro min(a, b)?
Potential side effects, for example if an argument is incremented.
Write the equivalent C code for the following for loop:
int i;
for (i = 0; i < 10; i += 2)
printf(“i = %d\n”,i);
int i = 0;
while (i < 10)
{
printf(“i=%d\n”, i);
i += 2;
}
In what sense is C considered weakly typed?
C permits implicit type conversions between variables of different types.
What is a hazard of C’s weak typing characteristic?
Precision may be lost in implicit conversion.
What effect does a call to the fork function have?
It causes a child process to start in the operating system.
The child contains the same program code as the forking parent process.
The state of the child diverges from that of the parent once the fork has been executed
State two methods by which one process can communicate with another.
- By sending signals
- By opening a pipe
Which method of inter-process communication is preferred for large amounts of data?
Opening a pipe.
How should RGB be defined to represent color values ranging from 0 to 255?
RGB should be a typedef-ed as an unsigned char datatype. The
typedef makes the code more readable and enables the programmer to change the implementation more transparently later. The unsigned char datatype is the smallest amount of memory in which the integer range 0 to 255 can be stored.
How is the string “Kernighan & Ritchie” represented in C?
Represented by an array of char (bytes) with an additional null termination byte.
Fill in the blank: To allocate memory and copy a string, the code uses _______.
malloc
Why should a programmer free memory that they have previously allocated?
If unused memory is not freed it remains on the heap and cannot be reused (memory leak); this leads to unnecessarily large programs, hindering performance and may exhaust the available memory.
Why is it difficult to automatically free unused memory?
Pointers into memory make it hard to determine when the memory is no longer used (has no references to it).
Why is the goto statement considered dangerous in programming?
Considered dangerous because the non-local jumps make comprehension of the program potentially very difficult. Iteration, for example, can be introduced without it being clear where the iteration counter is or how it is terminated.
Give an example where the use of the goto statement is considered acceptable.
To make non-local jumps to a common piece of code to tidy up and exit cleanly in case of an error.
How do setjmp and longjmp provide similar functionality to goto?
Both mechanisms allow nonlocal jumps.
What is a key difference between goto and setjmp/longjmp?
Goto only enables jumps within the current scope, while setjmp/longjmp allow arbitrary jumps.
Describe the program state after setjmp has returned following a longjmp call.
The stack contains frames of main, funcA, and funcB with base and frame pointers pointing at the main frame.
What is the key difference between subroutines and coroutines?
- Subroutines begin execution at the entry point and finish executing their code before returning. Once they have returned, the program state in the subroutine is lost.
- Coroutines can yield control to other routines during their execution. Execution at the yield point can subsequently be resumed, with the program state intact.
Explain why the error on line 9 in the given C++ code occurs.
funcA is private in the base class and not accessible in the derived class.
How can the error on line 9 in the C++ code be remedied?
By declaring funcA to be protected or public.
Why can child call funcB on line 24 but not on line 25 in the C++ code?
funcB is public in Child but protected in GrandChild, restricting access from outside.