Mercurial > sos > sos
diff src/std.c @ 26:c1ad124f2aaf
Re-did errors for pop, added some nice testing for dynamic memory
I now have a decent test program for dynamic memory implemented as a separate
program (dynamic.c)
The stack now stores its current size, this is used in operations.c to check
the size
Pop should now also be called with a pointer to an eltType. Description of
behavior is in stack.h
author | Jonathan Pevarnek <pevarnj@gmail.com> |
---|---|
date | Sun, 20 Mar 2011 14:17:24 -0400 |
parents | 45a80ea314ae |
children | 1a070e843bf6 |
line wrap: on
line diff
--- a/src/std.c Wed Mar 16 17:29:25 2011 -0400 +++ b/src/std.c Sun Mar 20 14:17:24 2011 -0400 @@ -1,6 +1,6 @@ #include <std.h> -char* itoa(int n, char *a) +char* itoa(long long n, char *a, unsigned short base) { char *ret = a; if(n < 0) { @@ -10,8 +10,10 @@ char *b = a; if(!n) *b++ = '0'; for(; n; b++) { - *b = n%10 + '0'; - n = n/10; + int temp = n%base; + if(temp < 10) *b = '0' + temp; + else *b = 'a' + temp - 10; + n = n/base; } *b-- = '\0'; for(; a < b; a++, b--) { //reverse @@ -27,7 +29,7 @@ char *ret = a; int n = (int) x; //integer part double d = x - (double) n; //fractional part; - itoa(n, a); + itoa(n, a, 10); if(prec) { //only do the decimal part if decimal parts were asked for while(*a && *++a); //get to the null character from itoa int i; //counter variable for the for loop @@ -139,7 +141,7 @@ if((sizeof(blockUnit)%sizeof(u64))) { sPrint("WARNING: MEMORY NOT 8-BYTE ALIGNED\n"); char foo[40]; - sPrint(append("MEMORY BLOCK SIZE IS: ", append(itoa(sizeof(Header), foo), "\n"))); + sPrint(append("MEMORY BLOCK SIZE IS: ", append(itoa(sizeof(Header), foo, 10), "\n"))); } } @@ -176,7 +178,7 @@ if(scan->next < scan && (toFree > scan || toFree < scan->next)) break; toFree->next = scan->next; scan->next = toFree; - if(scan + toFree->size == toFree) { + if(scan + scan->size == toFree) { scan->size += toFree->size; scan->next = toFree->next; toFree = scan; @@ -186,3 +188,27 @@ toFree->next = toFree->next->next; } } + +void printHeader(Header* head) +{ + char buffer[20]; + sPrint("Memory address is: "); + sPrint(itoa((size_t)head, buffer, 16)); + sPrint("\n"); + sPrint("The size of the block is: "); + sPrint(itoa(head->size, buffer, 10)); + sPrint("\n"); + sPrint("Next memory address is: "); + sPrint(itoa((size_t)head->next, buffer, 16)); + sPrint("\n\n"); +} + +void printMem() +{ + Header *cur = allocp; + do { + printHeader(cur); + cur = cur->next; + } while(cur != allocp); + sPrint("PRINTOUT DONE\n\n"); +}