Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > ridiculous warning or not?

Reply
Thread Tools

ridiculous warning or not?

 
 
kerravon
Guest
Posts: n/a
 
      08-20-2012
Is this warning considered ridiculous?

I'll stop using that syntax if the compiler is
more correct than me.


% cat foo.c

char f[];



int main(void) { return (0); }



% cc foo.c

"foo.c", line 1: warning: null dimension: f




% cc -V

cc: Sun C 5.9 SunOS_sparc Patch 124867-01 2007/07/12

usage: cc [ options] files. Use 'cc -flags' for details

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      08-20-2012
On 8/20/2012 5:41 AM, kerravon wrote:
> Is this warning considered ridiculous?


Not by me.

> I'll stop using that syntax if the compiler is
> more correct than me.
>
> % cat foo.c
>
> char f[];


This line attempts to define an array. What is an array?
A collection of a fixed number of elements, all of the same type.
What kind of elements does this array contain? `char', it seems:
That's fine. How many? Um, er, ah, well, ...

Perhaps you intended `extern char f[];', which would be a
different matter entirely: Not an attempt to define an array,
but a declaration that an array is defined somewhere else --
and that the size will be found at that "somewhere else."

> int main(void) { return (0); }
>
>
>
> % cc foo.c
>
> "foo.c", line 1: warning: null dimension: f


Another compiler says

foo.c:1: warning: array 'f' assumed to have one element

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
 
 
 
army1987
Guest
Posts: n/a
 
      08-20-2012
On Mon, 20 Aug 2012 02:41:21 -0700, kerravon wrote:

> char f[];


How big do you want that to be?



--
[ T H I S S P A C E I S F O R R E N T ]
Troppo poca cultura ci rende ignoranti, troppa ci rende folli.
-- fathermckenzie di it.cultura.linguistica.italiano
<http://xkcd.com/397/>
 
Reply With Quote
 
Andrew Cooper
Guest
Posts: n/a
 
      08-20-2012
On 20/08/2012 10:41, kerravon wrote:
> Is this warning considered ridiculous?


No

>
> I'll stop using that syntax if the compiler is
> more correct than me.
>
>
> % cat foo.c
>
> char f[];


I hope you are not using 'char f[];' where you mean to use 'char * f;',
as they are very different things.

~Andrew

>
>
>
> int main(void) { return (0); }
>
>
>
> % cc foo.c
>
> "foo.c", line 1: warning: null dimension: f
>
>
>
>
> % cc -V
>
> cc: Sun C 5.9 SunOS_sparc Patch 124867-01 2007/07/12
>
> usage: cc [ options] files. Use 'cc -flags' for details
>


 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-21-2012
kerravon <(E-Mail Removed)> writes:

> Is this warning considered ridiculous?
>
> I'll stop using that syntax if the compiler is
> more correct than me.
>
>
> % cat foo.c
>
> char f[];
>
> int main(void) { return (0); }
>
> % cc foo.c
>
> "foo.c", line 1: warning: null dimension: f


The warning is true, but possibly misleading. What you've written could
certainly be called a "null dimension" but the effect should be as if
you had written

char f[1];

The C standard gives meaning to this bizarre syntax for what I can only
imagine are historical reasons. There's no sensible reason to write
such code.

This makes it probable that others, who have suggested you did not mean
what you wrote, are right. One way or the other, you should stop using
this syntax because it either does not mean what you want, or (vanishing
unlikely) it's a confusing way to write what you do want (why would you
want am external one-element array?).

--
Ben.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-21-2012
Ben Bacarisse <(E-Mail Removed)> writes:
> kerravon <(E-Mail Removed)> writes:

[...]
>> char f[];

[...]
>> "foo.c", line 1: warning: null dimension: f

>
> The warning is true, but possibly misleading. What you've written could
> certainly be called a "null dimension" but the effect should be as if
> you had written
>
> char f[1];
>
> The C standard gives meaning to this bizarre syntax for what I can only
> imagine are historical reasons. There's no sensible reason to write
> such code.
>
> This makes it probable that others, who have suggested you did not mean
> what you wrote, are right. One way or the other, you should stop using
> this syntax because it either does not mean what you want, or (vanishing
> unlikely) it's a confusing way to write what you do want (why would you
> want am external one-element array?).


As far as I can tell, the rule that `char f[];` is treated as `char
f[1];` isn't even stated explicitly.

N1570 6.9.2p2, External definitions, says:

