Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Redundant statement in the Standard?

Reply
Thread Tools

Redundant statement in the Standard?

 
 
Army1987
Guest
Posts: n/a
 
      09-19-2007
On Tue, 18 Sep 2007 19:34:43 -0400, CBFalconer wrote:
> You can't have an array of arrays. You can have an array of
> pointers to an array.


#include <stdio.h>
int main(void)
{
char a[20][20];
char ((*b[20])[20];
printf("%lu %lu\n", (unsigned long)sizeof a,
(unsigned long)sizeof b);
return 0;
}

--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      09-19-2007
Keith Thompson wrote:
> CBFalconer <> writes:
>> Richard Bos wrote:
>>> Michal Nazarewicz <> wrote:

>> ... snip ...
>>>>
>>>> No, I don't think that's the case. arr[i++] is not a VLA -- it's
>>>> a character hence sizeof would return 1 and there will be no side
>>>> effects.
>>>
>>> Read again. arr is an array of VLAs, so arr[i++] is the i'th VLA.
>>> arr[i++][4] would be a char.

>>
>> You can't have an array of arrays.

>
> Incorrect, you most certainly can.


The original was dealing with an array of VLAs. I omitted the VLA
word in my comment.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
 
Old Wolf
Guest
Posts: n/a
 
      09-19-2007
On Sep 19, 11:34 am, CBFalconer <cbfalco...@yahoo.com> wrote:
> You can't have an array of arrays. You can have an array of
> pointers to an array.


Is this the real CBFalconer?


 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      09-19-2007
CBFalconer <> writes:

> Richard Bos wrote:
>> Michal Nazarewicz <> wrote:
>>

> ... snip ...
>>>
>>> No, I don't think that's the case. arr[i++] is not a VLA -- it's
>>> a character hence sizeof would return 1 and there will be no side
>>> effects.

>>
>> Read again. arr is an array of VLAs, so arr[i++] is the i'th VLA.
>> arr[i++][4] would be a char.

>
> You can't have an array of arrays. You can have an array of
> pointers to an array.


What total and utter nonsense. Another example of the c.l.c word games
at their very worst.

int multi[ROWS][COLS];

>
> --
> Chuck F (cbfalconer at maineline dot net)
> Available for consulting/temporary embedded and systems.
> <http://cbfalconer.home.att.net>

 
Reply With Quote
 
=?iso-2022-kr?q?Harald_van_D=0E=29=26=0Fk?=
Guest
Posts: n/a
 
      09-19-2007
On Wed, 19 Sep 2007 18:35:50 +0200, Richard wrote:
> CBFalconer <> writes:
>> You can't have an array of arrays. You can have an array of pointers
>> to an array.

>
> What total and utter nonsense. Another example of the c.l.c word games
> at their very worst.
>
> int multi[ROWS][COLS];


It doesn't work as a word game. What you declared is an array of arrays.
multi[0], in most contexts, will be converted to a pointer, but that
doesn't mean it is one. The original claim is simply wrong.
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      09-19-2007
Old Wolf <> writes:

> On Sep 19, 11:34 am, CBFalconer <cbfalco...@yahoo.com> wrote:
>> You can't have an array of arrays. You can have an array of
>> pointers to an array.

>
> Is this the real CBFalconer?


It would appear to be from the accuracy of his statements.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-20-2007
CBFalconer <> writes:
> Keith Thompson wrote:
>> CBFalconer <> writes:
>>> Richard Bos wrote:
>>>> Michal Nazarewicz <> wrote:
>>> ... snip ...
>>>>>
>>>>> No, I don't think that's the case. arr[i++] is not a VLA -- it's
>>>>> a character hence sizeof would return 1 and there will be no side
>>>>> effects.
>>>>
>>>> Read again. arr is an array of VLAs, so arr[i++] is the i'th VLA.
>>>> arr[i++][4] would be a char.
>>>
>>> You can't have an array of arrays.

>>
>> Incorrect, you most certainly can.

>
> The original was dealing with an array of VLAs. I omitted the VLA
> word in my comment.


I think you're still mistaken. As far as I can tell, VLAs of VLAs are
permitted.

The following program:

#include <stdio.h>
int main(void)
{
int n = 10;
char arr[n][n];
printf("sizeof arr[0] = %d\n", (int)sizeof arr[0]);
printf("sizeof arr = %d\n", (int)sizeof arr);
return 0;
}

is accepted without complaint by gcc, icc, and Sun's C compiler under
Solaris 10, and produces the output:

sizeof arr[0] = 10
sizeof arr = 100

(which doesn't actually prove anything), and I don't see any such
restriction in C99 6.7.5.2 (which doesn't really prove anything
either; I might have missed something).

It was also accepted without complaint by Comeau's test drive page,
<http://www.comeaucomputing.com/pcgi-bin/compiler.html>, in strict C99
mode (though that page doesn't give you output).

Can you quote any wording from the standard that forbids VLAs of VLAs?

--
Keith Thompson (The_Other_Keith) kst- <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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-20-2007
Keith Thompson wrote:
> CBFalconer <> writes:
>> Keith Thompson wrote:
>>> CBFalconer <> writes:
>>>

.... snip ...
>>>>
>>>> You can't have an array of arrays.
>>>
>>> Incorrect, you most certainly can.

>>
>> The original was dealing with an array of VLAs. I omitted the VLA
>> word in my comment.

>
> I think you're still mistaken. As far as I can tell, VLAs of VLAs
> are permitted.


I differ. Arrays need to be composed of equal sized objects.
VLAs, by nature, are variable sized objects. You could have an
array of pointers to VLAs, though.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      09-20-2007
>Keith Thompson wrote:
>>As far as I can tell, VLAs of VLAs are permitted.


(They are, necessarily so.)

In article <>,
CBFalconer <> wrote:
>I differ. Arrays need to be composed of equal sized objects.
>VLAs, by nature, are variable sized objects.


This is the tricky part: while VLAs are "variable length arrays",
each *instance* of any given VLA is fixed-size.

VLAs of VLAs are necessary because one of the goals, perhaps even
the primary goal, of VLAs in the first place was to allow C
programmers to write Fortran-like functions with Fortran-like
convenience, e.g.:

/* perform some operation on a matrix of "double" */
void mat_operate(size_t m, size_t n, double mat[m][n]) {
size_t i, j;

for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
... operate on mat[i][j] ...
}

When mat_operate() is called, m and n are variable, but while
mat_operate() operates, m and n remain fixed. The size of the
matrix "mat" is also fixed.

(This remains true even if m and/or n are modified inside mat_operate().
The size is, in effect, "captured" at the point the VLA is created.)

The key sentences in my draft read:

The size of each instance of a variable length array type
does not change during its lifetime.

and (in part):

... the size of the variable length array type does not change
if the value of n is subsequently changed.

>You could have an array of pointers to VLAs, though.


You can indeed have this, as well. You can also have a VLA of
pointers to VLAs, and so on.

The compile-time compatibility rules for pointers to VLAs are
quite relaxed, with the effect at runtime being undefined if the
sizes of the variably-modified types do not match:

void f(size_t n, double square_mat[n][n]) {
double (*p)[rand()]; /* bad idea, for illustration only */

p = square_mat; /* not a compile-time error */
...
}

The effect is undefined unless n happens to be equal to the
value rand() returns. In practice, in a real C compiler, if
rand() returns a number that does not match n, p[0][j] "works
right" but p[i][j] misbehaves: If we were to capture the
number that came out of the rand() call, e.g., via:

size_t x;
double (*p)[x = rand()];

instead, then p[i][j] is roughly equivalent to mat[0][i * x + j]
as long as no subscript checking occurs. (Of course, because of
the undefined behavior, the compiler can make various assumptions
and optimize this particular weirdness into *other* weirdness.
For instance, it might make the assumption that x must necessarily
equal n, hence remove x from the runtime image and use the saved
n -- so that p[i][j] accesses mat[i][j] after all!)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-20-2007
CBFalconer <> writes:
> Keith Thompson wrote:
>> CBFalconer <> writes:
>>> Keith Thompson wrote:
>>>> CBFalconer <> writes:
>>>>

> ... snip ...
>>>>>
>>>>> You can't have an array of arrays.
>>>>
>>>> Incorrect, you most certainly can.
>>>
>>> The original was dealing with an array of VLAs. I omitted the VLA
>>> word in my comment.

>>
>> I think you're still mistaken. As far as I can tell, VLAs of VLAs
>> are permitted.

>
> I differ. Arrays need to be composed of equal sized objects.
> VLAs, by nature, are variable sized objects. You could have an
> array of pointers to VLAs, though.


You snipped the part where I asked you to provide a citation from the
standard to support your claim. You also snipped the example
demonstrating a VLA of VLAs.

The size of a VLA is fixed at runtime when it's created, and all the
elements of an array of VLAs must have the same size.

If you have a specific reason to think this isn't allowed, I'd love to
hear it (and somebody should submit bug reports against the four
different compilers that didn't complain about VLAs of VLAs).

--
Keith Thompson (The_Other_Keith) kst- <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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if statement that, when false, skips first statement in its block, executes second? Jay McGavren Java 11 01-16-2006 05:49 PM
How do I do a conditional statement in a constant statement? tkvhdl@gmail.com VHDL 3 12-16-2005 06:13 PM
redundant switches / redundant server NICs Stuart Kendrick Cisco 4 08-10-2004 08:54 PM
exec "statement" VS. exec "statement in globals(), locals() Ted Python 1 07-22-2004 08:51 AM
exec "statement" VS. exec "statement" in globals(), locals() tedsuzman Python 2 07-21-2004 08:41 PM



Advertisments