Thread: Dynamically Allocated Array

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    8

    Dynamically Allocated Array

    My problem is that the dynamically allocated array only prints correctly within the function read_array. I can't figure out where I'm going wrong.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define MAX_SIZE 1000
    
    int read_array(int *array,int size);
    
    int main (void)
    {
      int size;
      int *array;
    
      size = read_array(array, size);
      printf("\n");
      for (int i = 0; i < size; i++)
            printf("%d ", *(array+i));
    
      return 0;
    }
    
    int read_array(int *array,int size)
    {
       #define FLUSH while (getchar() != '\n')
       array = (int*)calloc(MAX_SIZE, sizeof(int));
    
       printf("You may enter up to 1000 integers.\n"
             "How many would you like to enter?\n");
    
       while (scanf("%d", &size)!= 1)
       {
          FLUSH;
          printf("Invalid number. Try again.");
       }
    
       if (size > 1000)
          size = 1000;
    
       array = (int*)realloc(array, (size - 1) * sizeof(int));
    
       printf("Enter your numbers: \n");
       for (int i = 0; i < size; i++)
          scanf("%d", array[i]);
    
       for (int i = 0; i < size; i++)
          printf("%d ", array[i]);
    
       return size;
    }
    This is the output:
    You may enter up to 1000 integers.
    How many would you like to enter?
    4
    Enter your numbers:
    1 2 3 4
    1 2 3 4
    2968 1 0 0

    Why am I getting 2968 1 0 0 instead of 1 2 3 4?
    The only reason I can think of is that you need a pointer to get dynamically allocated data, but "array" is a pointer. So wheres the problem?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Don't calloc the array at the top then realloc half way through... Use calloc() once after you know the size.
    The correct call is not size - 1 ... it is array = calloc(size,sizeof(int));

    C arrays begin at 0 and end at size -1 ... so if you tell it you want 4 elements, you get valid array indexes of 0, 1, 2 and 3 ... that's four elements, go ahead count them...

    Also if you are typing all four numbers on one line, your scanf() is going to take the first one and ignore the rest.
    Try entering your numbers one at a time... 1 <enter> 2<enter> 3<enter> and see what you get.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    8
    Its required that I initialize the array to 1000 first using calloc then realloc it to the size. I've changed realloc to - int * realloc (int* array, int size); instead of //array = (int*)realloc(array, (size - 1) * sizeof(int));

    Same results.

    As for entering numbers in 1 at a time, I know. Any other suggestions? I still don't see why I would have different outputs in the function and then different outside of it.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    8
    Thanks that helped Bayint Naung.

    Heres how I fixed it...

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define MAX_SIZE 1000
    
    int* read_array(int &size);
    void ptr_array (int size, int *pointer_array, int array[]);
    
    int main (void)
    {
      int size;
    
      int *array = read_array(size);
      printf("%d", size);
      //ptr_array(size, pointer_array, array);
      printf("\n");
      for (int i = 0; i < size; i++)
            printf("%d ", array[i]);
    
      return 0;
    }
    
    int* read_array(int &size)
    {
       int *array;
       #define FLUSH while (getchar() != '\n')
       array = (int*)calloc(MAX_SIZE, sizeof(int));
    
       printf("You may enter up to 1000 integers.\n"
             "How many would you like to enter?\n");
    
       scanf("%d", &size);
    
       if (size > 1000)
          size = 1000;
    
       //array = (int*)realloc(array, (size - 1) * sizeof(int));
       int * realloc (int* array, int size);
    
    
       printf("Enter your numbers: \n");
       for (int i = 0; i < size; i++)
          scanf("%d", &array[i]);
    
    
       return array;
    }
    
    void ptr_array (int size, int *pointer_array, int array[])
          {
          pointer_array = (int*)calloc(size, sizeof(int));
    
          printf("%d %d", &array, pointer_array);
    
          for (int i = 0; i < size; i++)
             pointer_array[i] = array[i];
    
          pointer_array[size] = NULL;
    
          for (int i = 0; i < size; i++)
             printf("%d ", pointer_array[i]);
    
          }

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Do you realize that you are not using C anymore?
    You are using reference which is C++. not C!

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by chris4 View Post
    Its required that I initialize the array to 1000 first using calloc then realloc it to the size.
    What on earth for... Really that's just wasted code serving no purpose whatsoever in your program.

    (If this requirement came from a teacher... you might want to look into changing classes or schools.)

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    Quote Originally Posted by Bayint Naung View Post
    Do you realize that you are not using C anymore?
    You are using reference which is C++. not C!
    I'm confused by this statement. When you say using reference, are you referring to passing by reference? How in the world does that change it from C to C++?

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Because there is no passing by reference in C.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    Quote Originally Posted by claudiu View Post
    Because there is no passing by reference in C.
    My bad...I was looking at his original code and didn't see he changed it to:

    int* read_array(int &size)

    I was really confused there for a second...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initializing a 2D array dynamically allocated
    By newboyc in forum C Programming
    Replies: 5
    Last Post: 02-01-2011, 01:08 PM
  2. Dynamically allocated array
    By dre in forum C Programming
    Replies: 17
    Last Post: 08-13-2009, 06:57 PM
  3. Dynamically Allocated Array
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-17-2007, 08:40 AM
  4. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  5. Deleting Dynamically Allocated Array Members
    By theJ89 in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2006, 07:37 PM