One Way to Abuse COBOL



  • Timeline: somewhere in the late '70s

    Environment: IBM mini with 32mb of memory; programming in COBOL; all batch processing

    Task: don't remember what, exactly, the job was supposed to do but to do it ... we had to have two arrays. The arrays were coupled - if one grew we didn't need as much space in the other one and vice-versa. Problem is, COBOL didn't (doesn't?) have variable length arrays and we didn't have fancy stuff like virtual memory. And, of course, as was normal at the time, we didn't have near enough memory to use.

    Important fact: using that compiler, COBOL would place things in memory in the exact order they were declared and would never move anything.

    Solution:

    I took on the impossible task.

    I declared array1 dimensioned to fill just about all the free memory we had available. Then, I created array2(1) right after it. I used a forward incrementing index in array1 eating away at the space following it, and then ...

    and then ...

    and then ...

    and then ...

    and then ...

    I set up a negative index on array2 to use up the space above array2. Lordie, I love COBOL. It actually worked but after typing the description out I feel a little bit dirty. I do remember that the code to watch for a collision between the two arrays was somwhat non-intuitive. Complicated arithmetic in COBOL is, uh, different.

    WTFs:
    1) Nobody else there could come up with such an elegant solution. So there, I win.
    2) COBOL allowed negative array indexes (indicies?). In fact, COBOL didn't give a flying fark about what the index was, you had to sort of police that yourself. Or not. Element # 4,782,487,999,104? Just a moment, I'll truncate, strip sign bits, and figure out exactly where in this data centre this is. Hmmm ... seems to be value 42.
    3) The guy that had to maintain that code after I left the company probably had a massive breakdown and never programmed again.
    4) I'm still proud of actually getting away with it and making it work.



  • It's not a bad idea if you have no better way to do things. I've seen similar done on embedded systems; get a pointer to the end of memory, and every time something is allocated, subtract the size from that pointer. Similar concept to negative indices.



  • I'm not much of a coder and COBOL's a long way before my time, but sometimes I guess you just have to do the best job you can with the tools available, namely a programming language with a few limitations and a compiler with a feature/quirk/bug/glaring design error that obliged you to come up with a rather ungainly workaround. Besides, it's not like anyone made an alternative suggestion at the time.



  •  I've never used COBOL, but I'm assuming it's at least as high a level as assembly.  And in assembly you can still make abstractions that would allow you to implement some form of variable length list of things.



  • @Jake Grey said:

    I'm not much of a coder and COBOL's a long way before my time, but sometimes I guess you just have to do the best job you can with the tools available, namely a programming language with a few limitations and a compiler with a feature/quirk/bug/glaring design error that obliged you to come up with a rather ungainly workaround. Besides, it's not like anyone made an alternative suggestion at the time.

     

    the problem with this logic is that it's based on the reasoning that abstractions cannot exist above the level of the programming language.



  • The obvious solution was to just use ColdFusion. 



  • COBOL was great for things like this.  I used to be the 'techie' (what back then was called a "System Programmer") in a site with large Honeywell mainframes.  I had a programmer come to me one day who'd recently joined from an ICL mainframe shop.

     

    He wanted to know why his program wouldn't run.  A little investigation revealed that it hadn't even compiled.  It was full of compilation errors.  "But on ICL kit you run COBOL programs even if they have compilation errors" he explained.



  • Couldn't you just used one array or something?



  • That solutions isn't just limited to COBOL.

    It's also how some (all?) languages implement the stack and heap - growing from opposite ends of available memory.



  • @snoofle said:

    That solutions isn't just limited to COBOL.

    It's also how some (all?) languages implement the stack and heap - growing from opposite ends of available memory.

    Yes, I'm not quite sure about the correct taxonomy but I'll risk calling it a Double Headed Array.

    Declaring a second array and using negative indices, as a shortcut, is a bit "smelly" though and imo it's the meat of the post.



  • @OldProgrammer said:

    I set up a negative index on array2 to use up the space above array2.




    Is your name Mel?



  • Massive maintenance guy breakdown (and posting this at all, probably) could be prevented by reading a few introductory chapters of Knuth's "Art of the Computer Programming". There's also analyses and optimal allocation strategies for many resizing arrays one above other.



  • Wow. I thought that array index abuse wasn't possible with COBOL. Then again, COBOL is older than C, so low-level management is pretty possible in that language generation. Especially when working under memory limitations.

    The second "array" is, in effect, growing the way stacks usually grow.



  • Kudos to the OP for an elegant solution.  I think I could have figurd it out if  I was the maintenance programmer, unless it used 1-letter variable names and other rot.  I've never worked on IBM machines, but I've pounded Cobol for most of my work life on (Open)VMS machines.  At least VMS gives you memory management and pukes the process when you run off the end of the array.  Oh, and I don't know if this is standard in newer versions of Cobol or if it's a VMS extension, but you can now declare variable-length arrays, although it's wordy to declare and to use.  (Go figure!)  It's actually so cumbersome that I rarely bother.

    I hate Cobol.  The only thing that's made it acceptable to me over the years is that some brilliant person way back in the annals of history of my company wrote a very elegant pre-compiler that fixes all of Cobol's limitations and idiocies.  Cobol won't let you declare variable-length strings, but our precompiler lets you do it at compile time, at least.  It also gives us macro capabilities.

     



  • @jetcitywoman said:

    ...but I've pounded Cobol for most of my work life...

    Holy crap that's hot. 



  • Thank you!  You're pretty sweet, so I'll give you a tip....  if you really want to attract the ladies, maybe you shouldn't include the word "wilt" in your online name. 

    HTH 



  • @jetcitywoman said:

    ...if you really want to attract the ladies, maybe you shouldn't include the word "wilt" in your online name. 

    Is there any forum software out there with a 'buy consolation beer' function? We sure as hell need one right now...



  • @Zecc said:

    @snoofle said:

    That solutions isn't just limited to COBOL.

    It's also how some (all?) languages implement the stack and heap - growing from opposite ends of available memory.

    Yes, I'm not quite sure about the correct taxonomy but I'll risk calling it a Double Headed Array.

    Declaring a second array and using negative indices, as a shortcut, is a bit "smelly" though and imo it's the meat of the post.

     

    Reminds me of a similar trick I used to use in m68k assembler a lot: because you could get short instructions using a register indirect mode with a signed 16 bit offset, a common trick was to declare all your data and globals in one big struct up to 64k in size, point an address register right at the middle of it, then happily index away using negative offsets to get at one bunch of data and positive to get at the rest.

     



  • @OldProgrammer said:

    Timeline: somewhere in the late '70s

    Environment: IBM mini with 32mb of memory;

    You mean KB, right?



  • I did this many times in the back in the 'good old days' when you could only declare a maximum data item of 32k or so in COBOL. String contiguous 32k areas in storage until I had what I really needed.

     What I really prefered to do was work in PL/1 -- there you could actually declare an array like

     

     DECLARE FOO (-37:100) CHAR(100); 

     

    to get an array of 10 character strings, where the index tould vary from -13 to 100.

     

    Ahhh -- life was good back then....

     

     

     

     



  • @jetcitywoman said:

    Thank you!  You're pretty sweet, so I'll give you a tip....  if you really want to attract the ladies, maybe you shouldn't include the word "wilt" in your online name. 

    HTH 

     

    Best post of the year! Nice job, jcw! 


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.