LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-24-2024, 11:11 AM   #1
vmelkon
Member
 
Registered: Feb 2007
Location: Canada
Distribution: Kubuntu 22.04
Posts: 549

Rep: Reputation: 84
Struct size issue - Tell C++ not to add bytes to a struct


Hello,
I have my own BMP file reader. It is written in C++.
I am compiling it for Linux, using Qt Creator so underneath it, I think it uses gcc.

So, I think it is only natural to have structure that englobe a set of data.
A lot of file formats use some kind of structure.

Code:
struct BITMAPFILEHEADER
{
	ushort	bfType;
	uint	bfSize;
	ushort	bfReserved1;
	ushort	bfReserved2;
	uint	bfOffBits;
};


We tend to write the structure to file via something like
Code:
BITMAPFILEHEADER BitmapFileHeader;
....
....
fwrite(&BitmapFileHeader, 1, sizeof(BITMAPFILEHEADER), hfile);
BITMAPFILEHEADER should be 14 bytes but gcc has turned that into 16 bytes.
In other words, the BMP file created is not valid.

There is this:
https://en.cppreference.com/w/cpp/language/alignas

but I think that just effects start address. It helps with CPU optimization to have data start at certain addresses.

Is there something I can put in the C++ code to make it NOT ADD additional bytes to my struct?

It is possible to do it with compiler flags but I would prefer to do it in the C++ code.


That sort of code is very common for writing/reading a file, for sending some data over a network.
If the compiler does whatever it wants, then it can break your code.
 
Old 01-24-2024, 12:35 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,887
Blog Entries: 1

Rep: Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873
Code:
#pragma pack(push,2)
struct BITMAPFILEHEADER
{
	ushort	bfType;
	uint	bfSize;
	ushort	bfReserved1;
	ushort	bfReserved2;
	uint	bfOffBits;
};
#pragma pack(pop)
 
3 members found this post helpful.
Old 01-28-2024, 12:09 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,708
Blog Entries: 4

Rep: Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949
“Slack bytes” within structs are common with many modern compilers, because they allow faster code to be generated for modern pipelined CPUs. But “packing” is available when you need to control exactly how the bytes are laid out. Different compilers (and, versions thereof) do it in different ways, and may use different pragmas (if they do).

Last edited by sundialsvcs; 01-28-2024 at 12:11 PM.
 
1 members found this post helpful.
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gpg: do_plaintext(): wrote 1210414045 bytes but expected 822504068 bytes MensaWater Linux - Software 6 02-27-2018 08:48 AM
Is it okay if my sector size is "512 bytes / 4096 bytes" Altiris Slackware 5 07-31-2015 03:19 AM
does logrotate set size to 1048576 bytes by default if no size is specified? finebanana Red Hat 0 01-22-2009 12:10 AM
TX bytes vs. httpd bytes ovrload Linux - Networking 3 10-12-2005 04:19 PM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration