473,434 Members | 1,854 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,434 software developers and data experts.

disregard: sizeof struct returning unexpected results

I perused the archives of comp.lang.c and found some answers related
to addressability of struct elements (only in arrays?), and will
probably proceed by using byte or char arrays filled with raw data and
then converting to the expected types. Unless anyone has any helpful
or enlightening comments, I will leave this issue closed for now.

Thanks again for the time!
Nov 14 '05 #1
1 1313
On 20 Aug 2004 09:54:33 -0700, se******@bluebeard.org (Sean) wrote in
comp.lang.c:
I perused the archives of comp.lang.c and found some answers related
to addressability of struct elements (only in arrays?), and will
probably proceed by using byte or char arrays filled with raw data and
then converting to the expected types. Unless anyone has any helpful
or enlightening comments, I will leave this issue closed for now.

Thanks again for the time!


No, it has nothing at all to do with whether you define a single item
of a type or an array of them. The distance between one member of an
array of type T and the next is sizeof(type T), and the size of a
free-standing object of type T is also sizeof(type T).

Using an array of unsigned chars is a good idea, but be careful about
"converting" to the "expected" types. On some platforms the alignment
imposed by the compiler is required by the underlying processor
hardware, and violating it can have serious consequences.

Assuming sizeof(short) is 2 and sizeof(int) is 4, then you could use
something like this to pass the data around:

unsigned char data [6];

But this is a bad idea:

unsigned short us;
unsigned int ui;
unsigned char data [6];

/* get data into 'data' */

us = *(unsigned short *)data;
ui = *(unsigned long *)(data + 2);

....on some processors this produces code that runs more slowly but
correctly. On others, including newer RISC architectures, either you
get the wrong value or a hardware trap that terminates the program.

The safe way to transfer the data is:

memcpy(&us, data, 2);
memcpy(&ui, data + 2, 4);

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2

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

Similar topics

15
by: ak | last post by:
struct xy{ int x; int y; } _xy; size_of_xy(struct xy * a) { int len = sizeof a; printf( "\sizeof xy: %i\n", len ); }
10
by: Sean | last post by:
I have a struct that I wrote to test a protocol. The idea I had was to just declare the elements of the struct in the order in which they are sent and received as defined by the protocol. ...
2
by: aegis | last post by:
sizeof could not possibly evaluate to some size for a type, unless two things for a type occur: a) the type is complete and sizeof will evaluate to appropriate size b) type is not complete and...
37
by: JohnGoogle | last post by:
Hi, Newbie question... After a recent article in VSJ I had a go at implementing a Fraction class to aid my understanding of operator overloading. After a previous message someone suggested...
8
by: Chameleon | last post by:
I have a TGA image header struct. TGA has 18 bytes header, so the C struct too. why this return 20? sizeof(TGAHeader) I saw this in many structs. I believe compiler round up the size to 4...
10
by: Yevgen Muntyan | last post by:
Consider the following macro: #define ALLOCIT(Type) ((Type*) malloc (sizeof (Type))) The intent is to wrap raw memory allocation of N bytes into a macro which returns allocated chunk of memory...
72
by: goacross | last post by:
char ch='a'; int v=sizeof ++ch; cout<<ch<<endl;// output: 'a' why not 'b'? thanks
40
by: Boltar | last post by:
Hi Why - using gcc on linux - does this return 0 in C but returns 1 in C+ +? I don't get it. #include <stdio.h> struct foo { };
4
by: MTsoul | last post by:
I noticed some quirks with C++ (not sure if this is the same for C). I have: struct A { char a; }; struct B { char b;
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.