uint vs size_t and two bugfixes in fak and unicode
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:13 +0200
changeset 63 5a82f89d607e
parent 62 b7061c0e239f
child 64 993b97c4ad2d
uint vs size_t and two bugfixes in fak and unicode * Better Description of copy.cc * mem2swap throws a perror on failure committer: Markus Bröker <mbroeker@largo.homelinux.org>
atoi_print.c
copy.cc
duff.c
fak.c
floating.c
mem2swap.c
myprintf.c
unicode.c
--- a/atoi_print.c
+++ b/atoi_print.c
@@ -8,7 +8,7 @@
 
 int main (int argc, char **argv)
 {
-    int i;
+    unsigned int i;
 
     for (i = 1; i < argc; i++)
         printf ("atoi(%s):=%d\n", argv[i], atoi (argv[i]));
--- a/copy.cc
+++ b/copy.cc
@@ -15,7 +15,7 @@
     char c;
 
     if (argc != 3) {
-        std::cout << "Benutzung: " << argv[0] << " <datei> <datei>" << std::endl;
+        std::cout << "Benutzung: " << argv[0] << " <QUELLE> <ZIEL>" << std::endl;
         return EXIT_SUCCESS;
     }
 
--- a/duff.c
+++ b/duff.c
@@ -8,9 +8,9 @@
 #include <stdlib.h>
 #include <string.h>
 
-char *duffcopy (char *to, char *from, int len)
+char *duffcopy (char *to, char *from, size_t len)
 {
-    int n, pos = 0;
+    size_t n, pos = 0;
 
     n = (len + 7) / 8;
 
@@ -49,7 +49,7 @@
 int main (int argc, char **argv)
 {
     char *to, *from;
-    int len;
+    size_t len;
 
     if (argc != 2) {
         printf ("Usage: %s <string>\n", argv[0]);
--- a/fak.c
+++ b/fak.c
@@ -6,7 +6,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-long fak (int i)
+unsigned long fak (short i)
 {
     if (i == 0)
         return 1;
@@ -18,15 +18,20 @@
 
 int main (int argc, char **argv)
 {
-    int number;
+    short number;
 
     printf ("Enter a number: ");
-    if (scanf ("%d", &number) < 0) {
+    if (scanf ("%hd", &number) != 1) {
         printf ("READ ERROR\n");
         return EXIT_FAILURE;
     }
 
-    printf ("The faktorial of %d is %ld\n", number, fak (number));
+    if (number < 0) {
+        printf ("NUMBER FORMAT ERROR\n");
+        return EXIT_FAILURE;
+    }
+
+    printf ("The faktorial of %d is %lu\n", number, fak (number));
 
     return EXIT_SUCCESS;
 }
--- a/floating.c
+++ b/floating.c
@@ -7,7 +7,7 @@
 #include <stdlib.h>
 #include <math.h>
 
-double *getValues (int elements)
+double *getValues (unsigned int elements)
 {
     double *values;
     int i;
@@ -25,7 +25,7 @@
 {
     double *values = getValues (MAX);
 
-    int i;
+    unsigned int i;
 
     for (i = 0; i < MAX; i++) {
         if (fabs (cos (i) - values[i]) < 0.001)
--- a/mem2swap.c
+++ b/mem2swap.c
@@ -40,6 +40,8 @@
     else {
         printf ("\tUsage: %s [MEM]\n\n", argv[0]);
         printf ("Report bugs to mbroeker@largo.homelinux.org\n");
+
+        perror ("Limit Error");
         return EXIT_FAILURE;
     }
 
--- a/myprintf.c
+++ b/myprintf.c
@@ -39,6 +39,7 @@
                     if ((ptr = realloc (s_str, ++size * BUF_SIZE)) == NULL) {
                         s_str[pos] = '\0';
                         fputs (s_str, fp);  /* print it anyway... */
+                        free (s_str);
                         return EOF;
                     }
                     s_str = ptr;
@@ -67,6 +68,7 @@
 
         if (pos > (size * BUF_SIZE / 2)) {
             if ((ptr = realloc (s_str, ++size * BUF_SIZE)) == NULL) {
+                free (s_str);
                 return EOF;
             }
             s_str = ptr;
--- a/unicode.c
+++ b/unicode.c
@@ -11,11 +11,11 @@
 
 #define FACTOR 1.42
 
-wchar_t *substr (const wchar_t * s, int start, int end)
+wchar_t *substr (const wchar_t * s, size_t start, size_t end)
 {
     wchar_t *str;
 
-    int len = end - start;
+    size_t len = end - start;
 
     if (len < 1)
         return NULL;