realloc, descripcion y ejemplo

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
sherckuith
Date:
Tue Apr 03 21:03:00 2012 +0000
Commit message:
realloc, descripcion y ejemplo

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Apr 03 21:03:00 2012 +0000
@@ -0,0 +1,36 @@
+#include "mbed.h"
+/* realloc example: rememb-o-matic */
+#include <stdio.h>
+#include <stdlib.h>
+
+int main ()
+{
+  int input,n;
+  int count=0;
+  int * numbers = NULL;
+  int * more_numbers;
+
+  do {
+     printf ("Enter an integer value (0 to end): ");
+     scanf ("%d", &input);
+     count++;
+
+     more_numbers = (int*) realloc (numbers, count * sizeof(int));
+
+     if (more_numbers!=NULL) {
+       numbers=more_numbers;
+       numbers[count-1]=input;
+     }
+     else {
+       free (numbers);
+       puts ("Error (re)allocating memory");
+       exit (1);
+     }
+  } while (input!=0);
+
+  printf ("Numbers entered: ");
+  for (n=0;n<count;n++) printf ("%d ",numbers[n]);
+  free (numbers);
+
+  return 0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Apr 03 21:03:00 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/737756e0b479