The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Allocation of array size dynamically
Discuss Allocation of array size dynamically in the C Programming forum on Dev Shed. Allocation of array size dynamically C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 10th, 2013, 11:53 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 27
Time spent in forums: 8 h 7 m 21 sec
Reputation 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;
}
|

February 10th, 2013, 02:13 PM
|
 |
Contributed User
|
|
|
|
> 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);
|

February 10th, 2013, 02:14 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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.
|

February 11th, 2013, 12:40 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 27
Time spent in forums: 8 h 7 m 21 sec
Reputation 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.
|

February 11th, 2013, 01:49 AM
|
 |
Contributed User
|
|
|
|
Quote: | 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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|