C programming notes - School of Physics
C programming notes - School of Physics
C programming notes - School of Physics
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
C <strong>programming</strong> <strong>notes</strong><br />
file:///F:/my_docs/web_phys2020/C<strong>programming</strong><strong>notes</strong>.html<br />
36 <strong>of</strong> 40 19/03/2007 10:06 AM<br />
<strong>of</strong> memory.<br />
After freeing memory, it is good practice to set any pointers that referred to it to NULL, this will prevent you<br />
from inadvertantly using deallocated memory.<br />
The memory that malloc allocates can have random data in it. If you need it to be zeroed, either do this yourself<br />
with a "for" loop <strong>of</strong> use the "calloc" function (see "man calloc").<br />
Here is an example to get you started (mallocexample.c):<br />
#include <br />
#include <br />
#include <br />
/* Generates a two dimensional matrix, and fills it randomly with<br />
zeroes and ones. */<br />
int main(void) {<br />
int xdim, ydim;<br />
int i, j;<br />
int *p, *q;<br />
// Obtain the dimensions <strong>of</strong> the matrix from the user.<br />
printf("x dimension <strong>of</strong> matrix? > ");<br />
scanf("%d", &xdim);<br />
printf("y dimension <strong>of</strong> matrix? > ");<br />
scanf("%d", &ydim);<br />
// malloc the memory needed for the matrix. Note the use <strong>of</strong> "size<strong>of</strong>(int)"<br />
// which allows the program to run on machines with a variety <strong>of</strong> word sizes.<br />
assert(NULL != (p = (int *)malloc(xdim * ydim * size<strong>of</strong>(int))));<br />
// Fill the matrix with random 0s and 1s.<br />
for (i = 0; i < xdim * ydim; i++) {<br />
if (random() > RAND_MAX/2) {<br />
*(p+i) = 1;<br />
} else {<br />
*(p+i) = 0;<br />
}<br />
}<br />
// Now print the matrix out, showing the use <strong>of</strong> pointer arithmetic.<br />
for (i = 0; i < ydim; i++) {<br />
q = p + i * xdim;<br />
for (j = 0; j < xdim; j++) {<br />
printf("%1d", *(q++));<br />
}<br />
printf("\n");<br />
}<br />
// Free the memory we had allocated.<br />
free((void *)p);<br />
// And remove all information about where the memory used to be.<br />
// This is overkill in this example, since the program immediately<br />
// terminates, but it is good practice, in order to avoid<br />
// accidentally using free'ed memory.<br />
}<br />
p = q = NULL;<br />
return 0;<br />
Generating random numbers<br />
Random numbers are used in all sorts <strong>of</strong> ways in mathematics and physics. They are a fundamental tool that is <strong>of</strong>ten<br />
useful when conducting numerical simulations <strong>of</strong> physical systems with a computer.<br />
Writing a computer program to generate random numbers requires understanding some fairly subtle concepts.<br />
There are several different algorithms for generating a new random number given an initial random number. One <strong>of</strong> the