Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

c++ - Is a pointer to an array of unknown size incomplete?

3.9/6 N3797:

[...]

The type of a pointer to array of unknown size, or of a type defined by a typedef declaration to be an array of unknown size, cannot be completed.

It sounds like a pointer to an array of unknown size is an incomplete type. If so we couldn't define an object of a pointer to array of unknown size. But it is not true, because we can define an array of unknown bound.

#include <iostream>

using std::cout;
using std::endl;

int (*a)[] = (int(*)[])0x4243afff;

int main()
{

}

It compiles fine.

DEMO

We could't do it if it were incomplete type. Indeed: 3.9/5:

Objects shall not be defined to have an incomplete type

The Standard previously defined an incomplete types as follows 3./5:

A class that has been declared but not defined, an enumeration type in certain contexts (7.2), or an array of unknown size or of incomplete element type, is an incompletely-defined object type. Incompletely defined object types and the void types are incomplete types (3.9.1).

Which means the pointer to an incomplete type is complete. Contradiction?

So where I'm wrong in my reasoning?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think this wording is defective. In your code:

 int (*a)[];

the type of a is actually complete. The type of *a is incomplete. It seems to me (as dyp says in comments) that the intent of the quote was to say that there is no way that later in the program, *a will be an expression with complete type.

Background: some incomplete types can be completed later e.g. as suggested by cdhowie and dyp:

extern int a[];
int b = sizeof a;  // error
int a[10];
int c = sizeof a;  // OK

However int (*a)[]; cannot be completed later; sizeof *a will always be an error.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...