View previous topic :: View next topic
|
Author |
Message |
ismail.hasan
New User
Joined: 28 Mar 2007 Posts: 21 Location: India
|
|
|
|
Hi,
I have 2 doubts.
1) is it possible to redefine the comp variable (p.s. its not comp-3 its comp)?
If so is it possible to redifine with cross datatypes
For e.g.
01 VAR1 PIC S9(06) comp
02 VAR2 REDEFINES VAR1
03 VAR3 PIC X(02)
03 VAR4 PIC s9(04) comp
2) is it possible to redifne the X variable with comp variable i.e
01 var1 pic x(04)
02 var2 redifnes var1
03 var3 pic s9(02) comp
Please let me whether above things are possible and how the mem location is allocated..?
Thanks in advance
Noor Mohammed Ismail |
|
Back to top |
|
|
Arun Raj
Moderator
Joined: 17 Oct 2006 Posts: 2481 Location: @my desk
|
|
|
|
Hi Noor Mohammed Ismail
It is possible to redefine a COMP data-item with an alphanumeric data-item and vice-versa provided the level-nos of both the redefined and the redefining data-items are same.
Thanks
Arun |
|
Back to top |
|
|
ismail.hasan
New User
Joined: 28 Mar 2007 Posts: 21 Location: India
|
|
|
|
hi arcvns,
Thanks for your reply.
My understanding from your reply is the way wat i have declared is not possible and if the pic is placed 02 level then it will work.
is my understanding is write? if not please educate in till bit ellabrate, if you dont mind.
If my understanding is write how the mem allocation will be for the second case the x(04) redifined by s9(02) comp. where s9(02) comp occupies only 2 byte and what happens to remaining 2 byte. will it through an error or it will be ingored.
Please help me |
|
Back to top |
|
|
Arun Raj
Moderator
Joined: 17 Oct 2006 Posts: 2481 Location: @my desk
|
|
|
|
Hi
Code: |
01 A PIC S9(6) COMP.
01 B REDEFINES A PIC X(2) |
Code: |
01 A PIC X(2).
01 B REDEFINES A PIC S9(6) COMP |
if the redefining data-item is having a shorter length, the original data will get truncated and it wont give any error
Thanks
Arun |
|
Back to top |
|
|
Phrzby Phil
Senior Member
Joined: 31 Oct 2006 Posts: 1049 Location: Richmond, Virginia
|
|
|
|
Quote: |
01 VAR1 PIC S9(06) comp
02 VAR2 REDEFINES VAR1
03 VAR3 PIC X(02)
03 VAR4 PIC s9(04) comp
|
has a problem.
1. S9(6) COMP allocates a fullword - 4 bytes, and is pure binary, so a negative value will have the leftmost bit = 1, using complement arithmetic.
2. S9(4) COMP allocates a halfword - 2 bytes, with the same comments re binary.
3. Therefore, your redefines may be shifting VAR4 to the right OK, but its sign bit will not be aligned with VAR1's sign bit. |
|
Back to top |
|
|
ismail.hasan
New User
Joined: 28 Mar 2007 Posts: 21 Location: India
|
|
|
|
Thanks lot arcvns. Very kind of u |
|
Back to top |
|
|
ismail.hasan
New User
Joined: 28 Mar 2007 Posts: 21 Location: India
|
|
|
|
very clear Phrzby Phil |
|
Back to top |
|
|
mmwife
Super Moderator
Joined: 30 May 2003 Posts: 1592
|
|
|
|
Hi Ismail,
You can redefine any variable any way you like (obeying the compiler rules, of course), but you must first have a firm idea of how you plan to use the results. That, you didn't tell us.
Without it we can't determine if your redefine will give you the results you want.
Also your VAR1 field is 4 bytes long and you redefine it with 2 fields totaling 6 bytes. I think this will promt a compiler error.
In any event, you might want to reply and tell us what you want to accomplish AFTER the field is redefined. |
|
Back to top |
|
|
ismail.hasan
New User
Joined: 28 Mar 2007 Posts: 21 Location: India
|
|
|
|
Thanks for the reply mmwife.
I in study process. I am not going to implement in any practical envi.?
The thing is the layout which i have mention will work or not...?
and the same time if refined variables length is greater than redfine varibale what will be result.? whether the extra datas will truncate or will through error and what happend if the difference is in negative..? |
|
Back to top |
|
|
William Thompson
Global Moderator
Joined: 18 Nov 2006 Posts: 3156 Location: Tucson AZ
|
|
|
|
ismail.hasan wrote: |
and the same time if refined variables length is greater than redfine varibale what will be result.? whether the extra datas will truncate or will through error and what happend if the difference is in negative..? |
If the area being redefined (the original area) is larger than the redefining area, this might raise a "W" compile error but nothing is truncated.
If the area being redefined (the original area) is smaller than the redefining area and if the compiler allowed it (it could raise an "S" or "E" compile error) it would allocate enough storage for the longer definition, again nothing would be truncated. |
|
Back to top |
|
|
Phrzby Phil
Senior Member
Joined: 31 Oct 2006 Posts: 1049 Location: Richmond, Virginia
|
|
|
|
Jack -
VAR1 is not 6 bytes - it is 4 bytes - note it is COMP.
Same comment for VAR4 - it is 2 bytes.
S9(1) thru S9(4) COMP are halfwords (2 bytes), as a halfword can hold any 4-digit number. (It can hold some but not all 5-digit numbers.)
Similarly for S9(5) thru S9(9) COMP are all fullwords - 4 bytes.
COBOL note: COBOL gives you what you ask for, so if you have a S9(2) COMP, although a halfword is allocated, COBOL will truncate on the left to maintain at most a 2-digit number, up to +/- 99, as that is what you asked for. |
|
Back to top |
|
|
agkshirsagar
Active Member
Joined: 27 Feb 2007 Posts: 691 Location: Earth
|
|
|
|
Quote: |
COBOL note: COBOL gives you what you ask for, so if you have a S9(2) COMP, although a halfword is allocated, COBOL will truncate on the left to maintain at most a 2-digit number, up to +/- 99, as that is what you asked for. |
if I remember correctly, it also depends on TRUNC compiler option too.
see the link below to get an interesting discussion on similar topic.
http://www.ibmmainframes.com/viewtopic.php?t=19569&highlight=trunc |
|
Back to top |
|
|
mmwife
Super Moderator
Joined: 30 May 2003 Posts: 1592
|
|
|
|
Hi Phil,
I mis-read the quote below. My old eyes failed to pick up the "redefines" line, thinking that the whole thing was a redefines of the original var1 and added x(4) and 9(2) comp giving 6.
Quote: |
2) is it possible to redifne the X variable with comp variable i.e
01 var1 pic x(04)
02 var2 redifnes var1
03 var3 pic s9(02) comp |
That's why I said:
Quote: |
Also your VAR1 field is 4 bytes long and you redefine it with 2 fields totaling 6 bytes. I think this will promt a compiler error. |
Another senior moment. |
|
Back to top |
|
|
|