www.digitalmars.com         C & C++   DMDScript  

c++.command-line - Re: aligment

reply Nic Tiger <g_tiger progtech.ru> writes:
Hi guys,

does anyone know useful way of detecting problems with different struct 
layout when aligning 8 or 4 bytes?

problem is my old code used to have -a4 for DOSX, and when I ported it 
to win32 I had to change alignment from default 8 to needed 4 just in 
header (otherwise some structs changed size and layout).

now I want to switch fully to 8-byte alignment for both DOSX and Win32, 
but I'm afraid that some structs will change (they may or may not be 
read/written, so it is potentially problem)

it would be very convenient to have compiler switch to issue warnings 
about such structures, but may be there are specialized tools for that?

really don't know how to ask google about this :)

Nic Tiger.
Mar 31 2006
parent reply "tjulian" <tjulian removethis.oldi.com> writes:
Nic Tiger wrote:
 does anyone know useful way of detecting problems with different
 struct layout when aligning 8 or 4 bytes?
 
In C, the sizeof operator and/or offsetof macro can be used to check proper alignment at runtime, but you have to know the size (for sizeof) or offset (offsetof) beforehand. In C++ (and maybe C99?), I think sizeof can be used at compile-time to check the size of a structure. -- Tim
Mar 31 2006
parent reply Nic Tiger <g_tiger progtech.ru> writes:
tjulian wrote:
 In C, the sizeof operator and/or offsetof macro can be used to check
 proper alignment at runtime, but you have to know the size (for sizeof)
 or offset (offsetof) beforehand. In C++ (and maybe C99?), I think
 sizeof can be used at compile-time to check the size of a structure.
 
 --
 Tim
to make problem clear, I must say that I have MANY classed/structs (100+) and don't want check manually all of them for compiler it would be easy to detect such things, and I would just rebuilt all my project to see whether problem exists when problem structures highlighted, I would fix alignment rather easily (just suround it with #pragma pack). but how to spot these structures automatically and quickly? Nic Tiger
Mar 31 2006
parent reply Nicholas Jordan <Nicholas_member pathlink.com> writes:
In article <e0jej3$2n3t$1 digitaldaemon.com>, Nic Tiger says...
tjulian wrote:
 In C, the sizeof operator and/or offsetof macro can be used to check
 proper alignment at runtime, but you have to know the size (for sizeof)
 or offset (offsetof) beforehand. In C++ (and maybe C99?), I think
 sizeof can be used at compile-time to check the size of a structure.
 
 --
 Tim
to make problem clear, I must say that I have MANY classed/structs (100+) and don't want check manually all of them for compiler it would be easy to detect such things, and I would just rebuilt all my project to see whether problem exists when problem structures highlighted, I would fix alignment rather easily (just suround it with #pragma pack). but how to spot these structures automatically and quickly? Nic Tiger
.. the sizeof operator gives you the sizeof() - not the alignment. I am not degreed in the science, but two books - both 1,000 pages each, by people who are - discuss this problem. They give an example and show that you must name each data member individually when doing ops on a struct, and that attempting to take the address of a stuct - then copy or something - leads to data corruption. Then state that only the compiler can keep track of all of this. I found, by hard-experience (my favorite teacher) that when dealing with this many data items, I would write a stubb that would write out the header file for me, thereby taking the automated data methods of the language it's self into the problem domain to apply the power of the language to the coding's challenges. I don't understand why the approach of totally rewriting the entire project under the new (compilation model) does not immediately come to mind. You are gonna have to do it one day - cross compilation may be effective for your problem, only you understand all of the matters of fact and details of design, but you would only need to spot these structures automatically and quickly if you had them scattered all over the project. Why would you need to do that ? (not to challenge your design intent, just to understand the problem) This challenge intrigues me,.... Nick http://www.docdubya.com/belvedere/statement/index.html
Apr 07 2006
parent reply Nic Tiger <g_tiger progtech.ru> writes:
Nicholas Jordan wrote:
 I don't understand why the approach of totally rewriting the entire project
 under the new (compilation model) does not immediately come to mind.
