473,388 Members | 1,340 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Array size in run time

Hi, friends.

I've seen the following code in an example:

int fun(int n)
{
int i, x[n];

for (i = 0; i < n; i++)
x[i] = 5;

for (i = 0; i < n; i++)
printf("%i\n", x[i]);
}

main()
{
fun(10);
}
As you can see, array x size depends on the argument of the function fun.
I thought this was not possible as local variables are located in the
stack, and the compiler should know what size they are going to take.

I have compiled the code and seems to work ok, but I don't know if it's
correct and will always run ok.

Is it possible to make this kind of things? If it is, is
it better to work like that rather than using malloc()?

Thanks a lot.
Feb 14 '07 #1
7 1901
In article <pa****************************@yahoo.com>,
Heriberto Bens <pe*******@yahoo.comwrote:
>I've seen the following code in an example:
>int fun(int n)
{
int i, x[n];
for (i = 0; i < n; i++)
x[i] = 5;
for (i = 0; i < n; i++)
printf("%i\n", x[i]);
}
main()
{
fun(10);
}
>As you can see, array x size depends on the argument of the function fun.
I thought this was not possible as local variables are located in the
stack, and the compiler should know what size they are going to take.
C99 VLA (Variable Length Arrays). Legal in C99, not legal in C89.
See also "the struct hack" for something that works in a number of
C89 implementations.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Feb 14 '07 #2
On Feb 14, 7:20 am, Heriberto Bens <peekpo...@yahoo.comwrote:
Hi, friends.

I've seen the following code in an example:

int fun(int n)
{
int i, x[n];

for (i = 0; i < n; i++)
x[i] = 5;

for (i = 0; i < n; i++)
printf("%i\n", x[i]);

}

main()
{
fun(10);

}

As you can see, array x size depends on the argument of the function fun.
I thought this was not possible as local variables are located in the
stack, and the compiler should know what size they are going to take.

I have compiled the code and seems to work ok, but I don't know if it's
correct and will always run ok.

Is it possible to make this kind of things? If it is, is
it better to work like that rather than using malloc()?

Thanks a lot.
I do not think that it is possible with the ANSI C language this will
make the stack handling complicated ...

Feb 14 '07 #3
In article <11**********************@j27g2000cwj.googlegroups .com>,
<ro*********@gmail.comwrote:
>On Feb 14, 7:20 am, Heriberto Bens <peekpo...@yahoo.comwrote:
>int fun(int n)
{
int i, x[n];
[...]
>}
>As you can see, array x size depends on the argument of the function fun.
>I do not think that it is possible with the ANSI C language this will
make the stack handling complicated ...
You are correct if by "ANSI C" you mean specifically
X3.159-1989, also known as C89. Some sections of that ANSI standard
were renumbered and the result was adopted by ISO in 1990, and
(if I recall correctly) ANSI then adopted the ISO version. I believe
(possibly incorrectly) that the next major C standard, known usually
as C99, is an ISO standard, and possibly not an ANSI standard.

X3.159-1989 does not know about variable length arrays (VLAs).
ISO C99 *does* know about VLAs.

--
All is vanity. -- Ecclesiastes
Feb 14 '07 #4
ro*********@gmail.com wrote, On 14/02/07 16:56:
On Feb 14, 7:20 am, Heriberto Bens <peekpo...@yahoo.comwrote:
>Hi, friends.

I've seen the following code in an example:

int fun(int n)
{
int i, x[n];
<snip>

Either declare the function as void or return the int you have said it
returns.
>}

main()
Implicit int is not allowed in C99, the latest version of the C
standard. You have to be explicit about the return type.

int main(void)

It is also better, but not required, to be explicit about not taking
parameters.
>{
fun(10);
The return type of main is int therefore it is best to return an int.
return 0;
>}

As you can see, array x size depends on the argument of the function fun.
I thought this was not possible as local variables are located in the
stack, and the compiler should know what size they are going to take.

I have compiled the code and seems to work ok, but I don't know if it's
correct and will always run ok.

Is it possible to make this kind of things? If it is, is
it better to work like that rather than using malloc()?

Thanks a lot.

I do not think that it is possible with the ANSI C language this will
make the stack handling complicated ...
You should have read Walter's reply before posting especially as it was
sitting there on Google (through which you posted) for you to read. C99
*does* allow Variable Length Arrays (VLAs). Since ANSI (America) adopted
the ISO (International) standard this qualifies as ANSI C. Although as
there is an international standard and you are on an international group
is might be more appropriate to refer to ISO C.
--
Flash Gordon
Feb 14 '07 #5
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <11**********************@j27g2000cwj.googlegroups .com>,
<ro*********@gmail.comwrote:
>>On Feb 14, 7:20 am, Heriberto Bens <peekpo...@yahoo.comwrote:
>>int fun(int n)
{
int i, x[n];
[...]
>>}
>>As you can see, array x size depends on the argument of the function fun.
>>I do not think that it is possible with the ANSI C language this will
make the stack handling complicated ...

