473,566 Members | 3,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

struct task_struct;


I think this is a C question rather than a Linux question.

I have a copy of Linux Core Kernel Commentary, Scott Maxwell, published
in 1999. On p.110, the source for include/asm-i386/current.h begins

#ifndef _I386_CURRENT_H
#define _I386_CURRENT_H

struct task_struct;

I'm not very good at C, so maybe this is really a dumb question, but it
is my impression that one does not simply write things like

struct task_struct;

I thought structure declarations had to have more in them, e.g. something like:

struct gleep { int gleep_no; char gleep_char;} gleep_instance;

What does it signify just to write "struct task_struct;"?
--
Ignorantly,
Allan Adler <ar*@zurich.csa il.mit.edu>
* Disclaimer: I am a guest and *not* a member of the MIT CSAIL. My actions and
* comments do not reflect in any way on MIT. Also, I am nowhere near Boston.
Nov 15 '05 #1
7 4086
Hi,

Allan Adler wrote:

I'm not very good at C, so maybe this is really a dumb question, but it
is my impression that one does not simply write things like

struct task_struct;

I thought structure declarations had to have more in them, e.g. something like:

struct gleep { int gleep_no; char gleep_char;} gleep_instance;

What does it signify just to write "struct task_struct;"?


What you have there is called a "forward declaration".
After having made it, you can use the name task_struct, unless the
compiler needs to know the size of it. For example you can now define
pointers to task_struc.

A forward declaration is necessary when you have two objects that are to
hold pointers to each other. Naively writing

struct A {
B* ptr_to_b;
};

struct B {
A* ptr_to_a;
};

doesn't work, since in the declaration of A the compiler doens't know B
yet. So you put a

struct B;

in front of it.

Steffen

Nov 15 '05 #2
Allan Adler wrote:

I think this is a C question rather than a Linux question.

I have a copy of Linux Core Kernel Commentary, Scott Maxwell, published
in 1999. On p.110, the source for include/asm-i386/current.h begins

#ifndef _I386_CURRENT_H
#define _I386_CURRENT_H

struct task_struct;

I'm not very good at C, so maybe this is really a dumb question, but it
is my impression that one does not simply write things like

struct task_struct;

I thought structure declarations had to have more in them, e.g. something like:

struct gleep { int gleep_no; char gleep_char;} gleep_instance;

What does it signify just to write "struct task_struct;"?