I *AM* rewriting most important parts of it, but I have to maintain stability (as it is stable now and is used by many people) for transitional period and have to support older files. The solution I found so far, is to write Perl script for Understand C++ which will generate program that will examine and print out offsets of all members of all structs/classes. Then I have to compare output of program compiled with different switches and take measures. The initial intention was to use something common for this, but again I have to write all this by myself... Nic Tiger.
Apr 07 2006
next sibling parent reply Scott Michel <scottm aero.org> writes:
Nic Tiger wrote:
 Nicholas Jordan wrote:
 I don't understand why the approach of totally rewriting the entire
 project
 under the new (compilation model) does not immediately come to mind.
I *AM* rewriting most important parts of it, but I have to maintain stability (as it is stable now and is used by many people) for transitional period and have to support older files. The solution I found so far, is to write Perl script for Understand C++ which will generate program that will examine and print out offsets of all members of all structs/classes. Then I have to compare output of program compiled with different switches and take measures.
It's pretty dangerous to rely on offsets unless you absolutely must. The only time I really ever encountered needing to do something like what you're describing is when I wrote structures directly to disk and had to read them back in a different machine with a different alignment/padding arrangement. Basically, you're stuck with having to write that Perl script. There's really no other good way of doing what you need. -scooter
Apr 10 2006
parent reply "Bob Paddock" <graceindustries gmail.com> writes:
On Mon, 10 Apr 2006 12:55:12 -0400, Scott Michel <scottm aero.org> wrote:

 The solution I found so far, is to write Perl script for Understand C++
 which will generate program that will examine and print out offsets of
 all members of all structs/classes.
 Basically, you're stuck with having to write that Perl script. There's
 really no other good way of doing what you need.
Use 'offsetof()' for structures, see this for example: http://www.embedded.com/showArticle.jhtml?articleID=18312031 'offsetof()' is standard C hiding away in <stddef.h> .
Apr 10 2006
parent Nicholas Jordan <Nicholas_member pathlink.com> writes:
In article <op.s7sx14e2ocfjro grace002.graceinc.com>, Bob Paddock says...
On Mon, 10 Apr 2006 12:55:12 -0400, Scott Michel <scottm aero.org> wrote:

http://www.embedded.com/showArticle.jhtml?articleID=18312031
returns: "Unable to access the article page you requested. Please check your request and try again, or return to the homepage." M$IE 6.0.28 A dog will treat you like one of the family. A cat will treat you like one of the staff.
Apr 15 2006
prev sibling parent Nicholas Jordan <Nicholas_member pathlink.com> writes:
In article <e17kk3$13d0$1 digitaldaemon.com>, Nic Tiger says...
Nicholas Jordan wrote:
 I don't understand why the approach of totally rewriting the entire project
 under the new (compilation model) does not immediately come to mind.
I *AM* rewriting most important parts of it, but I have to maintain stability (as it is stable now and is used by many people) for transitional period and have to support older files. The solution I found so far, is to write Perl script for Understand C++ which will generate program that will examine and print out offsets of all members of all structs/classes. Then I have to compare output of program compiled with different switches and take measures. The initial intention was to use something common for this, but again I have to write all this by myself... Nic Tiger.
I gave some deep thought to the issuses while responding as though I had your undivided attention for half-an-hour at 4 a.m. Fully thought out, the reply runs 4.10 kb, so I put it up in http://www.digitalmars.com/drn-bin/wwwnews?cpp.chat I have quite a lot of experience navigating through this phase of project development; it is dicey and warants review of my post if it does not pull you away from your duties http://www.docdubya.com/belvedere/cpp/cppfaq.htm "One day, when I was sitting in my office, I thought of this little scheme which would reinvigorate computer-think. The motif was that STL would be the only way to go if you wanted to retain your sanity.."
Apr 16 2006