473,386 Members | 1,734 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,386 software developers and data experts.

Another silly question: array with vars

Hi again! I have ``yet another silly question'', about arrays this time.

I've looked through the FAQs in the array & memory sections without
finding an answer. I surely didn't look deep enough.

What does the standards (plural! C99 and pre-C99 standards) say about
variable-initialized arrays like in the following code?

int main(void)
{
int a = 6;
char s[a];
/* ... */
return 0;
}

If my memory doesn't fail, that is not pre-C99 compliant and resulting
in a compiler error, but I'm not sure... And does C99 state something
about the initializer ``int a''? I mean, what if it is NOT initialized?

I know, curiosity killed the cat :)

--
Sensei <senseiwa@Apple's mail>

Research (n.): a discovery already published by a chinese guy one month
before you, copying a russian who did it in the 60s.

Jan 22 '07 #1
11 1428

"Sensei" <senseiwa@apple's mail.comwrote in message
news:45***********************@reader1.news.tin.it ...
Hi again! I have ``yet another silly question'', about arrays this time.

I've looked through the FAQs in the array & memory sections without
finding an answer. I surely didn't look deep enough.

What does the standards (plural! C99 and pre-C99 standards) say about
variable-initialized arrays like in the following code?

int main(void)
{
int a = 6;
char s[a];
/* ... */
return 0;
}

If my memory doesn't fail, that is not pre-C99 compliant and resulting in
a compiler error, but I'm not sure...
That's correct. The array's size must be specified
by a constant expression. In the code above, 'a' is not.
And does C99 state something about the initializer ``int a''?
'int a' is not an initializer, it's a declaration (as well
as a definition).
>I mean, what if it is NOT initialized?
If an object is not initialized or assigned a valid value, evaluating
its value in any context produces undefined behavior.

-Mike
Jan 22 '07 #2
Mike Wahler wrote:
"Sensei" <senseiwa@apple's mail.comwrote in message
news:45***********************@reader1.news.tin.it ...
I mean, what if it is NOT initialized?

If an object is not initialized or assigned a valid value, evaluating
its value in any context produces undefined behavior.
Actually, C99 changed the rules on that. The value of an uninitialised
object is indeterminate. An indeterminate value may be either an
unspecified but valid value, or a trap representation. If the type has
no trap representations, there is no possible undefined behaviour in
reading the object.

Jan 22 '07 #3
Sensei <senseiwa@apple's mail.comwrites:
[...]
What does the standards (plural! C99 and pre-C99 standards) say about
variable-initialized arrays like in the following code?

int main(void)
{
int a = 6;
char s[a];
/* ... */
return 0;
}

If my memory doesn't fail, that is not pre-C99 compliant and resulting
in a compiler error, but I'm not sure... And does C99 state something
about the initializer ``int a''? I mean, what if it is NOT initialized?
s is a variable *length* array, not a variable initialized array; it's
also referred to as a VLA.

VLAs were introduced in C99. C90 does not support them.

If "a" were not initialized, any attempt to access its value,
including using it in a VLA declaration, would invoke undefined
behavior.

--
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.
Jan 22 '07 #4
Keith Thompson <ks***@mib.orgwrites:
Sensei <senseiwa@apple's mail.comwrites:
[...]
What does the standards (plural! C99 and pre-C99 standards) say about
variable-initialized arrays like in the following code?

int main(void)
{
int a = 6;
char s[a];
/* ... */
return 0;
}

If my memory doesn't fail, that is not pre-C99 compliant and resulting
in a compiler error, but I'm not sure... And does C99 state something
about the initializer ``int a''? I mean, what if it is NOT initialized?

s is a variable *length* array, not a variable initialized array; it's
also referred to as a VLA.

VLAs were introduced in C99. C90 does not support them.

If "a" were not initialized, any attempt to access its value,
including using it in a VLA declaration, would invoke undefined
behavior.
As Harald points out, accessing an int object probably doesn't invoke
UB if int has no trap representations (and it's possible for a program
to prove that it doesn't by looking at CHAR_BIT*sizeof(int), INT_MIN,
and INT_MAX). But it's still a Very Bad Idea. In this case, it's
likely to result in either undefined behavior if "a" happens to be
negative, or stack overflow^H^H^H^H^H^H^H^H^H^H^H^H^H^H an attempt to
create an object whose size exceeds implementation limits (and, again,
undefined behavior) if the value of "a" happens to be large.

