Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » unexpected output while reading the size of structure.
unexpected output while reading the size of structure. [message #34753] Sat, 10 December 2011 05:39 Go to next message
Shwetha is currently offline  Shwetha
Messages: 39
Registered: August 2011
Member
Hii,


Please find the attached test case for the reference..in which I am trying to read the size of the following structure.

struct read_data
{
int i;
char c;
double d;

};
read_data r;

Output :


Size of int = 4
Size of char = 1
Size of double = 8

Size of r.i = 4
Size of r.c = 1
Size of r.d = 8

size of struct read_data = 16

Expected size of the struct read_data is 13 bytes.But giving the wrong output..Would you please let me know the issue behind this.


Thank You,

Shwetha S

Re: unexpected output while reading the size of structure. [message #34754 is a reply to message #34753] Sat, 10 December 2011 05:58 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
Data structure alignment
Structure Alignment Examples


Regards,
Novo
Re: unexpected output while reading the size of structure. [message #34755 is a reply to message #34753] Sat, 10 December 2011 08:54 Go to previous messageGo to next message
Shwetha is currently offline  Shwetha
Messages: 39
Registered: August 2011
Member
Hi,

Using struct PACKED we can read the size of the structure correctly.

#ifndef PACKED
#define PACKED  __attribute__((__packed__))
#endif

typedef struct  PACKED
		{
			int i;
			char c;
			double d;
			
		}read_data;
		
	read_data r;
	
	
};


Thanks & Regards,

Shwetha S
Re: unexpected output while reading the size of structure. [message #34771 is a reply to message #34755] Mon, 12 December 2011 09:41 Go to previous message
cbpporter is currently offline  cbpporter
Messages: 1401
Registered: September 2007
Ultimate Contributor
The size of 16 is correct. There are very good technical reasons for it to be so and Novo pointed out some links. Unless you have very good reasons to use an unaligned/packed struct, I would recommend against it.

One thing that you could try is reorder the elements. There is this old trick of ordering elements, with the largest/more alignment constrained ones first. So put the double first, then the int, then the char.

If you are using binary storage an rely on fixed size (like 13) the solution is to read/write each field separately.
Previous Topic: dli file for Oracle10g
Next Topic: gdk_pixbuf-2.0 linker error
Goto Forum:
  


Current Time: Sat Apr 20 11:09:56 CEST 2024

Total time taken to generate the page: 0.02592 seconds