1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include "stdio.h" #include "stdlib.h" #include "string.h" static void print_it(unsigned len) { if(!len) return; unsigned i; char *star, *buf, fmt[16]=""; star = malloc(sizeof(char) * len+1); if(!star) return; buf = malloc(sizeof(char) * len+1); if(!buf) return; for(i=0; i<len; i++) strcat(star, "*"); snprintf(fmt, 16, "%%%ds\n", len); for(i=len; i>0; i--){ strcpy(buf, star+i-1); printf(fmt, buf); } free(star); free(buf); return; } int main(int argc, char **argv) { print_it(12); return 0; } |
[root@Imperfecto_1 ~]gcc run.c && ./a.out * ** *** **** ***** ****** ******* ******** ********* ********** *********** ************
Valgrind Output
[root@Imperfecto_1 ~]valgrind ./a.out ==5472== HEAP SUMMARY: ==5472== in use at exit: 0 bytes in 0 blocks ==5472== total heap usage: 2 allocs, 2 frees, 26 bytes allocated ==5472== ==5472== All heap blocks were freed -- no leaks are possible
No comments:
Post a Comment