And let me issue a plea to avoid the use of "a" as an identifier in
sample code, or of any other identifier that can easily be confused
with an English word. "x" and "s" are ok.

--
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.
Jan 22 '07 #5
On 2007-01-22 10:14:31 +0100, "Harald van Dijk" <tr*****@gmail.comsaid:
Mike Wahler wrote:
>"Sensei" <senseiwa@apple's mail.comwrote in message
news:45***********************@reader1.news.tin.i t...
>>I mean, what if it is NOT initialized?

If an object is not initialized or assigned a valid value, evaluating
its value in any context produces undefined behavior.

Actually, C99 changed the rules on that. The value of an uninitialised
object is indeterminate. An indeterminate value may be either an
unspecified but valid value, or a trap representation. If the type has
no trap representations, there is no possible undefined behaviour in
reading the object.
Then

int i;
int array[i];

does not invoke an UB, but does C99 say anything about the actual
storage? From some trials I'd say that compilers usually allocate a
certain amount of memory, but I don't know if this is against the
standard or not.

--
Sensei <senseiwa@Apple's mail>

Research (n.): a discovery already published by a chinese guy one month
before you, copying a russian who did it in the 60s.

Jan 23 '07 #6
On 2007-01-22 23:09:42 +0100, Keith Thompson <ks***@mib.orgsaid:
>s is a variable *length* array, not a variable initialized array; it's
also referred to as a VLA.

VLAs were introduced in C99. C90 does not support them.

If "a" were not initialized, any attempt to access its value,
including using it in a VLA declaration, would invoke undefined
behavior.

As Harald points out, accessing an int object probably doesn't invoke
UB if int has no trap representations (and it's possible for a program
to prove that it doesn't by looking at CHAR_BIT*sizeof(int), INT_MIN,
and INT_MAX). But it's still a Very Bad Idea. In this case, it's
likely to result in either undefined behavior if "a" happens to be
negative, or stack overflow^H^H^H^H^H^H^H^H^H^H^H^H^H^H an attempt to
create an object whose size exceeds implementation limits (and, again,
undefined behavior) if the value of "a" happens to be large.
One more thing, weren't simple types as int, char, and so on
initialized to 0 with C99? I remember reading this somewhere, and of
course I might be completely wrong.
And let me issue a plea to avoid the use of "a" as an identifier in
sample code, or of any other identifier that can easily be confused
with an English word. "x" and "s" are ok.
Ok, I'll do that :)

--
Sensei <senseiwa@Apple's mail>

Research (n.): a discovery already published by a chinese guy one month
before you, copying a russian who did it in the 60s.

Jan 23 '07 #7
Sensei wrote:
>
One more thing, weren't simple types as int, char, and so on initialized
to 0 with C99? I remember reading this somewhere, and of course I might
be completely wrong.
Not for automatic objects, the value is indeterminate. Maybe you where
thinking of compound initialisers?

--
Ian Collins.
Jan 23 '07 #8
Sensei wrote:
On 2007-01-22 10:14:31 +0100, "Harald van Dijk" <tr*****@gmail.comsaid:
>Mike Wahler wrote:
>>"Sensei" <senseiwa@apple's mail.comwrote in message
news:45***********************@reader1.news.tin. it...

I mean, what if it is NOT initialized?
If an object is not initialized or assigned a valid value, evaluating
its value in any context produces undefined behavior.


Actually, C99 changed the rules on that. The value of an uninitialised
object is indeterminate. An indeterminate value may be either an
unspecified but valid value, or a trap representation. If the type has
no trap representations, there is no possible undefined behaviour in
reading the object.


Then

int i;
int array[i];

does not invoke an UB, but does C99 say anything about the actual
storage? From some trials I'd say that compilers usually allocate a
certain amount of memory, but I don't know if this is against the
standard or not.
Well considering i can hold any value, it's pot luck how big a array you
get and due to the lack of error reporting with VLAs, attempting to use
the array may do odd things.

