473,508 Members | 4,179 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array typedef vs. structure

Hi,

I'm writing code for an embedded system. In the system a
Timer has 4 memory mapped registers of 32-bit lengths
in contiguous locations:
Timer 0:
0x1000 Configuration register
0x1004 Control register
0x1008 Input register
0x100C Output register.

My objective is to declare a type for the timer,
so I can write the following:
Timer * pTimers = (Timer *) 0x1000;
pTimers[1]->Config_Reg = 0x24;
/* or */
pTimer++; /* points to next timer */

My choices are to declare a typedef of an array of
4 32-bit {unsigned} integers or a structure with
4 32-bit {unsigned} integers.

Th issue with a structure is that the compiler
*may* insert padding bytes between members, which
would mess up the mapping (of members to physical
registers).

However, an array doesn't have field members, so
one would have to declare named constants for the
indices:
#define Config_Reg 0
#define Control_Reg 1
#define Input_Reg 2
#define Output_Reg 3

Is there any preference between an array typedef
and a structure for readability?

I know if I use a structure, I can always say
something like:
assert(sizeof(struct Timer) == 4 * sizeof(/* 32-bit integer */));

--
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.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #1
4 2810
Thomas Matthews <Th****************************@sbcglobal.net> wrote in
news:oY*******************@newssvr33.news.prodigy. com:
Hi,

I'm writing code for an embedded system. In the system a
Timer has 4 memory mapped registers of 32-bit lengths
in contiguous locations:
Timer 0:
0x1000 Configuration register
0x1004 Control register
0x1008 Input register
0x100C Output register.

My objective is to declare a type for the timer,
so I can write the following:
Timer * pTimers = (Timer *) 0x1000;
pTimers[1]->Config_Reg = 0x24;
/* or */
pTimer++; /* points to next timer */

My choices are to declare a typedef of an array of
4 32-bit {unsigned} integers or a structure with
4 32-bit {unsigned} integers.


I use the struct version all the time in embedded systems since I don't
care so much about portability at this level. However, I also rigorously
ensure that the compiler can pack the struct such that the elements end up
at the expected locations.

--
- Mark ->
--
Nov 14 '05 #2
Thomas Matthews wrote:
My choices are to declare a typedef of an array of
4 32-bit {unsigned} integers or a structure with
4 32-bit {unsigned} integers.
Another choice is a structure with an array of 4 32-bit unsigned ints,
which gives you both the assurance that there will be no padding and
normal C object semantics. (I doubt that there will be any padding
between 32-bit structure elements in practice, anyway.)
Is there any preference between an array typedef
and a structure for readability?


Yes. Arrays are not first-class in C, so they behave somewhat
differently to other objects. This means that a typedef for an array
is a leaky abstraction: it will be apparent to the user (of the
typedef) that the "opaque" type is really an array type. In
particular, the user will not be able to assign one instance of the
type to another and may find that a function to which such an object
is passed is able to modify the object (because the array is passed as
a pointer to the first element; if the object had struct type a copy
of the whole object would be passed as with builtin types).

Jeremy.
Nov 14 '05 #3
Mark A. Odell wrote:
Thomas Matthews <Th****************************@sbcglobal.net> wrote in
news:oY*******************@newssvr33.news.prodigy. com:

Hi,

I'm writing code for an embedded system. In the system a
Timer has 4 memory mapped registers of 32-bit lengths
in contiguous locations:
Timer 0:
0x1000 Configuration register
0x1004 Control register
0x1008 Input register
0x100C Output register.

My objective is to declare a type for the timer,
so I can write the following:
Timer * pTimers = (Timer *) 0x1000;
pTimers[1]->Config_Reg = 0x24;
/* or */
pTimer++; /* points to next timer */

My choices are to declare a typedef of an array of
4 32-bit {unsigned} integers or a structure with
4 32-bit {unsigned} integers.

I use the struct version all the time in embedded systems since I don't
care so much about portability at this level. However, I also rigorously
ensure that the compiler can pack the struct such that the elements end up
at the expected locations.


Adding

assert(sizeof(Timer) == sizeof(uint32) * 4); /* four 32 bit words,
no padding allowed */

would probably a struct of four ints a safe and convenient solution.
Nov 14 '05 #4
Tor Husabø wrote:
Mark A. Odell wrote:
Thomas Matthews <Th****************************@sbcglobal.net> wrote in
news:oY*******************@newssvr33.news.prodigy. com:
Hi,

I'm writing code for an embedded system. In the system a
Timer has 4 memory mapped registers of 32-bit lengths
in contiguous locations:
Timer 0:
0x1000 Configuration register
0x1004 Control register
0x1008 Input register
0x100C Output register.

My objective is to declare a type for the timer,
so I can write the following:
Timer * pTimers = (Timer *) 0x1000;
pTimers[1]->Config_Reg = 0x24;
/* or */
pTimer++; /* points to next timer */

My choices are to declare a typedef of an array of
4 32-bit {unsigned} integers or a structure with
4 32-bit {unsigned} integers.


I use the struct version all the time in embedded systems since I don't
care so much about portability at this level. However, I also rigorously
ensure that the compiler can pack the struct such that the elements
end up
at the expected locations.

Adding

assert(sizeof(Timer) == sizeof(uint32) * 4); /* four 32 bit words,
no padding allowed */

would probably a struct of four ints a safe and convenient solution.

....which the original poster already suggested himself. Sorry about
that one!
Nov 14 '05 #5

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

Similar topics

4
5620
by: Jens Mittag | last post by:
Hi! In my code, I have an array of a structure, which I want to save to a binary file. When the array is just created, everything works fine, but when I change contents of the array, saving...
8
2562
by: Steve Lambert | last post by:
Hi, I'd be grateful if someone could clarify this for me. In the linked list structure my intention is to declare an array of length 3 containing pointers to node eg. Node *Iterators The...
11
2090
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
0
1235
by: Vidya Bhagwath | last post by:
Hello Experts, I am porting the C++ code to the C#. In my C++ code, I have used structure of function pointers. Then I have created the array of that structure and intialized that array. Here...
0
367
by: Adam Warner | last post by:
Hi all, One cannot return a pointer to an array in C since there are no first class array types. But one can return a pointer to an incomplete array via the illegal but widely supported zero...
12
3856
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
11
3755
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
1
2113
by: peary | last post by:
Hi, everyone, I'm writing a program to discover wireless network using Windows Native Wifi API & VB.net. I have to declare the windows API in my VB.net program. The original windows...
1
2067
by: opendep | last post by:
hi, my project has a header file structure.h which has the following code #ifndef STRUCTURE_H #define STRUCTURE_H #include <stdio.h> typedef struct {
0
7228
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
7128
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
7332
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
7502
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
5635
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,...
1
5057
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
3206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.