#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Allocation of array size dynamically


    can't we allocate arraysize dynamically?
    i have two compilers devc++ and ms visualstudio.when i compiled the below program in these two compilers ,dev c++ allowed dynamic allocation of array whereas ms visual studio raised an error
    i.e unknown size of the array.
    Why it raised this sort of error.
    The program is
    Code:
    #include<stdio.h>
    #include<conio.h>
    
        void change_it(int a)
        {
             a = 555;
    }
    /*bubble sort*/
    void bubblesort(int bubble[],int n)
    {
         
        int inc,inc1;
         for(inc=0;inc<n;inc++)
                     {
                        printf("%d",bubble[inc]);
                        }
        for(inc=0;inc<n;inc++)
        {
             for(inc1=(n-1);inc1>inc;inc1--)
             {
                if(bubble[inc1]<bubble[inc1-1])
                {
                     int temp;
                     temp = bubble[inc1];
                     bubble[inc1]=bubble[inc1-1];
                     bubble[inc1-1] = temp;
                     }
             }
    }
                     for(inc=0;inc<n;inc++)
                     {
                        printf("%d",bubble[inc]);
                        }                       
        
        
        
    }
    
     
    int main(void)
    {
    
        int a =1,n,bubble[n],inc;
    /*    void change_it(int);*/
        printf("%d\n",a);
        printf("enter the value of n i.e the size of array\n");
        scanf("%d",&n);
        printf("Enter tihe values in the Array");
        for(inc=0;inc<n;inc++)
        {
                scanf("%d",&bubble[inc]);
                }
        change_it(a);
        bubblesort(bubble,n);
        printf("%d\n",a);
        getch();
        return 0;
        }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,381
    Rep Power
    1871
    > can't we allocate arraysize dynamically?

    > int a =1,n,bubble[n],inc;
    First of all, the value of n is garbage, so your array length is also garbage. The array length isn't revised automatically whenever you input a new value for n.

    Second, variable length arrays such as this were introduced in C99. dev-c++ (which uses gcc, supports C99 (and did so as an extension prior to C99).

    Visual studio does not support C99, and so it's a syntax error.

    If you want something portable, then do this.
    Code:
    int *bubble;
    ...
        printf("enter the value of n i.e the size of array\n");
        scanf("%d",&n);
        bubble = malloc( n * sizeof(*bubble) );
    ...// do your thing
        free(bubble);

    Comments on this post

    • Technovicez agrees
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,155
    Rep Power
    2222
    I don't see where you ever call malloc or calloc to allocate any array dynamically. Just how do you think that you're allocating an array dynamically?

    I can't see how MinGW gcc (the compiler that Dev-C++ uses) would have compiled that. Unless it's one of those weird C99 things. MS Visual Studio doesn't support C99, because C99 mostly just tries to bring C up-to-date with C++ and Microsoft is investing in C++ and supports ANSI C (AKA "C89") which is what everybody uses.

    Comments on this post

    • Technovicez agrees
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0
    Thanks a Ton.... :)
    Sir U Specified that
    [highlight]
    "The array length isn't revised automatically whenever you input a new value for n."
    [\highlight]
    how to know about this aspect, i mean any resources to know when array is allocated memory whether is it is revised automatically .Not only this in general all datastructures.
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,381
    Rep Power
    1871
    Originally Posted by Technovicez
    how to know about this aspect, i mean any resources to know when array is allocated memory whether is it is revised automatically .Not only this in general all datastructures.
    1. Investigate realloc while you're reading about malloc.

    2. Use C++ containers
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo