473,769 Members | 5,072 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(s truct 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.l earn.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 2824
Thomas Matthews <Th************ *************** *@sbcglobal.net > wrote in
news:oY******** ***********@new ssvr33.news.pro digy.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******** ***********@new ssvr33.news.pro digy.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(T imer) == 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******** ***********@new ssvr33.news.pro digy.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(T imer) == 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
5634
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 results in a file, which doesn't hold the information of the changed array anymore. I dont know why. Here is the code: /*
8
2583
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 compiler seems to interpret this as a pointer to an array of 3 nodes instead. This interpretation ensures that the second assigment to mynode below fails compilation with the given message.
11
2125
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
1254
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 is the code typedef struct { unsigned char (CMyclass::*Construct)(LINKINFO&); unsigned char (CMyclass::*parse)(LINKINFO&,LINKINFO&,unsigned
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 array struct hack: #include <stdlib.h> typedef struct byte_vector_t byte_vector_t;
12
3885
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 looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
11
3785
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
2133
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 declaration is as below. The problem is in "struct _WLAN_AVAILABLE_NETWORK_LIST", it declared "WLAN_AVAILABLE_NETWORK Network;". I think which means a Network array of struct WLAN_AVAILABLE_NETWORK.
1
2082
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
9423
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10214
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9996
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3963
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 we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.