Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-10-2007, 07:13 PM
Eagle_ Eagle_ is offline
Newbie
 
Join Date: Nov 2007
Posts: 2
Rep Power: 0
Eagle_ is on a distinguished road
Default sizeof Struct [SOLVED]

Hi all,

I know alot of people had asked question about structs and why does the size of them doesnt match the expected size, so why it doesnt match i know! I use C2D 64bits processor.

Those are my structs:
Code:
#include <stdint.h>
#define u1 uint8_t
#define u2 uint16_t
#define u4 uint32_t
#define i2 int16_t
#define i4 int32_t
Code:
typedef struct{
				// SV status [bitfield], or
				// Delta pseudo-range [0.02 meters].
				// SV status [bitfield]:
	u1 NavStatus:6;		// 5...0: SV navigation status
				// Delta pseudo-range [0.02 meters]:
				// [full pseudo-range for given slot]-[refrange]
	u1 ChannelNum:5;	// 10...6: Channel number [0...31], 31 - unavailable
	u1 GLONASS_Slot_num:5;	// 15...11: GLONASS slot number (for GPS SV the field
				// is undefined), [0...24], 0 - unknown
} SV_Status_;

typedef struct {		// Packed data 1 [bitfield]:
	u1 SNR:6;		// 5...0: Signal-to-noise ratio [dB*Hz]	
	u1 LockTime:1;		// 6: lock time is available
	u1 SLL_Flag:1;		// 7: signal lock loop flags are available
	u1 Reserved:1;		// 8: reserved
	u1 SlotID:3;		// 11...9: slot ID:
				// 0 - C/A L1; 1 - P1; 2 - P2; 3 - C/A L2;
				// 4 - L5; 5,6,7 - reserved
	i4 CarrierPhaseDelta:20;// 31...12: [carrier phase] - [refrange]
				// [-2^19...(2^19-1)] [0.0005 meters]
} PackedData1_;
typedef struct __attribute__((__packed__)) {
	// Note: The zeroth element of the array Slot[i],i=0,...,M-1,
	// unlike the other elements, does not contain corrections
	// to the reference pseudo-range from the Header structure.
	// To provide the user with additional information, the flag
	// ?svst? is used for ?delrange? in the zeroth slot.
	 union __attribute__((__packed__)) {
		 i2	Delrange;
		 SV_Status_ Svst;
	};
	 			// SV status [bitfield], or
				// Delta pseudo-range [0.02 meters].
				// SV status [bitfield]:
				// 15...11: GLONASS slot number (for GPS SV the field
				// is undefined), [0...24], 0 - unknown
				// 10...6: Channel number [0...31], 31 - unavailable
				// 5...0: SV navigation status
				// Delta pseudo-range [0.02 meters]:
				// [full pseudo-range for given slot]-[refrange]
	PackedData1_ word1;	// Packed data 1 [bitfield]:
				// 31...12: [carrier phase] - [refrange]
				// [-2^19...(2^19-1)] [0.0005 meters]
				// 11...9: slot ID:
				// 0 - C/A L1; 1 - P1; 2 - P2; 3 - C/A L2;
				// 4 - L5; 5,6,7 - reserved
				// 8: reserved
				// 7: signal lock loop flags are available
				// 6: lock time is available
				// 5...0: Signal-to-noise ratio [dB*Hz]
	u2 flags;		// Signal lock loop flags (see [FC] message)
	u2 lock;		// Packed data 2 [bitfield]:
				// 15...12: fractional part of Signal-to-noise
				// ratio [0.1 dB*Hz]
				// 11...10: reserved
				// 9...0: lock time [0.1 second]. Tracking time since
				// last loss of lock. Varies between 0 and
				// 102.3 seconds. ?Gets stuck? at 102.3s after
				// the actual tracking time exceeds this value
				// (until another loss of lock occurs).
	u4 word2;		// Packed data 3. Only present for ?version? 0 [bitfield]:
				// 31...7: Doppler [-224...(224-1)], [0.001 Hz]
				// 6...0: reserved
 } SlotRec_14_;
WIth out __attribute__((__packed__)) the size of SlotRec_14_ would have bin 16 and with it, it is only 15, but it should have been 14 bytes.

Because the size of SlotRec_14_ is wrong, i get wrong values in my struct when i debug the data! I cant explain why in other structs with bits separation there were no problems.

For my question, i am not so advance C programmer and i would like to know how would advance programer would have write such kind of decoder. If he\she would have used bitwise operators to decode this data or structs? Or is there another way in C?

Thanks

Last edited by Eagle_; 11-18-2007 at 11:16 AM. Reason: SOLVED
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 11-12-2007, 11:09 AM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 793
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

Check to see what the byte alignment is set to on your compiler. Structures are bloated to fit the nearest byte alignment boundary, so with 16-bit alignment a structure of 7 bytes will have an extra byte tagged onto the end to make it 8 bytes.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-18-2007, 11:15 AM
Eagle_ Eagle_ is offline
Newbie
 
Join Date: Nov 2007
Posts: 2
Rep Power: 0
Eagle_ is on a distinguished road
Default

My problem was solved be changing struct SV_Status_ to the following:
Code:
typedef struct{
	u2 NavStatus:6;
	u2 ChannelNum:5;
	u2 GLONASS_Slot_num:5;
} SV_Status_;
by changing the variable from one byte to two bytes, the compiler align the bits into 16bits instead of 8bits like before.

Then the size of SlotRec_14_ became 14 as desired.

Thanks for all
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Struct in C and C++ dirkfirst C and C++ 9 03-05-2008 06:28 AM
Flex, bison multifunction calculator annatsos C and C++ 1 01-04-2007 07:00 AM


All times are GMT -5. The time now is 03:50 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads