Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > A question on incomplete definitions

Reply
Thread Tools

A question on incomplete definitions

 
 
ark
Guest
Posts: n/a
 
      12-17-2003
Hello group,
Could you help me with this:

static const int x;
............ something .............
static const int x = 17;

It looks perfectly legal to me but MSVC/C++ 6.0 gives, on the first line,
"warning C4132: 'x' : const object should be initialized"
yet generates correct code.

What is correct - the code or the compiler? If the code, is it known what
compilers choke on this and how hard?

Thanks,
Ark


 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      12-17-2003

"ark" <(E-Mail Removed)> wrote in message
> static const int x;
>

This is a tentative forward declaration of x.
>
> ........... something .............
> static const int x = 17;
>

This is the declaration of x
>
> It looks perfectly legal to me but MSVC/C++ 6.0 gives, on the first
> line, "warning C4132: 'x' : const object should be initialized"
> yet generates correct code.
>
> What is correct - the code or the compiler? If the code, is it known
> what compilers choke on this and how hard?
>

The compiler is allowed to give warnings for code which is correct but looks
suspicious. Here you are using a little-used construct with a const, so it
wonders if you know what you are doing.

I would also give a warning, since I suspect that you don't need a tentative
forward declaration at all of a static const integer in your code. However I
may be wrong.


 
Reply With Quote
 
 
 
 
Alex
Guest
Posts: n/a
 
      12-17-2003
ark <(E-Mail Removed)> wrote:
> Hello group,
> Could you help me with this:


> static const int x;


You declare and define x, with the implicit value of 0.
However, since the object is const, you won't have a chance
to change its value. AFAIK, this construct is valid, but
is largely meaningless.

> ........... something .............
> static const int x = 17;


You redeclare and redefine x with the value of 17. This
is obviously incorrect because the x identifier already
exists and has storage.

> It looks perfectly legal to me but MSVC/C++ 6.0 gives, on the first line,
> "warning C4132: 'x' : const object should be initialized"
> yet generates correct code.


This is a friendly warning that says that your first line
doesn't make much sense. It is probably not a required
diagnostic.

Alex
 
Reply With Quote
 
ark
Guest
Posts: n/a
 
      12-17-2003

"Malcolm" <(E-Mail Removed)> wrote in message
news:brqjsi$685$(E-Mail Removed)...
>

<snip>
> I would also give a warning, since I suspect that you don't need a

tentative
> forward declaration at all of a static const integer in your code. However

I
> may be wrong.


Imagine, e.g., a header file that #define's useful thing about x (which is
really likely to be a struct of some sort). Then the header needs a
"tentative forward declaration" of x, and here it is useful IMHO.
Sorry for wrong terminology,
- Ark


 
Reply With Quote
 
ark
Guest
Posts: n/a
 
      12-17-2003

"Alex" <(E-Mail Removed)> wrote in message
news:brqlfn$6id5i$(E-Mail Removed)-berlin.de...
> ark <(E-Mail Removed)> wrote:
> > Hello group,
> > Could you help me with this:

>
> > static const int x;

>
> You declare and define x, with the implicit value of 0.
> However, since the object is const, you won't have a chance
> to change its value. AFAIK, this construct is valid, but
> is largely meaningless.
>
> > ........... something .............
> > static const int x = 17;

>
> You redeclare and redefine x with the value of 17. This
> is obviously incorrect because the x identifier already
> exists and has storage.
>
> > It looks perfectly legal to me but MSVC/C++ 6.0 gives, on the first

line,
> > "warning C4132: 'x' : const object should be initialized"
> > yet generates correct code.

>
> This is a friendly warning that says that your first line
> doesn't make much sense. It is probably not a required
> diagnostic.
>
> Alex


I am not sure your analysis is correct; see Malcolm's posting above.
- Ark


 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      12-17-2003
Alan Balmer <(E-Mail Removed)> writes:

