473,386 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

#include in header file for size_t

Imagine I have a structure with a size_t member:
/* foo.h */
struct foo {
char const *bar;
size_t barlen;
};
void make_foo(struct foo *p);
Now I #include it where needed
/* foo.c */
#include "foo.h"
void make_foo(struct foo *p) {
p->bar = "immutable string";
p->barlen = 16;
}
If I compile these files now I get a `parse error before "size_t"'.
To compile I need to #include <stdlib.h>.

As foo.c doesn't need anyhting from stdlib.h I thought about including
it in foo.h but then remembered header files shouldn't include other
header files, so I tried including stdlib.h in either file, which worked
for both experiments.

Can this be an exception to the rule about not including header files in
header files?
[ It doesn't matter, but for completeness sake, I'm using gcc 3.3.5 ]
[ compile command-line: gcc -W -Wall -std=c89 -pedantic -c foo.c ]

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Jan 27 '06 #1
14 22320
Pedro Graca <he****@dodgeit.com> writes:
Imagine I have a structure with a size_t member:
/* foo.h */
struct foo {
char const *bar;
size_t barlen;
};
void make_foo(struct foo *p);
Now I #include it where needed
/* foo.c */
#include "foo.h"
void make_foo(struct foo *p) {
p->bar = "immutable string";
p->barlen = 16;
}
If I compile these files now I get a `parse error before "size_t"'.
To compile I need to #include <stdlib.h>.

As foo.c doesn't need anyhting from stdlib.h I thought about including
it in foo.h but then remembered header files shouldn't include other
header files, so I tried including stdlib.h in either file, which worked
for both experiments.

Can this be an exception to the rule about not including header files in
header files?


Where did you get the idea that there's such a rule?

There's nothing wrong with including header files in header files.
Do it if you need to, don't do it if you don't.

Most (or all?) header files should have include guards so they can
safely be included multiple times. For example:

========================================
#ifndef FOO_H
#define FOO_H

struct foo {
char const *bar;
size_t barlen;
};
void make_foo(struct foo *p);

#endif /* FOO_H */
========================================

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Jan 27 '06 #2
Keith Thompson said:
There's nothing wrong with including header files in header files.
Do it if you need to, don't do it if you don't.

Most (or all?) header files should have include guards so they can
safely be included multiple times. For example:

========================================
#ifndef FOO_H
#define FOO_H

struct foo {
char const *bar;
size_t barlen;
};
void make_foo(struct foo *p);

#endif /* FOO_H */
========================================


You forgot to #include <stddef.h> :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jan 27 '06 #3
> Most (or all?) header files should have include guards so they can
safely be included multiple times. For example:


Some of them don't: <assert.h>, for one. The only time include guards
shouldn't really be used is if a header can be included multiple times
in the same file and have different effects each time. For example, I
once designed a dynamic array header that I didn't put include guards
in because it needed things defined to work and could be included
multiple times in the same file for dynamic arrays of different types.

Still, it's the exception, not the rule. If an include file should not
have include guards, the programmer should say so in the comments.

Gregory Pietsch

Jan 27 '06 #4
Keith Thompson wrote:
Pedro Graca <he****@dodgeit.com> writes:

<snip>
Can this be an exception to the rule about not including header files in
header files?


Where did you get the idea that there's such a rule?


I must have misunderstood something I read.
The inexistence of such a rule solves my 'problem' :)

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Jan 27 '06 #5
Richard Heathfield wrote:
[ snip: stuff with references to <stdlib.h> lost ]
You forgot to #include <stddef.h> :-)


Ah! I've peeked into /usr/include/stdlib.h and found out it does a
#include <stddef.h>

My hard disk thanks you :)

.... or is there another reason to choose <stddef.h> over <stdlib.h>?

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Jan 27 '06 #6
On 2006-01-27, Pedro Graca <he****@dodgeit.com> wrote:
Imagine I have a structure with a size_t member:

Now I #include it where needed