It says that you have a struct named "task_struc t", but does not define
the structure itself. This will allow you to make reference to the
struct, as long as you don't need to know how it's defined. For example,
you can define a function which gets passed a "struct task_struct *",
or returns such a pointer. (Note that you can define such functions
without the above declaration, but the compiler is probably free to give
a warning that it's not defined/declared.)

The actual definition of the struct is elsewhere.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Nov 15 '05 #3
Steffen <s.*********@gm x.de> wrote:
Allan Adler wrote:

I'm not very good at C, so maybe this is really a dumb question, but it
is my impression that one does not simply write things like

struct task_struct;
<snip>What you have there is called a "forward declaration".
After having made it, you can use the name task_struct, unless the
compiler needs to know the size of it. For example you can now define
pointers to task_struc.

A forward declaration is necessary when you have two objects that are to
hold pointers to each other. Naively writing

struct A {
B* ptr_to_b;
};

struct B {
A* ptr_to_a;
};

doesn't work, since in the declaration of A the compiler doens't know B
yet. So you put a

struct B;

in front of it.


And, in order to make it work, you have to write
struct B* ptr_to_b;
and
struct A* ptr_to_a;
respectively in above declarations, since in C the keyword struct,
though introducing a new type, does not automatically generate a
typedef-name for that new type - unlike that other language with a
capital c in its name.

Best regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc.
Nov 15 '05 #4
Steffen wrote:
Hi,

Allan Adler wrote:

I'm not very good at C, so maybe this is really a dumb question, but it
is my impression that one does not simply write things like

struct task_struct;

I thought structure declarations had to have more in them, e.g. something like:

struct gleep { int gleep_no; char gleep_char;} gleep_instance;

What does it signify just to write "struct task_struct;"?


What you have there is called a "forward declaration".
After having made it, you can use the name task_struct, unless the
compiler needs to know the size of it. For example you can now define
pointers to task_struc.


Wouldn't the compiler need to know the size of the struct to be able to
actually decide how to implement a pointer to such a structure (I'm
thinking of alignment needs)?

Nov 15 '05 #5
"Antonio Contreras" <an*****@gmail. com> wrote:
Steffen wrote:
Allan Adler wrote: <snip>
> struct task_struct;
<snip> What you have there is called a "forward declaration".
After having made it, you can use the name task_struct, unless the
compiler needs to know the size of it. For example you can now define
pointers to task_struc.


Wouldn't the compiler need to know the size of the struct to be able to
actually decide how to implement a pointer to such a structure (I'm
thinking of alignment needs)?


C has what is called "incomplete types". It's perfectly legitimate to
define pointers to incomplete types. If alignment is an issue, the
compiler has to take care of that. For example think about void
pointers: since a pointer to void (an incomplete type that cannot be
completed) can be converted to a pointer to any other object type, it
has to be suitable aligned.

Best regards.
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc.
Nov 15 '05 #6
Antonio Contreras wrote:

Steffen wrote:
Hi,

Allan Adler wrote: [...]
What does it signify just to write "struct task_struct;"?


What you have there is called a "forward declaration".
After having made it, you can use the name task_struct, unless the
compiler needs to know the size of it. For example you can now define
pointers to task_struc.


Wouldn't the compiler need to know the size of the struct to be able to
actually decide how to implement a pointer to such a structure (I'm
thinking of alignment needs)?


Nothing in this code needs to know anything about "mystruct":

typedef struct mystruct MYSTRUCT;
extern int realfunc(MYSTRU CT *,int);

int mywrapper(MYSTR UCT *pt)
{
static int depth = 0;
int retval;

depth++;
retval = realfunc(pt,dep th);
depth--;

return(retval);
}

A pointer variable doesn't need to be suitably aligned based on what it
points to. The value of the pointer needs to be aligned. (I hope I've
explained that well.)

So, "struct task_struct *ptask;" doesn't need to know anything about the
struct to be properly handled. It's not until you try to do something
with the pointer (dereference it, increment it, malloc(sizeof(* ptask)),
etc.) does it need to know the complete struct definition.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Nov 15 '05 #7
"Antonio Contreras" <an*****@gmail. com> writes:
Steffen wrote:
Hi,

Allan Adler wrote:
>
> I'm not very good at C, so maybe this is really a dumb question, but it
> is my impression that one does not simply write things like
>
> struct task_struct;
>
> I thought structure declarations had to have more in them,
> e.g. something like:
>
> struct gleep { int gleep_no; char gleep_char;} gleep_instance;
>
> What does it signify just to write "struct task_struct;"?


What you have there is called a "forward declaration".
After having made it, you can use the name task_struct, unless the
compiler needs to know the size of it. For example you can now define
pointers to task_struc.


Wouldn't the compiler need to know the size of the struct to be able to
actually decide how to implement a pointer to such a structure (I'm
thinking of alignment needs)?


It's possible for different pointer types to have different
representations depending on what they point to. For example, on a
word-addressed machine a byte pointer (char* or void*) might be bigger
than a word pointer (int*). But C specifically requires all struct
pointers to have the same representation, precisely so that incomplete
types will work.

--
Keith Thompson (The_Other_Keit h) 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.
Nov 15 '05 #8

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

Similar topics

5
17594
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have two questions: 1. Is it generally safe to "overlay" the structure on the buffer, e.g.: unsigned char buffer;
5
3284
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used to be in that single file, that worked OK. But when I divdie that file into several ones, I have many "invalid use of undefined type" errors. The...
19
2598
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; } RtWidget;
16
3818
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
5
4319
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read that there is no difference between giving this function a
7
2251
by: Alex | last post by:
If I have two struct. See below: struct s1 { int type; int (*destroy)(struct s1* p); } struct s2 { struct s1 base;
4
5055
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or deleted, however it must be initialized by a function during run-time to contain so many users which each contain so many directories of which each...
4
9791
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
4
2762
by: Sheldon | last post by:
Hi, I have a unique case where I need an array of structs that grows and within this array is another struct that grows in some cases. I'm having trouble allocating memory. Since I have never done this before, I'm sure it's a rookie mistake but I cannot seem to find it. Can someone render some assistance please? struct Fpos {...
0
7673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7893
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.