struct in C99

P

pocmatos

Hi all,

Using -std=c99 to enforce the c99 standard in gcc I get:
$ gcc -Wall -std=c99 timespec.c
timespec.c: In function 'main':
timespec.c:5: error: storage size of 'a' isn't known
timespec.c:5: error: storage size of 'b' isn't known
....

with:

#include <time.h>
#include <stdio.h>

int main() {

struct timespec a, b;

printf("Doh!\n");

return 0;

}


Can someone please explain why? This doesn't happen in previous
versions of C.

Cheers,

Paulo Matos
 
M

Michael Mair

Hi all,

Using -std=c99 to enforce the c99 standard in gcc I get:

Note: gcc still is not entirely a C99 compiler, see
http://gcc.gnu.org/C99status.html
$ gcc -Wall -std=c99 timespec.c

Use gcc -Wall -std=c99 -pedantic -O timespec.c to get more
warnings and refuse more non-Standard C stuff.
timespec.c: In function 'main':
timespec.c:5: error: storage size of 'a' isn't known
timespec.c:5: error: storage size of 'b' isn't known
...

with:

#include <time.h>
#include <stdio.h>

int main() {

struct timespec a, b;

Note: Neither <stdio.h> nor <time.h> is guaranteed to define the
type struct timespec. This type _probably_ comes from your
<sys/types.h> and seems to be not excluded properly for other
modes invoked by the std option.
In short: The compiler gets it right for -std=c99 but should
get it right for -std=c89 as well...
It may be necessary to
#include <sys/types.h>
or to
#define FOO
where FOO governs whether certain parts or the whole of
sys/types.h are processed/included. This is implementation
specific and thus off-topic round here.

It depends on the gcc and glib versions in use, so you have
to ask in a newsgroup dealing with either gcc or your OS to get
a complete/good answer.

printf("Doh!\n");

return 0;

}

Can someone please explain why? This doesn't happen in previous
versions of C.

Note that gcc by default compiles with -std=gnu89 which is C90
plus GNU extensions -- this language and its library contain
stuff plain standard C (in the C90, C95, or C99 variety) does
not have.


Cheers
Michael
 
E

Eric Sosman

Hi all,

Using -std=c99 to enforce the c99 standard in gcc I get:
$ gcc -Wall -std=c99 timespec.c
timespec.c: In function 'main':
timespec.c:5: error: storage size of 'a' isn't known
timespec.c:5: error: storage size of 'b' isn't known
...

with:

#include <time.h>
#include <stdio.h>

int main() {

struct timespec a, b;

printf("Doh!\n");

return 0;

}


Can someone please explain why? This doesn't happen in previous
versions of C.

There is no `struct timespec' in the C Standard library.
Your problem (if you want to call it such) is that you have
told the compiler to adhere to the C Standard (-stc=c99) but
are using a non-Standard extension.
 
K

Keith Thompson

Using -std=c99 to enforce the c99 standard in gcc I get:
$ gcc -Wall -std=c99 timespec.c
timespec.c: In function 'main':
timespec.c:5: error: storage size of 'a' isn't known
timespec.c:5: error: storage size of 'b' isn't known
...

with:

#include <time.h>
#include <stdio.h>

int main() {

struct timespec a, b;

printf("Doh!\n");

return 0;

}


Can someone please explain why? This doesn't happen in previous
versions of C.

The type "struct timespec" is defined by POSIX, not by the C standard.
In (non-POSIX) standard C, a user program is allowed to declare its
own "struct timespec", so it's not allowed to declare it in <time.h>.
You need to tell your compiler to compile in some sort of
POSIX-specific mode. I don't know how to do that. Try a Google
search, or gnu.gcc.help.
 
S

SuperKoko

Hi all,

Using -std=c99 to enforce the c99 standard in gcc I get:
$ gcc -Wall -std=c99 timespec.c
timespec.c: In function 'main':
timespec.c:5: error: storage size of 'a' isn't known
timespec.c:5: error: storage size of 'b' isn't known
...

with:

#include <time.h>
#include <stdio.h>

int main() {

struct timespec a, b;

printf("Doh!\n");

return 0;

}

It simply means that "struct timespec" is an incomplete type... Which
simply means that there has not been any definition of this structure.
Since it is a non-standard structure, this result was expectable.
 
P

pocmatos

Eric Sosman escreveu:
There is no `struct timespec' in the C Standard library.
Your problem (if you want to call it such) is that you have
told the compiler to adhere to the C Standard (-stc=c99) but
are using a non-Standard extension.

