Angel David Yaguana Hernandez / Mbed 2 deprecated CPP_realloc

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 /* realloc example: rememb-o-matic */
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 
00006 int main ()
00007 {
00008   int input,n;
00009   int count=0;
00010   int * numbers = NULL;
00011   int * more_numbers;
00012 
00013   do {
00014      printf ("Enter an integer value (0 to end): ");
00015      scanf ("%d", &input);
00016      count++;
00017 
00018      more_numbers = (int*) realloc (numbers, count * sizeof(int));
00019 
00020      if (more_numbers!=NULL) {
00021        numbers=more_numbers;
00022        numbers[count-1]=input;
00023      }
00024      else {
00025        free (numbers);
00026        puts ("Error (re)allocating memory");
00027        exit (1);
00028      }
00029   } while (input!=0);
00030 
00031   printf ("Numbers entered: ");
00032   for (n=0;n<count;n++) printf ("%d ",numbers[n]);
00033   free (numbers);
00034 
00035   return 0;
00036 }