Pointers to fields in a struct

J

JS

I have this struct:

struct pcb {
void *(*start_routine) (void *);
void *arg;
jmp_buf state;
int stak[1024];
};

These pointers:
int *state_pointer;
struct pcb *pcb_pointer;
pcb_pointer = (struct pcb *) malloc(sizeof(struct pcb));

Now I have this:
state_pointer = (int *) &pcb_pointer->state;


state_pointer point to the address of state in the pcb struct.

What confuses me is that state_pointer is an integer pointer while state has
type jmp_buf. How can an integer pointer point to a variable with type
jmp_buf??

JS
 
T

Thomas Matthews

JS said:
I have this struct:

struct pcb {
void *(*start_routine) (void *);
void *arg;
jmp_buf state;
int stak[1024];
};

These pointers:
int *state_pointer;
struct pcb *pcb_pointer;
pcb_pointer = (struct pcb *) malloc(sizeof(struct pcb));

Now I have this:
state_pointer = (int *) &pcb_pointer->state;


state_pointer point to the address of state in the pcb struct.

What confuses me is that state_pointer is an integer pointer while state has
type jmp_buf. How can an integer pointer point to a variable with type
jmp_buf??

JS

The cast "(int *)" in the assignment tells the compiler to
do its best job to convert a "pointer to jmp_buf" to a
pointer to integer. The details of the conversion are
compiler specific. The results are undefined behavior,
and most-likely platform dependent.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
E

Eric Sosman

JS said:
I have this struct:

struct pcb {
void *(*start_routine) (void *);
void *arg;
jmp_buf state;
int stak[1024];
};

These pointers:
int *state_pointer;
struct pcb *pcb_pointer;
pcb_pointer = (struct pcb *) malloc(sizeof(struct pcb));

This would be better as

pcb_pointer = malloc(sizeof *pcb_pointer);
Now I have this:
state_pointer = (int *) &pcb_pointer->state;


state_pointer point to the address of state in the pcb struct.

What confuses me is that state_pointer is an integer pointer while state has
type jmp_buf. How can an integer pointer point to a variable with type
jmp_buf??

