BasicOS
Basics
of
Operating
Systems

General information

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.).

Trick. In Linux, do you know that you can do a copy/paste very easily with your mouse only? Simply select some text with your mouse, then move your mouse pointer to the place where you want to copy this text, and then click on the middle mouse button ... the text is copied!
This session refers to the section "Advanced Concepts in C" of the slides on . do use these slides whenever necessary.

I. Enhancing a program

  1. Save the funnyAllocation.c program in a directory of your choice.
  2. What happens if you edit the file and move free(buf) before the last call to printf()? Test.
  3. What happens if you remove the call to calloc? Try to guess what happens before testing.
  4. Modify this program such that the value of b defined in main() is modified by b = b + 1 performed in funnyAllocation(). You may modify the program as you wish, except the int b parameter declaration of the funnyAllocation function, the b = b + 1; line of funnyAllocation, and the int b = 3; line of main. These parts shall not be changed. Compile and test.
  5. Modify the program such that the value of the global variable a is not modified by the call to the funnyAllocation() function in main, without removing or changing the int a; declaration nor the a = 5; line of code. Check your solution by assigning value 1 to a before calling funnyAllocation, and printing its value after the call.

II. Pointer manipulation


Design and code a program according to the following specifications.
The program first asks the user to enter a number. It then queries the user for that many other numbers; 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 (calloc).

III. Randomness


Design and code a program according the following specifications.

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.