A declaration of an identifier for an object that has file scope
without an initializer, and without a storage-class specifier or
with the storage-class specifier static, constitutes a *tentative
definition*. If a translation unit contains one or more tentative
definitions for an identifier, and the translation unit contains
no external definition for that identifier, then the behavior
is exactly as if the translation unit contains a file scope
declaration of that identifier, with the composite type as of
the end of the translation unit, with an initializer equal to 0.

An example (non-normative) says:

If at the end of the translation unit containing

int i[];

the array i still has incomplete type, the implicit initializer
causes it to have one element, which is set to zero on program
startup;

So this:

char f[];

is treated as:

char f[] = 0;

which causes `f` to inherit its size from the initializer.

Except that

char f[] = 0;

is invalid. 6.7.9p16 says:

Otherwise, the initializer for an object that has aggregate or union
type shall be a brace-enclosed list of initializers for the elements
or named members.

A twist: That "shall" is not in a constraint, so it's undefined
behavior, not something that requires a diagnostic.

Apparently we have to interpret the "initializer equal to 0" to mean
`{ 0 }` rather than `0` in this case. And we're supposed to infer
from that that `f` has exactly one element; apparently `{ 0 }` is
"equal to 0" but `{ 0, 0 }` isn't.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
kerravon
Guest
Posts: n/a
 
      08-21-2012
On Monday, August 20, 2012 7:41:21 PM UTC+10, kerravon wrote:
> I'll stop using that syntax if the compiler is
> more correct than me.


Wow, just wow. Thanks everyone for your responses.

I'll tell you a story. I normally don't have a
signature in my emails, but someone at work
complained that I was asking them to do things
and they had no idea who I was, and could I thus
include a signature.

The standard template for a signature includes a
job title. I'm a contractor so I didn't think I
had one, other than "contractor", so I just made
up "Junior C Programmer", since as I have stated
my whole career, my career goal is to have a title
of "Senior C Programmer", and I wanted to have
a goal to work towards. Note that I have been
programming in C since 1987. Also note that I have
written my own C runtime library for DOS/Windows/
Unix/OS2/MVS/CMS/VSE, which you can find here:

http://pdos.sourceforge.net

along with not one but two operating systems written
from scratch.

I also spent years getting GCC ported to MVS, CMS
and VSE, which were the very last commercial
programming environments that didn't have a free,
or standard, C compiler. You can find that here:

http://gccmvs.sourceforge.net

But despite all that, it took about 10 minutes for
comp.lang.c to (correctly) point out that the title
of "Junior C Programmer" was probably the right thing
to run with. I later found out (from Lync status
reported by others), that I actually do have an
official title, which is "Senior Software Engineer",
so I updated my title to that, but in spirit, I'm
still learning C. I actually bought the ISO C90
standard (or its AS3955-1991 equivalent), and it
really is a beautiful document, but I've never
read it cover to cover yet, so the title of "Junior"
is well-deserved.

I thought that int **x was the same as int *x[]
anywhere, but your pointed questions made me realise
just how messed up my thinking was (despite my
high (misplaced) confidence that I was right and
the compiler was being ridiculous).

Thanks. Paul.
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      08-21-2012
On Tuesday, August 21, 2012 6:25:29 PM UTC+8, kerravon wrote:
> On Monday, August 20, 2012 7:41:21 PM UTC+10, kerravon wrote:
>
> > I'll stop using that syntax if the compiler is

>
> > more correct than me.

>
>
>
> Wow, just wow. Thanks everyone for your responses.
>
>
>
> I'll tell you a story. I normally don't have a
>
> signature in my emails, but someone at work
>
> complained that I was asking them to do things
>
> and they had no idea who I was, and could I thus
>
> include a signature.
>
>
>
> The standard template for a signature includes a
>
> job title. I'm a contractor so I didn't think I
>
> had one, other than "contractor", so I just made
>
> up "Junior C Programmer", since as I have stated
>
> my whole career, my career goal is to have a title
>
> of "Senior C Programmer", and I wanted to have
>
> a goal to work towards. Note that I have been
>
> programming in C since 1987. Also note that I have
>
> written my own C runtime library for DOS/Windows/
>
> Unix/OS2/MVS/CMS/VSE, which you can find here:
>
>
>
> http://pdos.sourceforge.net
>
>
>
> along with not one but two operating systems written
>
> from scratch.
>
>
>
> I also spent years getting GCC ported to MVS, CMS
>
> and VSE, which were the very last commercial
>
> programming environments that didn't have a free,
>
> or standard, C compiler. You can find that here:
>
>
>
> http://gccmvs.sourceforge.net
>
>
>
> But despite all that, it took about 10 minutes for
>
> comp.lang.c to (correctly) point out that the title
>
> of "Junior C Programmer" was probably the right thing
>
> to run with. I later found out (from Lync status
>
> reported by others), that I actually do have an
>
> official title, which is "Senior Software Engineer",
>
> so I updated my title to that, but in spirit, I'm
>
> still learning C. I actually bought the ISO C90
>
> standard (or its AS3955-1991 equivalent), and it
>
> really is a beautiful document, but I've never
>
> read it cover to cover yet, so the title of "Junior"
>
> is well-deserved.
>
>
>
> I thought that int **x was the same as int *x[]
>
> anywhere, but your pointed questions made me realise
>
> just how messed up my thinking was (despite my
>
> high (misplaced) confidence that I was right and
>
> the compiler was being ridiculous).
>
>
>
> Thanks. Paul.