You are correct if by "ANSI C" you mean specifically
X3.159-1989, also known as C89. Some sections of that ANSI standard
were renumbered and the result was adopted by ISO in 1990, and
(if I recall correctly) ANSI then adopted the ISO version. I believe
(possibly incorrectly) that the next major C standard, known usually
as C99, is an ISO standard, and possibly not an ANSI standard.
I believe ANSI adopted the ISO C99 standard. Officially the older
C89/C90 standards are obsolete (though of course they're still very
relevant in the real world).

So strictly speaking, "ANSI C" refers to C99, though it's clearer to
refer to it as "ISO C". Due to historical practice, though, "ANSI C"
commonly refers to the language defined by the (obsolete) 1989
standard (which is identical to the language defined by the 1990
standard). It's best to refer specifically to C89, C90, or C99.
X3.159-1989 does not know about variable length arrays (VLAs).
ISO C99 *does* know about VLAs.
Yes.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 14 '07 #6
On Wed, 14 Feb 2007 10:47:36 -0800, Keith Thompson <ks***@mib.org>
wrote:
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
You are correct if by "ANSI C" you mean specifically
X3.159-1989, also known as C89. Some sections of that ANSI standard
were renumbered and the result was adopted by ISO in 1990, and
(if I recall correctly) ANSI then adopted the ISO version. I believe
(possibly incorrectly) that the next major C standard, known usually
as C99, is an ISO standard, and possibly not an ANSI standard.

I believe ANSI adopted the ISO C99 standard. Officially the older
C89/C90 standards are obsolete (though of course they're still very
relevant in the real world).
ANSI definitely did adopt it, but not until about May 2000; I remember
seeing the announcement, and the 'ANSI' labelled version being added
to their webstore, at much lower cost than the ISO-only version.

Actually, to be technical, I believe it was adopted on ANSI's behalf
by the committee for computing, NCITS, which as far as I can see was
just a tweak on The Accredited Standards Committee Formerly Known As
X3, and has now changed again to _I_NCITS*. Similarly I believe it was
really JTC1SC22 which adopted 9899, on behalf of ISO and IEC.

(* Meanwhile ASC X9, banking, goes merrily on as X9. Apparently when
you have gobs of other people's money clutched in your hot little
hands, you don't worry so much about glitzing up your public image.)
So strictly speaking, "ANSI C" refers to C99, though it's clearer to
refer to it as "ISO C". Due to historical practice, though, "ANSI C"
commonly refers to the language defined by the (obsolete) 1989
standard (which is identical to the language defined by the 1990
standard). It's best to refer specifically to C89, C90, or C99.
Or C95, if you are concerned about the wchar/i18n enhancements.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Feb 26 '07 #7
On Feb 14, 7:20 am, Heriberto Bens <peekpo...@yahoo.comwrote:
Hi, friends.

I've seen the following code in an example:

int fun(int n)
{
int i, x[n];

for (i = 0; i < n; i++)
x[i] = 5;

for (i = 0; i < n; i++)
printf("%i\n", x[i]);

}

main()
{
fun(10);

}

As you can see, array x size depends on the argument of the function fun.
I thought this was not possible as local variables are located in the
stack, and the compiler should know what size they are going to take.

I have compiled the code and seems to work ok, but I don't know if it's
correct and will always run ok.
as many of here as already replied, its correct as per c99, but not as
per c88.

Upto my knowledge(not 100 % confirmed, but you may need to digout)
"Will always run OK" is NOT Guranted,

if you are not writing portable code and you stick to any particular
implementation of compiler you may need to ask your vendor.

if you are writing portable code for a,b,c,d compiler you may need to
refer all the vendor supports this feature or not.?

For gcc i found this link,
http://gcc.gnu.org/c99status.html
It clearly shows me variable-length arrays "Broken" status.
>
Is it possible to make this kind of things? If it is, is
it better to work like that rather than using malloc()?
It is possible if the compiler/platform vendor support the feature.

Or I think malloc/related functions can be used in most cases.
>
Thanks a lot.
--Raxit

Feb 26 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
8
by: Hagen | last post by:
Hi, I have a question that you probably shouldn´t worry about since the compiler cares for it, but anyways: When you run your compiler with optimization turned on (eg. g++ with -Ox flag) and...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
8
by: Gerald | last post by:
I have a problem with an array of pointers. In a program I'm writing, I have to read a file, containing thousands of short lines. The content of another file will be compared against each line...
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
7
by: simkn | last post by:
Hello, I'm writing a function that updates an array. That is, given an array, change each element. The trick is this: I can't change any elements until I've processed the entire array. For...
7
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
18
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
4
by: frizzle | last post by:
Hi there, I have a function to create an array of all files in a certain folder, so i can display the structure. The actual function is below the message, as is an example of its output. As...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.