Unsafely. You're likely to get away with it, because
in a typical implementation a `jmp_buf' will contain things
whose alignment is at least as strict as that of an `int'.
But it seems a gratuitous piece of uncleanliness; why not
use `jmp_buf *state_pointer;' instead?
 
A

Andrey Tarasevich

JS said:
I have this struct:

struct pcb {
void *(*start_routine) (void *);
void *arg;
jmp_buf state;
int stak[1024];
};

These pointers:
int *state_pointer;
struct pcb *pcb_pointer;
pcb_pointer = (struct pcb *) malloc(sizeof(struct pcb));

Now I have this:
state_pointer = (int *) &pcb_pointer->state;


state_pointer point to the address of state in the pcb struct.

What confuses me is that state_pointer is an integer pointer while state has
type jmp_buf. How can an integer pointer point to a variable with type
jmp_buf??
...

Well, the first question is why the hell are you (or the author of the
code) trying to use an 'int*' pointer to point to a 'jmp_buf' object?

At the same time, C standard guarantees that if 'jmp_buf' is an
aggregate type and the first member of that aggregate type is of type
'int', then the resultant pointer will point to that first member. That
would be portable. Otherwise, you are probably dealing with a hack that
can only be explained by some platform-specific reasons.
 
C

Chris Torek

I have [a pointer to struct containing a jmp_buf named "state", and also]
int *state_pointer; ...
state_pointer = (int *) &pcb_pointer->state;

state_pointer point to the address of state in the pcb struct.

What confuses me is that state_pointer is an integer pointer while state has
type jmp_buf. How can an integer pointer point to a variable with type
jmp_buf??

It cannot. Unless perhaps it can; see below. But the cast -- the
thing in parentheses -- tells the compiler: "shut up about this
code that is probably wrong, because I, the programmer, know much
better than you and I want to do it anyway."

As a general rule, pointer casts in C mean that whoever wrote the
code was either very smart, or rather careless. :) Most C code
should be cast-free. There are some exceptions, and casts from
int to double (or vice versa), for instance, are pretty mild and
minor. It is pointer casts that get people into trouble. If you
can write the code without a cast, and compile it without complaint
from the compiler, you will usually be better off than if you use
a cast and shut the compiler up even if it is right and your code
is wrong after all.

Now, if we take out the cast, *and* take out the unary "&" operator:

state_pointer = pcb_pointer->state;

and if the code still compiles quietly, then we have learned
something about the implementation in question. We know -- from
the C standard, which says so -- that "jmp_buf" is a typedef for
an array type. We do not know what the array type is, and it may
(and probably does) change from one compiler to the next. But it
could well be a typedef for "array N of int", for some constant N,
and in fact this turns out to be fairly common. If you have a text
<setjmp.h> that you can read, you might peek at it and find:

typedef int jmp_buf[2];

on some machine, and:

typedef int jmp_buf[11];

on another. And of course, we all know The Rule about arrays and
pointers in C by now (see http://web.torek.net/torek/c/pa.html),
so:

int *p;
int arr[N];
...
p = arr;

is quite OK (setting p to &arr[0]). If jmp_buf is a typedef for
"array N of int", then -- regardless of the constant N -- the
assignment to "int *" is OK.

Of course, the moment you change compilers and/or machines, you
may find the code is no longer OK, because now perhaps jmp_buf
is a typedef of the form:

typedef struct __jmp_buf {
short __jb_rX;
long __jb_rY;
char *__jb_rZ;
} jmp_buf[1];

So it is not a good idea to use "int *" to point to a jmp_buf,
even if it happens to work on the machine at hand.
 
K

Keith Thompson

JS said:
I have this struct:

struct pcb {
void *(*start_routine) (void *);
void *arg;
jmp_buf state;
int stak[1024];
};

Indentation is your friend. Try it this way:

struct pcb {
void *(*start_routine) (void *);
void *arg;
jmp_buf state;
int stak[1024];
};

These pointers:
int *state_pointer;
struct pcb *pcb_pointer;
pcb_pointer = (struct pcb *) malloc(sizeof(struct pcb));

That last statement would be better written as:

pcb_pointer = malloc(sizeof *pcb_pointer);

The cast is unnecessary and can mask errors. By applying the sizeof
operator to whatever pcb_pointer points to, it becomes obvious that
you're using the size of the correct type; referring to the size of
the type "struct pcb" means it's correct only if pcb_pointer is of
type struct pcb* (which it is today, but may not be tomorrow).
Now I have this:
state_pointer = (int *) &pcb_pointer->state;


state_pointer point to the address of state in the pcb struct.

What confuses me is that state_pointer is an integer pointer while state has
type jmp_buf. How can an integer pointer point to a variable with type
jmp_buf??

It points to a variable with type jmp_buf because you told it to. The
(int*) cast converts the value (which is of type jmp_buf*) to type
int*.

It's legal to use a cast operator to convert a pointer of one type to
a pointer of another type. Typically, if you know what you're doing,
this can be a way to interpret the pointed-to memory as if it were of
some other type. If you don't know what you're doing, it's a good way
to make your program misbehave.

Why is this code converting a jmp_buf* to an int*? How should we
know? It's your code, not ours.

In this case, I'm guessing that it's a way to dig into the internals
of the jmp_buf by viewing it as an array of int. That's usually a
very bad idea unless you happen to know the details of the
implementation. If the jmp_buf isn't properly aligned, you can even
invoke undefined behavior.
 
K

Keith Thompson

Andrey Tarasevich said:
At the same time, C standard guarantees that if 'jmp_buf' is an
aggregate type and the first member of that aggregate type is of type
'int', then the resultant pointer will point to that first member. That
would be portable. Otherwise, you are probably dealing with a hack that
can only be explained by some platform-specific reasons.

The standard guarantees that jmp_buf is an array type. The length and
element type are not specified.

On one system I use, it's a 208-byte array consisting of 52 4-byte
elements. On another, it's a 704-byte array consisting of a single
704-byte element.
 

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,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top