Advanced concepts in C
This session is dedicated to the study of basic concepts of C: pointers and basic memory management.You don't need to submit source files or reports for this session. For this session, you will need to log into a PC running Linux. If you are at Eurecom, simply log on a PC of rooms 52 or 53. If you were to use another PC running GNU/Linux, I cannot guarantee that the lab works in the same way (outputs could be different, manual pages could be different, etc.).
This session refers to the section "Advanced Concepts in C" of the slides on . Use these slides whenever necessary.
Trick. Under most GNU/Linux distributions, you can easily copy/paste with the mouse: select some text (click and drag, double-click or triple-click), move your cursor to the destination and click with the middle button (or the wheel).
I. Enhancing a program
-
Save the
funnyAllocation.cprogram in a directory of your choice. -
What happens if you edit the file and move
free(buf)before the last call toprintf()? Test. -
What happens if we replace the call to
callocby a call tomalloc? Would the program still work? Would it be more or less efficient? -
What happens if you remove the call to
malloc? Try to guess what happens before testing. -
Modify this program such that the value of
bdefined inmain()is modified byb = b + 1performed infunnyAllocation(). You may modify the program as you wish, except theint bparameter declaration of thefunnyAllocationfunction, theb = b + 1;line offunnyAllocation, and theint b = 3;line ofmain. These parts shall not be changed. So, you cannot modify the input offunnyAllocationfunction... so what can you modify? Compile and test. -
Modify the program such that the value of the global variable
ais not modified by the call to thefunnyAllocation()function inmain, without removing or changing theint a;declaration nor thea = 5;line of code. Check your solution by assigning value 1 toabefore callingfunnyAllocation, and printing its value after the call.
II. Pointer manipulation
Design and code a C program that meets the following specification.
The program first asks the user to enter a number. It then queries the user for that many other numbers. For instance, if the user initially entered 5, the program queries the user 5 times for a number. Once all numbers have been entered and stored, the program prints them in reverse order of entry. Note: you will probably need the
atoi() function; as usual see the manual page to learn about it.
Additional constraint: do not use any array allocated in the stack, use only memory allocations in the heap (malloc or calloc).
III. Randomness
Design and code a C program that meets the following specification.
Generate a random number between 1 and 10. In the following we refer to this number as size.
Allocate an array of size integers and fill it with random numbers.
Finally, print the content of this array in reverse order.
For the random number generation use the random function (see the manual page).
Bonus: add an optional numeric parameter to your program.
If it is not given use time to seed the Pseudo Random Number Generator (PRNG) with the number of seconds since the Epoch (see man -S2 time).
Else, use the numeric parameter to seed the PRNG.
Test that without the parameter you get different outputs for different runs.
Test that with different parameter values you get different outputs while with the same parameter value you always get the same output.

