473,465 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Create 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.csail.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 4076
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_struct", 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.*********@gmx.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*******@freenet.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*******@freenet.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(MYSTRUCT *,int);

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

depth++;
retval = realfunc(pt,depth);
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_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.
Nov 15 '05 #8

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

Similar topics

5
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...
5
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...
19
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; }...
16
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
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...
7
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
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...
4
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
4
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...
1
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...
0
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...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.