Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- /*
- Demo program:
- gcc -o memfun memfun.c -lm
- valgrind --tool=massif --massif-out-file=massif.out ./memfun
- massif-visualizer massif.out
- (or ms_print massif.out > massif.txt; less massif.txt)
- */
- int main(void)
- {
- int ii,jj;
- int *pt;
- int *pt2;
- float ac;
- int numberOfPeaks = 0;
- for (ii=0;ii<100;ii++) {
- /* Allocate some small linearly increasing memory block */
- pt = (int *)malloc((1000*ii+1)*sizeof(int));
- if (ii%5 == 0) {
- /* Allocate some gigantic memory block - but only every 10th iteration */
- pt2 = (int *)malloc((1000000)*sizeof(int));
- printf("Allocating large memory block\n");
- numberOfPeaks++;
- } else {
- /* Allocate just one element for every 9/10 iteration */
- pt2 = (int *)malloc((1)*sizeof(int));
- }
- /* Put in data and print it */
- pt2[0] = ii;
- printf(" Found value %i\n",pt2[0]);
- /* Free the memory - note that the peak */
- free(pt2);
- /* Spend time on "something" - the actual work does not matter*/
- ac = 1;
- for (jj=0;jj<10000*(1 + ii);jj++) {
- ac += sin(jj*0.01) + cos(jj*0.01 + 0.3);
- }
- printf(" Found dummy value %f\n",ac);
- /* Free the memory */
- free(pt);
- }
- printf("I made %i memory peaks\n",numberOfPeaks);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement