473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Treating 'char' Structure Members as Contiguous Bytes

The following compiles cleanly on gcc3.0.4 using -W -Wall -ansi -
pedantic. Lint gives this warning:
_
p_member_09 = p_start + OFFSET_TO_MEMBER_09;
t_offset.c 27 Warning 416: creation of out-of-bounds pointer (33
beyond end of data) by operator ptr+int' [Reference: file t_offset.c:
lines 26, 27]

The underscore Lint uses to indicate the point of warning is over the
'O' in OFFSET_TO_MEMBER_09.

My question is this. Is the technique used here well-defined and safe
practice?
typedef unsigned char uchar;

struct byte_array {
uchar member_01;
uchar member_02[8];
uchar member_03[2];
uchar member_04[2];
uchar member_05[8];
uchar member_06[2];
uchar member_07[2];
uchar member_08[8];
uchar member_09[2];
uchar member_10[2];
};

struct byte_array byte_array;

#define OFFSET_TO_MEMBER_06 21
#define OFFSET_TO_MEMBER_09 33

int main( void )
{
uchar *p_start;
uchar *p_member_09;

p_start = &byte_array.member_01;
p_member_09 = p_start + OFFSET_TO_MEMBER_09;

return 0;
}
Feb 7 '08 #1
3 1454
Martin wrote:
The following compiles cleanly on gcc3.0.4 using -W -Wall -ansi -
pedantic. Lint gives this warning:
_
p_member_09 = p_start + OFFSET_TO_MEMBER_09;
t_offset.c 27 Warning 416: creation of out-of-bounds pointer (33
beyond end of data) by operator ptr+int' [Reference: file t_offset.c:
lines 26, 27]

The underscore Lint uses to indicate the point of warning is over the
'O' in OFFSET_TO_MEMBER_09.

My question is this. Is the technique used here well-defined and safe
practice?
typedef unsigned char uchar;

struct byte_array {
uchar member_01;
uchar member_02[8];
uchar member_03[2];
uchar member_04[2];
uchar member_05[8];
uchar member_06[2];
uchar member_07[2];
uchar member_08[8];
uchar member_09[2];
uchar member_10[2];
};

struct byte_array byte_array;

#define OFFSET_TO_MEMBER_06 21
#define OFFSET_TO_MEMBER_09 33

int main( void )
{
uchar *p_start;
uchar *p_member_09;

p_start = &byte_array.member_01;
p_member_09 = p_start + OFFSET_TO_MEMBER_09;

return 0;
}
It is not defined because the struct may have padding between members
(although it is unlikely when the members are arrays of unsigned char).

You can instead write
p_member_09 = byte_array.member_09;
or
p_member_09 = (uchar*)byte_array + offsetof (struct byte_array, member_09);
--
Thad
Feb 7 '08 #2
Martin <ma************@which.netwrote:
The following compiles cleanly on gcc3.0.4 using -W -Wall -ansi -
pedantic. Lint gives this warning:
_
p_member_09 = p_start + OFFSET_TO_MEMBER_09;
t_offset.c 27 Warning 416: creation of out-of-bounds pointer (33
beyond end of data) by operator ptr+int' [Reference: file t_offset.c:
lines 26, 27]
My question is this. Is the technique used here well-defined and safe
practice?
I'll let others figure out the exact safety of the technique you use -
my first instincts say that it's undefined behaviour in theory, but very
unlikely to break - and suggest that regardless of the outcome, you
switch to a guaranteed safe technique: use offsetof(). You'll need to
#include <stddef.h>.

Richard
Feb 7 '08 #3
Martin wrote:
[...]
typedef unsigned char uchar;

struct byte_array {
uchar member_01;
uchar member_02[8];
[...]
uchar member_10[2];
};

struct byte_array byte_array;

#define OFFSET_TO_MEMBER_06 21
#define OFFSET_TO_MEMBER_09 33
#include <stddef.h>
>
int main( void )
{
uchar *p_start;
uchar *p_member_09;

p_start = &byte_array.member_01;
p_member_09 = p_start + OFFSET_TO_MEMBER_09;
Why not p_start + offsetof(byte_array, member_09)?
[...]
--
Army1987 (Replace "NOSPAM" with "email")
Feb 8 '08 #4

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...
3
by: sieg1974 | last post by:
Hi, I have made this simple program to understand char ** pointers, but I still having many questions. int main() { char ** testPointerPointerChar = 0; char * A = "string01";
4
by: Thomas Matthews | last post by:
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...
13
by: Vijay Kumar R. Zanvar | last post by:
Hello, I have few questions. They are: 1. Is "const char * const *p;" a valid construct? 2. How do I align a given structure, say, at 32-byte boundary? 3. Then, how do I assert that a given...
31
by: aarklon | last post by:
Hi all, this is a question which i saw in a book typedef struct mall_li_header_ { int refcnt; uchar pool; uchar flag; ushort magic_no; char data;
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
43
by: emyl | last post by:
Hi all, here's an elementary question. Assume I have declared two variables, char *a, **b; I can then give a value to a like a="hello world";
4
by: ...vagrahb | last post by:
Hi, I have the following structure struct Format { char x; unsigned char a; unsigned char b; unsigned char c; char y;
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
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,...

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.