LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 05-06-2013, 05:38 PM   #1
timl
Member
 
Registered: Jan 2009
Location: Sydney, Australia
Distribution: Fedora,CentOS
Posts: 286

Rep: Reputation: 41
help with C


Hi, I am trying to learn C & I have a little personal project converting a VMS/Pascal program into C (I maintain the VMS system). This program involves passing messages through a TCP gateway. As the proprietary VMS program is not going to change in a hurry I need to ensure all C message formats match the structure of the current formats.

The Pascal compiler under VMS contains a neat little switch where I can dump all messages and their structures. Below is a simple example. The structure only contains 6 bits but occupies 1 byte. When this record is included in another structure it will be defined as occupying 1 byte.


Code:
                                      1 Byte    WS{In PROGRAM ABCD} = PACKED RECORD 
Size                 0 Bytes          1 Bit         S1 : BOOLEAN
Align; Size          1 Bit            1 Bit         S2 : BOOLEAN
Align; Size          2 Bits           1 Bit         S3 : BOOLEAN
Align; Size          3 Bits           1 Bit         S4 : BOOLEAN
Align; Size          4 Bits           1 Bit         S5 : BOOLEAN
Align; Size          5 Bits           1 Bit         S6 : BOOLEAN
                                                    END
Is there a similar facility in C under Linux. FYI I am using Fedora 18 and gcc (GCC) 4.7.2 20121109 (Red Hat 4.7.2-8).

EDIT: I can use sizeof to establish the size of each field and struct but this is a tad time consuming.

Thanks

Last edited by timl; 05-07-2013 at 12:11 AM.
 
Old 05-07-2013, 12:59 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 1,527

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Compile with debug on (-g) and print the value in gdb.
 
Old 05-07-2013, 06:33 PM   #3
timl
Member
 
Registered: Jan 2009
Location: Sydney, Australia
Distribution: Fedora,CentOS
Posts: 286

Original Poster
Rep: Reputation: 41
thanks but this seems to just print the value allocated to the variable. Is that right? Or is there a switch I need to apply? I want to know the actual storage space allocated to the variable. No matter as there must be something in the debugger I can use. I can read up on debug commands
 
Old 05-09-2013, 11:37 AM   #4
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 1,527

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
p sizeof(varname)
 
Old 05-09-2013, 10:35 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,765

Rep: Reputation: 494Reputation: 494Reputation: 494Reputation: 494Reputation: 494
untested:

Code:
typedef struct bit8 {
    unsigned:1 bit[8];
} bit8;

bit8 mybyte;

*(unsigned char *)&mybyte= 0;
mybyte.bit[0]= 1;

printf ("mybyte=%02x\n", *(unsigned char *)&mybyte)
 
Old 05-09-2013, 11:03 PM   #6
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 30
C provides bit fields and bitwise operators & or &=, | or |=, ^ or ^=, and ~. If I care about the bit position I declare constants in the header file like
Quote:
#define BIT_0 1
#define BIT_1 2
and use the bit operator | to set the bit and use & with ~ to clear the bit.
Quote:
bitVariable |= BIT_0 //set bit zero in bitVariable
bitVariable &= ~BIT_1 // clear bit one in bitVariable
 
Old 05-10-2013, 03:59 PM   #7
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 30
Defining bits is usually done in hexadecimal. 0x01 or 0x0001 is the least significant bit. 0x02 is the next bit. Other bits are 0x04, 0x08, 0x10, 0x20, and so forth. Testing any single bit uses AND.
Quote:
if( bitVariable & BIT_0 ) // using defines in previous post
{
... // do this if bit was set
}
else
{
... // do this if bit was clear
}
 
Old 05-16-2013, 03:17 AM   #8
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 627

Rep: Reputation: 137Reputation: 137
First you can get the limits and standard storage sizes in limits.h (mostly probably).
Next, sizeof with struct will give you the result though not as formatted as your PASCAL compiler. What concerns me is the PACKED RECORD in the description. Is it implied that the PACKing is like an integer of is it PACKED-BCD or something else.

An octal dump of the required PASCAL output (from the running application) will tell what you want.

OK
 
Old 05-16-2013, 09:32 AM   #9
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,377

Rep: Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108
Personally, what I would select as a target language, if possible, is the GCC implementation of Pascal.

Porting it from one implementation of the same language to another is "porting." The other is "rewriting." Since the GCC language-suite supports a number of different languages (all of them basically being variations of the front-end with the same back-end ...), this would be both (maybe) a faster and a less-risky proposition.

Last edited by sundialsvcs; 05-16-2013 at 09:33 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



All times are GMT -5. The time now is 07:22 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration