1

Im trying to write a program that asks the user for the size of an array and if they are asked again then the size of the array would increase based on the number they input.

int main()
{
   int size;
   int n;
   printf("Size of array: ");
   scanf("%d", &n);
   int *ptr = (int*)malloc(n*sizeof(int));
   size = n;
   int n1;  
   do {
       printf("Input the increase size of the array: ");
       scanf("%d", &n1);
       size += n1;
       int *ptr = realloc((size)*sizeof(int));
   } while (n1 != -1);

  return 0;
}


Here is what I have got but how can I move this into a function

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Kain
  • 329
  • 1
  • 10

5 Answers5

3

For starters this call of the function realloc

int *ptr = realloc((size)*sizeof(int));

is incorrect. The function expects two arguments.

If you want to write a function that changes the size of a dynamically allocated array then it can look the following way

int resize( int **a, size_t n )
{
    int *tmp = realloc( *a, n * sizeof( int ) );

    if ( tmp != NULL ) *a = tmp;

    return tmp != NULL;
} 

and the function can be called for example like

do {
    n1 = -1;
    printf("Input the increase size of the array: ");
    scanf("%d", &n1);
} while ( n1 != -1 && resize( &ptr, size += n1 ) );

    
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1
if ( scanf( "%d", &n1 ) == 1 )
{
  int *tmp = realloc( ptr, (size + n1) * sizeof *ptr );
  if ( tmp )
  {
    ptr = tmp;
    size += n1;
  }
}

If realloc cannot satisfy the request it will return NULL and leave the original buffer in place; if you assign the result back to ptr, you run the risk of losing your only reference to that memory. Therefore it's better to assign the result to a temporary pointer value and make sure it isn't NULL before updating ptr.

Also, you don't want to update your size until you know the realloc succeeded.

To put it into a function:

bool resize( int **ptr, int *size )
{
  int n1;
  bool success = false;

  printf( "Increase the size by: " );
  if ( scanf( "%d", &n1 ) == 1 )
  {
    int *tmp = realloc( *ptr, (*size + n1) * sizeof **ptr );
    if ( tmp )
    {
      *ptr = tmp;
      *size += n1;
      success = true;
    }
    else
    {
      fputs( "realloc could not extend the buffer\n", stderr );
    }
  }
  else
  {
    fputs( "Error or bad value on input\n", stderr );
  }
  return success;
}

and you would call it as

int main( void )
{
  int size = 0;
  int *ptr = NULL;
  ...
  if ( resize( &ptr, &size ) )
    // do stuff with ptr
  else
    // handle resize error
John Bode
  • 119,563
  • 19
  • 122
  • 198
1

If you just want to move the memory allocation into a function, you could do something like this:

int *allocate_array(int *ptr,size_t siz)
{
    return realloc(ptr,size * sizeof(int));
}

int main()
{
    int n, n1, size, *ptr = NULL;

    printf("Size of array: ");
    scanf("%d",&n);
    ptr = allocate_array(ptr,n);
    size = n;
    do {
        printf("Input the increase in the array size: ");
        scanf("%d",&n1);
        size += n1;
        ptr = allocate_array(ptr,size);
    } while (n1 > 0);
    return 0;
}

Note that your call to realloc was incorrect, as the first argument must be a pointer to the existing array. Also, you created a new ptr whose scope was just the while loop, which then ceased to exist outsid the loop.

SGeorgiades
  • 1,771
  • 1
  • 11
  • 11
0
  • You have to include corresponding headers to use the standard library.
  • You have to pass the old pointer to reallocate to realloc().
  • You have to give another name to the pointer variable to get the return value of realloc() not to shadow (hide) the variable to store the pointer to the array.
  • You should check the return values of malloc() and realloc() (and scanf() to check if they succeeded.
  • Casting results of malloc() family is considered as a bad practice.

Fixed code:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
   int size;
   int n;
   printf("Size of array: ");
   if (scanf("%d", &n) != 1) return 1;
   int *ptr = malloc(n*sizeof(int));
   if (ptr == NULL) return 1;
   size = n;
   int n1;  
   do {
       printf("Input the increase size of the array: ");
       if (scanf("%d", &n1) != 1) return 1;
       size += n1;
       int *new_ptr = realloc(ptr, (size)*sizeof(int));
       if (new_ptr == NULL) return 1;
       ptr = new_ptr;
   } while (n1 != -1);

  return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

You can use the realloc function for allocate the mamory again.

void *realloc(void *ptr, size_t size);