int **x ; // a pointer to a pointer of an integer
int* x2[4000]; // an array of 4000 pointers to integers in the heap?
int i,j; //i-n for ineger idexing

// malloc might fail

for(j=0;j<4000;j++)
if (( x2[j]=malloc(4000* sizeof(int *))) ==NULL)
{i=-1; break; }

if (i==-1)
{ //error trapping here ....}

//x2[j][i] of 4000X4000 integers here can be used

x=x2 ; // an alias of x2


Can you get the difference?
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-21-2012
88888 Dihedral <(E-Mail Removed)> writes:
<snip>
> int **x ; // a pointer to a pointer of an integer
> int* x2[4000]; // an array of 4000 pointers to integers in the heap?
> int i,j; //i-n for ineger idexing
>
> // malloc might fail
>
> for(j=0;j<4000;j++)
> if (( x2[j]=malloc(4000* sizeof(int *))) ==NULL)


Wrong type in the malloc call. Using the often quoted idiom here would
have prevented this problem:

x2[j] = malloc(4000 * sizeof *x2[j])

> {i=-1; break; }
>
> if (i==-1)
> { //error trapping here ....}


i may be -1 even without any error.

> //x2[j][i] of 4000X4000 integers here can be used
>
> x=x2 ; // an alias of x2
>
> Can you get the difference?


Do you mean that they behave differently as operands of sizeof and of &?

--
Ben.
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      08-21-2012
On Wednesday, August 22, 2012 4:07:24 AM UTC+8, Ben Bacarisse wrote:
> 88888 Dihedral <(E-Mail Removed)> writes:
>
> <snip>
>
> > int **x ; // a pointer to a pointer of an integer

>
> > int* x2[4000]; // an array of 4000 pointers to integers in the heap?

>
> > int i,j; //i-n for ineger idexing

>
> >

>
> > // malloc might fail

>
> >

>
> > for(j=0;j<4000;j++)

>
> > if (( x2[j]=malloc(4000* sizeof(int *))) ==NULL)

>
>
>
> Wrong type in the malloc call. Using the often quoted idiom here would
>
> have prevented this problem:
>
>
>
> x2[j] = malloc(4000 * sizeof *x2[j])
>
>
>
> > {i=-1; break; }

>
> >

>
> > if (i==-1)

>
> > { //error trapping here ....}

>
>
>
> i may be -1 even without any error.


You are right check j for the value that equals 4001 or not is the
more elegant way.
>
>
>
> > //x2[j][i] of 4000X4000 integers here can be used

>
> >

>
> > x=x2 ; // an alias of x2

>
> >

>
> > Can you get the difference?

>
>
>
> Do you mean that they behave differently as operands of sizeof and of &?
>
>
>
> --
>
> Ben.



OK, I'll prevent the array allocated on the stack to be not destroyed
first if I want to use int ** x outside the function that defines
int* x2[4000].


 
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
*RANT* Ridiculous EDA software "user license agreements"? license_rant_master VHDL 37 04-29-2005 03:48 PM
Ridiculous readInt() bug? Read-head not advancing far enough? nobrow@eircom.net Java 17 04-15-2005 10:12 AM
ridiculous - javax.swing.text.html.HTMLEditorKit.Parser needs X11 server to work Nebojsa Topolscak Java 3 01-14-2005 10:43 PM
Rather ridiculous/OT but Im sharing this :p Rob HTML 6 12-18-2003 10:46 PM
This is ridiculous...I AM THE Ron Williams!! R.J. Williams MCSE 33 08-16-2003 10:15 AM



Advertisments