Wierd, I would guess that -stc=c99 would let me use other stuff besides
pure c99 but would require _my_ code to be c99 compliant. It seems that
it is much more restrictive. Which is a shame!

Thanks for clearing that up.
 
K

Keith Thompson

Michael Mair escreveu:>

Why is the -O?

I presume you know, or can easily find out from the documentation,
that "-O" enables optimization.

A compiler typically has to collect more information about the program
it's compiling in order to perform certain optimizations. For
example, given:

{
int x, y;
...
x = 10;
...
y = x;
}

If the compiler can prove that x is not modified before its value is
assigned to y, it can replace "y = x;" with the equivalent of "y = 10;".
This is called "dataflow analysis".

In the course of collecting this information, it can detect certain
kinds of programming errors, such as using a variable that hasn't been
initialized. So enabling optimization *and* warnings can often give
you more information about errors in your code than just enabling
warnings.
 
R

Rafael Almeida

On 12 Jun 2006 16:45:30 -0700
Wierd, I would guess that -stc=c99 would let me use other stuff besides
pure c99 but would require _my_ code to be c99 compliant. It seems that
it is much more restrictive. Which is a shame!

Thanks for clearing that up.
If you use functions that are not from c99 standard, then your code is
not c99 compilant. You can, of course, create the function you want, and
that includes functions that are not part of c99 standard, and you have
to have a definition for such functions.
 
M

Michael Mair

Eric Sosman escreveu:


Wierd, I would guess that -stc=c99 would let me use other stuff besides
pure c99 but would require _my_ code to be c99 compliant. It seems that
it is much more restrictive. Which is a shame!

<OT>
You are probably looking for "-std=gnu99".
</OT>

Cheers
Michael
 
S

SuperKoko

Eric Sosman escreveu:

Wierd, I would guess that -stc=c99 would let me use other stuff besides
pure c99 but would require _my_ code to be c99 compliant. It seems that
it is much more restrictive. Which is a shame!
No, it is a flag which allows the GNU compiler to be fully C99
compliant, with extensions.
It allows any piece of strictly conforming code to compile (well, at
this time, the GNU compiler doesn't fully implement C99, but in a near
future, it will).
But these extensions mustn't interact badly with strictly conforming
code.
And a strictly conforming code can write its own "struct timespec".
If this non-standard extension was put in the language, a strictly
conforming program, such as:
#include <time.h>
#include <stdio.h>

struct timespec {int a,b;};
int main() {


struct timespec a, b;
a.a=0;a.b=8;


printf("Doh!\n");


return 0;


}

Would not compile, saying that timespec is defined twice.

It doesn't mean that this extension is inexistant; Read the
documentation.

There is perhaps a __timespec (names starting with double underscores
are reserved for compiler extensions and this kind of stuff).
Or there is perhaps a header to include : Perhaps <timespec.h>

The gnu99 mode is "almost" ISO compliant but contains a few core or
library extensions which doesn't permit the compilation of a few
strictly conforming well-formed programs.

Anyway, -std=c99 doesn't require your program to be conforming... You
can still use a lot of non-standard extensions, but not extensions
which change the meaning of strictly-conforming programs.
With -std=c99, the compiler itself is ISO-compliant (as far as
possible).

If you want to reduce the number of non-standard extensions used in
your program, then you should use the "--pedantic-errors" compiler
flag... Otherwise your program might contain several non-standard
extensions.

For instance, this piece of code:

int main() {
int i=42;
int u;
}

Compiles with "-xc -std=c89"
While it doesn't compile (complaining that VLA are forbidden) with "-xc
-std=c89 --pedantic-errors".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top