If I compile these files now I get a `parse error before "size_t"'.
To compile I need to #include <stdlib.h>.

As foo.c doesn't need anyhting from stdlib.h I thought about including
it in foo.h but then remembered header files shouldn't include other
header files, so I tried including stdlib.h in either file, which worked
for both experiments.

Can this be an exception to the rule about not including header files in
header files?


I've never heard of such a rule, but size_t also "lives" in stddef.h,
which doesn't declare any functions.
Jan 27 '06 #7
On 2006-01-27, Gregory Pietsch <GK**@flash.net> wrote:
The only time include guards shouldn't really be used is if a header
can be included multiple times in the same file and have different
effects each time.


I wouldn't call that a header - i'd maybe name it .inc instead. Same
with includable files that define data structures [unless it has a more
specific suffix associated with it, such as .xpm]
Jan 27 '06 #8
On 2006-01-27, Pedro Graca <he****@dodgeit.com> wrote:
Richard Heathfield wrote:
[ snip: stuff with references to <stdlib.h> lost ]
You forgot to #include <stddef.h> :-)
Ah! I've peeked into /usr/include/stdlib.h and found out it does a
#include <stddef.h>


That's not guaranteed - all that's guaranteed is that size_t (, wchar_t,
and NULL) is made available by both.

My hard disk thanks you :)

... or is there another reason to choose <stddef.h> over <stdlib.h>?


You don't need the stuff in stdlib.
Jan 27 '06 #9

"Keith Thompson" <ks***@mib.org> wrote .
Pedro Graca <he****@dodgeit.com> writes:

Can this be an exception to the rule about not including header files in
header files?


Where did you get the idea that there's such a rule?

There's nothing wrong with including header files in header files.
Do it if you need to, don't do it if you don't.

I've never found a good answer to this one.

If you follow a policy of never allowing nested inclusions, then you have a
list of dependencies at the top of each source file.
You also have a list, in reverse order, of the files in call hierarchy.

The problem is that quite often, in video games, you need a "world".
Everything depends on the world, but it also contains everything.
Therefore my function

move_invader(INVADER *baddy, WORLD *wld)

take the world as a parameter, becase the invader needs to update its
position within it.
We will want all the invader functions, like invader_fire, invader_die,
invader_land_and_take_over_Earth in the same invader.c file.

However the world looks like this

typedef struct
{
INVADER *invaderlist;
int width;
int height;
CELL *cells;
SHIP *goody;
} WORLD;

So the easiest thing is to include the definitions of INVADER, CELL, and
SHIP as nested includes in world.h

That means that every file in the program includes "world.h" and no other
headers, and any sense of hierarchy is lost.
Jan 28 '06 #10
Malcolm wrote:
"Keith Thompson" <ks***@mib.org> wrote .
Pedro Graca <he****@dodgeit.com> writes:


Can this be an exception to the rule about not including header files in
header files?


Where did you get the idea that there's such a rule?

There's nothing wrong with including header files in header files.
Do it if you need to, don't do it if you don't.


I've never found a good answer to this one.

If you follow a policy of never allowing nested inclusions, then you have a
list of dependencies at the top of each source file.
You also have a list, in reverse order, of the files in call hierarchy.

The waters are muddied further when the compiler supports some form of
pre-compiled headers, where a global header that includes all headers
can save a significant amount of compile time.

--
Ian Collins.
Jan 28 '06 #11
Malcolm wrote:
That means that every file in the program includes "world.h" and no other
headers, and any sense of hierarchy is lost.


I don't understand what hierarchy has to do with it here. Include when
you need a definition or declaration to be present - otherwise don't.
Perhaps move your globally used types to a world_types.h file or
otherwise equivalent.

You won't get any coding done if you fret about the hierarchy of header
file includes. It doesn't mean make it messy and unorganized - but
there's a point where one just has to move on and start on the actual
heavy lifting. Include-guard everything.

Jan 28 '06 #12
On Sat, 28 Jan 2006 00:55:14 +0000 (UTC), in comp.lang.c , "Malcolm"
<re*******@btinternet.com> wrote:

However the world looks like this

typedef struct
{
INVADER *invaderlist;
int width;
int height;
CELL *cells;
SHIP *goody;
} WORLD;

So the easiest thing is to include the definitions of INVADER, CELL, and
SHIP as nested includes in world.h


I believe that since they're all pointers, you can actually declare
them as incomplete types, and only supply a definition when you need
it.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jan 28 '06 #13
On 27 Jan 2006 20:19:24 -0800, in comp.lang.c , "clayne"
<cl****@anodized.com> wrote:
Malcolm wrote:
That means that every file in the program includes "world.h" and no other
headers, and any sense of hierarchy is lost.


I don't understand what hierarchy has to do with it here. Include when
you need a definition or declaration to be present - otherwise don't.


Malcolm's point was that one struct that is universally used, relies
on pointers to other structs which are not always used. Thus his
universal header included all other headers and there was no easy way
to tell what features a given module actually used.

I actually think he can get round this via incomplete types, but its a
valid issue.

Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jan 28 '06 #14

"Mark McIntyre" <ma**********@spamcop.net> wrote
However the world looks like this

typedef struct
{
INVADER *invaderlist;
int width;
int height;
CELL *cells;
SHIP *goody;
} WORLD;

So the easiest thing is to include the definitions of INVADER, CELL, and
SHIP as nested includes in world.h


I believe that since they're all pointers, you can actually declare
them as incomplete types, and only supply a definition when you need
it.

That's what I'm currently doing.
I still don't really like it, because then we can't modify WORLD to include
a

struct cell current_cell;

Also, the world.c file itself is still topsy-turvy. Probably the solution is
to put the trivial WORLD functions like "getwidth()" in one file, and the
main ones like "run_for_cycle" in another.
Jan 29 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: JKop | last post by:
I'm writing a ".cpp" and ".hpp" combination. The ".hpp" just contains one sole function declaration. The ".cpp" makes use of the function "std::strlen" and the type "std::size_t".
6
by: alan | last post by:
Dear all, I have written my own function by C. And my development platform is W2k with VC6.0. Then I also defined a header file to extern declare this function. After that, I include this...
6
by: candy | last post by:
hi all, I just want to know that whether the C header files( like stdio.h,etc which the compiler provides) just contains the function declarations or they also contain some additionalinformation...
5
by: Angel | last post by:
Is there some way to add a C-header file into a C# project? The problem is that this .h file contains several complex structures (over 20) that are used by some unmanaged code. These functions...
13
by: giovanniparodi79 | last post by:
Hello everybody is there some utility to convert a raw image in an header file? Thanks everybody Gio
9
by: chat | last post by:
Hi, every body. I have 3 files like this: -------------------------------------------------------- file name : header.h #ifndef TEST_H #define TEST_H int a=1; double b=0.5;
3
by: andrejohn.mas | last post by:
Hi, I am writing a C++ program that needs to include a C header file. The problem I am having is that a couple of the functions have 'new' as parameter name: int kernel_sysctl(struct proc *p,...
3
by: Dv | last post by:
I have a API lib (written in C/C++) that can be used by C/C++ project. Now, I'm adding support to C# project. I changed the Lib to DLL. This is easy. However, I have no idea how to deal with the...
11
by: subramanian100in | last post by:
Suppose the following is in Test.h #ifndef TEST_H #define TEST_H #include <iostream> #include <string> using namespace std;
1
by: pralu | last post by:
hi i have made one header file named struct EventLogEntry.h can u tell me how can i add it to my main source code ...in visual c
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.