--
Ian Collins.
Jan 23 '07 #9
On 2007-01-23 09:27:23 +0100, Ian Collins <ia******@hotmail.comsaid:
Sensei wrote:
>>
One more thing, weren't simple types as int, char, and so on initialized
to 0 with C99? I remember reading this somewhere, and of course I might
be completely wrong.
Not for automatic objects, the value is indeterminate. Maybe you where
thinking of compound initialisers?
Probably I'm just confusing the two. Thanks, you've been really clear!

--
Sensei <senseiwa@Apple's mail>

Research (n.): a discovery already published by a chinese guy one month
before you, copying a russian who did it in the 60s.

Jan 23 '07 #10


On Jan 22, 4:20 pm, Sensei <senseiwa@apple's mail.comwrote:
Hi again! I have ``yet another silly question'', about arrays this time.

I've looked through the FAQs in the array & memory sections without
finding an answer. I surely didn't look deep enough.

What does the standards (plural! C99 and pre-C99 standards) say about
variable-initialized arrays like in the following code?

int main(void)
{
int a = 6;
char s[a];
/* ... */
return 0;

}If my memory doesn't fail, that is not pre-C99 compliant and resulting
in a compiler error, but I'm not sure... And does C99 state something
about the initializer ``int a''? I mean, what if it is NOT initialized?

I know, curiosity killed the cat :)

--
Sensei <senseiwa@Apple's mail>

Research (n.): a discovery already published by a chinese guy one month
before you, copying a russian who did it in the 60s.

i think (... i'm sure) you HVAE TO use a 'constant' to specify the size
of a array, because the memory space of the array will be allocate
before the program start to run, and at that time, the value of your
variable 'a' is not known yet.

Jan 24 '07 #11
"HyperCube" <co********@gmail.comwrites:
On Jan 22, 4:20 pm, Sensei <senseiwa@apple's mail.comwrote:
Hi again! I have ``yet another silly question'', about arrays this time.

I've looked through the FAQs in the array & memory sections without
finding an answer. I surely didn't look deep enough.

What does the standards (plural! C99 and pre-C99 standards) say about
variable-initialized arrays like in the following code?

int main(void)
{
int a = 6;
char s[a];
/* ... */
return 0;

}If my memory doesn't fail, that is not pre-C99 compliant and resulting
in a compiler error, but I'm not sure... And does C99 state something
about the initializer ``int a''? I mean, what if it is NOT initialized?
[...]
>
i think (... i'm sure) you HVAE TO use a 'constant' to specify the size
of a array, because the memory space of the array will be allocate
before the program start to run, and at that time, the value of your
variable 'a' is not known yet.
Nope.

The array "s" is inside a function, so memory for it is allocated when
the function is called, not on program startup. The distinction is a
subtle one for main(), but it still aplies.

C99 supports variable-length arrays (VLAs).

This has been mentioned in this thread.

--
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.
Jan 24 '07 #12

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

Similar topics

2
by: AJ | last post by:
Ok, I have a "form" class, which I am going to use to generate HTML forms. I also have a form_input, which I use to generate the individual HTML input elements. What I want to do is some...
2
by: jerrygarciuh | last post by:
Hello, In this sample script I create an array of objects. Print out their data with print_r().Update their data with a sub called v_set(). Print out data showing the chnages using print_r(). I...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
3
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
0
by: Kurt Lange | last post by:
no... the array is created dynamically. and no... that defeats the purpose of what im trying todo.. encapsulate all initializing of variables in base class... derive from it... by deriving...
188
by: christopher diggins | last post by:
I have posted a C# critique at http://www.heron-language.com/c-sharp-critique.html. To summarize I bring up the following issues : - unsafe code - attributes - garbage collection -...
3
by: iulian.ilea | last post by:
Hi, How can I count how many variables are in an array. I give an example to make my self clear enough: $arr="fds"; $arr="fdfs"; $arr=null; $arr="213"; count($arr) will return 3. I...
4
by: Man-wai Chang | last post by:
I knew the GET way ('called.php?para1=aaa&para2=xxxx'), but how about the POST way? -- iTech Consulting Services Limited Expert of ePOS solutions Website: http://www.itech.com.hk (IE only)...
10
by: r035198x | last post by:
There are a lot of common C mistakes. I have put together the ones I consider to be the silliest made by C programmers. These are usually the source of the popular C programming tears and remembering...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.