Question about using & and structs (sorry, poor subject title!)

S

Stephen Kershaw

Hi,

I know some basic C and am currently trying to understand some source code.
I enclose some relevant snippets of code below.

My question is this:
The code creates a large array of chars, called t_data. The first 10
spaces are then filled with characters.
However, the line:
params = (struct param *)&t_data;
then, I think (not entirely sure), creates a variable of type param
(struct defined elsewhere), starting at the address of the start of t_data?

So, to my mind the values previously written to t_data are overwritten
so there is no point to the code filling the first 10 entries of the
array? Is this correct?

Hopefully I have provided enough context below,

Many thanks,
Stephen

(... represents cropped code)
(the struct param is defined in a .h file, structure of ints and chars)

#define T_DATA_MAX 10000000
unsigned char t_data[T_DATA_MAX];
struct param *params;
....
for(i=0; i<10; i++)
{
t_data = i;
}
....
params = (struct param *)&t_data;
 
S

santosh

Stephen said:
Hi,

I know some basic C and am currently trying to understand some source code.
I enclose some relevant snippets of code below.

My question is this:
The code creates a large array of chars, called t_data. The first 10
spaces are then filled with characters.
However, the line:
params = (struct param *)&t_data;
then, I think (not entirely sure), creates a variable of type param
(struct defined elsewhere), starting at the address of the start of t_data?

It initialises a pointer to structure of type param, with the address
of the array t_data[]. Since they are both objects of different types a
cast is needed.

BTW, params = (struct param *) t_data would also be the same.
So, to my mind the values previously written to t_data are overwritten
so there is no point to the code filling the first 10 entries of the
array? Is this correct?

No, the values written to t_data are still there and can still be
accessed, however their access via the pointer params might or might
not be meaningful. It depends on the implementation and the intent of
the programmer who wrote the code.
 
M

Martin Ambuhl

Stephen said:
The code creates a large array of chars, called t_data. The first 10
spaces are then filled with characters.
However, the line:
params = (struct param *)&t_data;
then, I think (not entirely sure), creates a variable of type param
(struct defined elsewhere), starting at the address of the start of t_data?

params should have been declared as a pointer-to-(struct param):
struct param *params;
The value of the address of t-data is stored in params as a
such a pointer.

No variable is created, and params is not of type 'struct param'.
This is a dangerous trick which will be portable only in special cases.
In particular,
1) struct param may have internal padding that differs between
implementations (even between different releases of the same
implementations), so any member beyond the first may be misaligned.
2) If the first member of struct param has alignment requirements more
stringent that that of char *, the contents of t_data may be
misaligned for it.
So, to my mind the values previously written to t_data are overwritten
so there is no point to the code filling the first 10 entries of the
array? Is this correct?

No. Nothing about t_data has been changed or even looked at. Only its
address has been stored as a pointer-to-(struct param) in params.
 
S

santosh

Martin said:
params should have been declared as a pointer-to-(struct param):
struct param *params;
The value of the address of t-data is stored in params as a
such a pointer.

No variable is created, and params is not of type 'struct param'.
This is a dangerous trick which will be portable only in special cases.
In particular,
1) struct param may have internal padding that differs between
implementations (even between different releases of the same
implementations), so any member beyond the first may be misaligned.
2) If the first member of struct param has alignment requirements more
stringent that that of char *, the contents of t_data may be
misaligned for it.

Can you give an example struct declaration for 2. above?
 
M

Martin Ambuhl

santosh said:
Can you give an example struct declaration for 2. above?

Many platforms require several or all of int, long int, long long int,
float, double, and long double to start at addresses that are multiples
of 2, 4, 8, or even larger values. Since the char array can begin at
any address, all of these will break. It is easier to find examples
where (2) is a problem than to find ones where it is not.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top