> On Wed, 17 Dec 2003 22:01:25 -0000, "Malcolm"
> <(E-Mail Removed)> wrote:
>
> >> static const int x;
> >>

> >This is a tentative forward declaration of x.
> >>

> How does the compiler distinguish this from a normal declaration with
> default initialization?


See C99 6.9.2#2:

2 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.

--
"The way I see it, an intelligent person who disagrees with me is
probably the most important person I'll interact with on any given
day."
--Billy Chambless
 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      12-17-2003
On Wed, 17 Dec 2003 22:01:25 -0000, "Malcolm"
<(E-Mail Removed)> wrote:

>> static const int x;
>>

>This is a tentative forward declaration of x.
>>

How does the compiler distinguish this from a normal declaration with
default initialization?

--
Al Balmer
Balmer Consulting
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
ark
Guest
Posts: n/a
 
      12-18-2003

"Ben Pfaff" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Alan Balmer <(E-Mail Removed)> writes:
>

<snip>
> See C99 6.9.2#2:
>
> 2 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.
>
> --
> "The way I see it, an intelligent person who disagrees with me is
> probably the most important person I'll interact with on any given
> day."
> --Billy Chambless


So... I can do it only in C99, not in classic C (90?) or whatever was
before?
What about K&R?
Thanks for all help,
- Ark


 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      12-18-2003
"ark" <(E-Mail Removed)> writes:

> "Ben Pfaff" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Alan Balmer <(E-Mail Removed)> writes:
> >

> <snip>
> > See C99 6.9.2#2:
> >
> > 2 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.

>
> So... I can do it only in C99, not in classic C (90?) or whatever was
> before?


There is similar text in C90 if I recall correctly. I generally
quote from C99 because I have an accurate electronic
transcription.

> What about K&R? Thanks for all help, - Ark


K&R didn't have the concept of a tentative definition as far as I
know.
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      12-18-2003
ark wrote:
>
> "Alex" <(E-Mail Removed)> wrote in message
> news:brqlfn$6id5i$(E-Mail Removed)-berlin.de...
> > ark <(E-Mail Removed)> wrote:
> > > Hello group,
> > > Could you help me with this:

> >
> > > static const int x;

> >
> > You declare and define x, with the implicit value of 0.
> > However, since the object is const, you won't have a chance
> > to change its value. AFAIK, this construct is valid, but
> > is largely meaningless.
> >
> > > ........... something .............
> > > static const int x = 17;

> >
> > You redeclare and redefine x with the value of 17. This
> > is obviously incorrect because the x identifier already
> > exists and has storage.
> >
> > > It looks perfectly legal to me but MSVC/C++ 6.0 gives, on the first

> line,
> > > "warning C4132: 'x' : const object should be initialized"
> > > yet generates correct code.

> >
> > This is a friendly warning that says that your first line
> > doesn't make much sense. It is probably not a required
> > diagnostic.
> >
> > Alex

>
> I am not sure your analysis is correct; see Malcolm's posting above.
> - Ark

Alex may be more correct that Malcolm. The whole point of the const
qualifier is that its object should not be modified without diagnostic.
You noticed that..

const int x;

yielding a diagnostic because you missed your only 'legal' chance to
define the value of x. It is not clear that the diagnostic is required
but x clearly useless in this case. Further down in your code..

const int x = 17;

redifines x and assigns a value to it. The redifinition is the error
here and requires a diagnostic. Assigning 17 to x here is probably ok.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
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
Newbie Question. Class definitions on the fly. ishtar2020 Python 2 08-28-2006 04:29 AM
Committed Access Rate definitions question Bartosz Piec Cisco 2 10-10-2005 07:37 AM
Question about definitions Thomas Barth C++ 3 11-15-2004 04:11 PM
Silly question - automating *manual* anti-virus definitions update process Computer Support 3 09-01-2003 06:50 PM
Question about incomplete array element types Paul F. Dietz C Programming 5 07-11-2003 01:19 PM



Advertisments