Presentation is loading. Please wait.

Presentation is loading. Please wait.

ISP – 6 th Recitation What’s void? Pointer to pointer/Array of pointers/Two dimensional array “Random” numbers Semaphores Code examples.

Similar presentations


Presentation on theme: "ISP – 6 th Recitation What’s void? Pointer to pointer/Array of pointers/Two dimensional array “Random” numbers Semaphores Code examples."— Presentation transcript:

1 ISP – 6 th Recitation What’s void? Pointer to pointer/Array of pointers/Two dimensional array “Random” numbers Semaphores Code examples

2 What’s void? In C, void is a data type with two uses: 1.When used directly as a return value of functions, it means that the function doesn’t return a value (procedure in other programming languages). void myFunction(int a) { printf(“a+1=%d\n”,a); } //no return and no compilation warning

3 What’s void? 2. When used as a pointer to void (void *p_void) it indicates a pointer to an unspecified data type. int num; void *p_void; char *p_char; p_char=# //generates a warning p_void=# //no warning generated

4 Pointers Important notes to understand about pointers: A pointer is an address to a place in memory. Memory addresses have the same size (4 bytes/ 32bits / int). sizeof(float *) = sizeof(int *) = sizeof(char *) = sizeof(void *) We define specific pointers (float *p_float) to allow the compiler to “understand” correctly the data in the address of the pointer and warn us of inconsistencies in code.

5 Examples float r_num; int i_num, *p_int; llist *head; p_int=&r_num; //issues a warning p_int=&i_num; //works alright r_num=*p_int; //another warning head->next = NULL; //works ok p_int=head; //warning p_int->next = NULL; //ERROR!

6 Array of Pointers Recall that in C, an array is defined as a pointer to the head of the array. An array of pointers is thus a pointer to a pointer. Examples: char **data, *str; Both data and str are pointers and have a value of a memory address. str[n] is a char variable and not a pointer. data[n] is a pointer to an array of type char. Note that data[n] is equivalent, in type, to str.

7 Array of Pointers data=malloc(5*sizeof(char *)); data data[3]=malloc(2*sizeof(char)); 0x1ff 0x200 0x201 0x202 0x203 0x204 0x205 0x206 0x207 Addr 0x206 Suppose you now wanted to increase the number of pointers in the array of pointers to 10. How can you do it and what would happen to the memory? Hint: check realloc() out.

8 Two Dimensional Arrays Since an array is seen as a pointer in C, it’s possible to look at a pointer to a pointer as a two dimensional array. int **data,tmp; data = (int **) malloc(5*sizeof(int *)); for (tmp=0;tmp<5;tmp++) data[tmp] = (int *) malloc (10*sizeof(int)); data[0][3]=15;

9 Random Numbers Computers are state machines. Their behavior has to be predictable and has to repeat itself given similar conditions. As such, computers can’t generate truly random numbers. They can, however, generate pseudo random numbers. Pseudo random numbers are sequences of numbers that the computer computes according to a certain algorithm based on a given number called “seed”. For a given seed, the computer would always generate the same sequence of pseudo random numbers.

10 srand() Initializes the random number generator with a given seed. Syntax: void srand (unsigned int seed); seed is naturally the seed number. We often use the current time as a seed: srand(time(NULL)); srand() and rand() require stdlib.h and time() requires time.h.

11 rand() Generates a random number. Syntax: int rand (void); The returned number is a pseudo random number in the range of 0 to RAND_MAX (normally 32767 but defined in cstdlib.h). If we desire a random number in the range of 0:N- 1, we can use: value = rand() % N;

12 Semaphores A semaphore object is a synchronization object that maintains a count between zero and a specified maximum value. The count is decremented each time a thread completes a wait for the semaphore object and incremented each time a thread releases the semaphore. When the count reaches zero, no more threads can successfully wait for the semaphore object state to become signaled. The state of a semaphore is set to signaled when its count is greater than zero, and nonsignaled when its count is zero.

13 Creating a Semaphore Creating a new semaphore Syntax : HANDLE CreateSemaphore( SECURITY_ATTRIBUTES *SemaphoreAttributes, LONG InitialCount, Long MaximumCount, char* Name ); InitialCounts is the initial number contained in the semaphore. MaximumCount is the max number that the semaphore counts. Name field identifies the semaphore in the system/process. If name already exists, a handle to the sempahore is returned and GetLastError() shows ERROR_ALREADY_EXISTS.

14 Opening an Existing Semaphore Opening a semaphore Syntax : HANDLE OpenSemaphore( DWORD DesiredAccess, BOOL InheritHandle, char* Name ); DesiredAccess requires a minimum permission of SYNCHRONIZE.

15 WaitForSingleObject() - Semaphore WaitForSingleObject() is used for waiting for an object to become “signaled”. Syntax : DWORD WaitForSingleObject( HANDLE hObject, DWORD dwMilliseconds ); For semaphores, “Signaled” means that the semaphore’s counter value is bigger than 0. Non signaled means that the counter’s value is 0.

16 ReleaseSemaphore() Release increases the count of the specified semaphore object. Syntax : BOOL ReleaseSemaphore( HANDLE hSemaphore, LONG ReleaseCount, LONG *PreviousCount); ReleaseCount – a number by which to increase the counter PreviousCount – a pointer to a long int where the previous count can be stored. Can be NULL if not necessary. Returns 0 if failed, nonzero if successful Note: Make sure you only release the semaphore with the right number in ReleaseCount.


Download ppt "ISP – 6 th Recitation What’s void? Pointer to pointer/Array of pointers/Two dimensional array “Random” numbers Semaphores Code examples."

Similar presentations


Ads by Google