CompuServe Thread

Forum unknown · Programming

#More Modula-2 Questions

19 messages in this thread
#36864From: Lon LeaderOct 21, 1986 12:19 AM
Richie, I've got a couple of M2 questions for you, if you have the time to answer. (Any other M2 gurus… please jump in also!) 1. How do you pass NULL as a parameter to a procedure…. eg. in C… RefreshGadgets (&firstgadget,wp,NULL) M2… RefreshGadgets (firstgadget,wp, ? ) or when setting up several similar gadgets using one common procedure, for the last one you want to pass NULL for the NextGadget parameter. 2. I am attempting to convert a C program to M2 (BIG mistake, 'cause the reason I am learning M2 is because I can't understand C) and I am setting all the window stuff up first… have 23 BOOLGADGETs, 3 STRGADGETs, and 1 PROPGADGET… but no code to use them yet. It compiles and runs ok with all variables declared in the main module, but when I move the variables into their respective procedures it compiles ok but locks up the system when I run it. Any ideas? Thanks, Lon
#36908From: Steve FaiwiszewskiOct 21, 1986 9:04 PM
1. Simple: Just IMPORT NULL from SYSTEM, and then simply pass it in the procedure call. 2. Hmmm… It's hard to tell without looking at the code, but maybe because the variables are local to a procedure, they get allocated on the stack, and when the procedure returns, the stack space gets reclaimed and those variables get trashed, except that Intuition is still using them. When you declare them globally, the variables "live" throughout the execution time of the program. How's that for an explanation? What do you think Richie? – Steve –
#36954From: Lon LeaderOct 22, 1986 5:36 PM
Gee Steve, I wish it was that simple to pass the NULL. That was the first thing I tried. I believe the error that produced was 'type incompatible'. That particular procedure I used for an example expects the third parameter to be a Requester, so I tried RefreshGadgets (firstgadget,wp,(Requester(NULL)); but that didn't work either. Maybe on the second problem the stack figures in somehow… I'd be more inclined to think it was maybe not big enough if that was the problem, but I have it at 30K to avoid compile problems. Perhaps I'll move the variables back into the procedures again and try a huge stack. Thanks, Lon
#36976From: Steve FaiwiszewskiOct 22, 1986 9:30 PM
Re: first problem. Hmmm.. I see your point. It seems to me that the RefreshGadgets procedure is declared incorrectly in the Gadgets module. The third parameter should have been an address, and not a Requester. This is not the first time I've seen this problem of wrong declaration. (Richie, do you see a way around this?) Re: second problem. You don't seem to understand. I'm not talking about the system's stack (the one you set with the CLI STACK command). What I meant was, the way Modula (and C) works is that when you enter a procedure storage for all the local variables gets allocated, and when you exit the procedure that storage is reclaimed and those variables are destroyed, kaput, gone! So if Intuition still tries to refer to them, thinking they're still there, well you've seen what happens. The solution is that you gotta keep those variables used by Intuition global. If you don't want them to be visible to other procedures, put them in a separate module – that's why Modula is so great! – Steve –
#36993From: Lon LeaderOct 22, 1986 11:09 PM
Steve, thanks for your help. I see what you meant now about those variables going away (and I know first hand what happens when they do!). I didn't stop to think about M2 having it's own stack. Richie said that you had figured it right on. He also had a sugested 'work around' for the NULL problem by declaring a dummy requester and just passing it. I had actually already tried this and it works, but it somehow didn't seem like the 'right' thing to do… however in this case I guess it doesn't matter how you play the game, as long as you win! Thanks, Lon
#36993From: Lon LeaderOct 22, 1986 11:09 PM
Steve, thanks for your help. I see what you meant now about those variables going away (and I know first hand what happens when they do!). I didn't stop to think about M2 having it's own stack. Richie said that you had figured it right on. He also had a sugested 'work around' for the NULL problem by declaring a dummy requester and just passing it. I had actually already tried this and it works, but it somehow didn't seem like the 'right' thing to do… however in this case I guess it doesn't matter how you play the game, as long as you win! Thanks, Lon
#36976From: Steve FaiwiszewskiOct 22, 1986 9:30 PM
Re: first problem. Hmmm.. I see your point. It seems to me that the RefreshGadgets procedure is declared incorrectly in the Gadgets module. The third parameter should have been an address, and not a Requester. This is not the first time I've seen this problem of wrong declaration. (Richie, do you see a way around this?) Re: second problem. You don't seem to understand. I'm not talking about the system's stack (the one you set with the CLI STACK command). What I meant was, the way Modula (and C) works is that when you enter a procedure storage for all the local variables gets allocated, and when you exit the procedure that storage is reclaimed and those variables are destroyed, kaput, gone! So if Intuition still tries to refer to them, thinking they're still there, well you've seen what happens. The solution is that you gotta keep those variables used by Intuition global. If you don't want them to be visible to other procedures, put them in a separate module – that's why Modula is so great! – Steve –
#36954From: Lon LeaderOct 22, 1986 5:36 PM
Gee Steve, I wish it was that simple to pass the NULL. That was the first thing I tried. I believe the error that produced was 'type incompatible'. That particular procedure I used for an example expects the third parameter to be a Requester, so I tried RefreshGadgets (firstgadget,wp,(Requester(NULL)); but that didn't work either. Maybe on the second problem the stack figures in somehow… I'd be more inclined to think it was maybe not big enough if that was the problem, but I have it at 30K to avoid compile problems. Perhaps I'll move the variables back into the procedures again and try a huge stack. Thanks, Lon
#36978From: Richard BielakOct 22, 1986 9:51 PM
Well Steve, one out of two ain't bad! Look at the "RefreshGadgets" declaration in "gadgets" module…Richie
#36978From: Richard BielakOct 22, 1986 9:51 PM
Well Steve, one out of two ain't bad! Look at the "RefreshGadgets" declaration in "gadgets" module…Richie
#36908From: Steve FaiwiszewskiOct 21, 1986 9:04 PM
1. Simple: Just IMPORT NULL from SYSTEM, and then simply pass it in the procedure call. 2. Hmmm… It's hard to tell without looking at the code, but maybe because the variables are local to a procedure, they get allocated on the stack, and when the procedure returns, the stack space gets reclaimed and those variables get trashed, except that Intuition is still using them. When you declare them globally, the variables "live" throughout the execution time of the program. How's that for an explanation? What do you think Richie? – Steve –
#36977From: Richard BielakOct 22, 1986 9:49 PM
Hi! The problem with "RefreshGadgets" is the way TDI declared it in the "Gadgets" module. The third parameter is a variable parameter, so you cannot pass a constant. The third parameter should have been delared as a pointer so that you could pass NULL (imported from SYSTEM). In this case try to declare a dummy Requester variable and pass it in. If you're lucky, Intuition will just ignore it……The "right" solution is to re-write the part of the "Gadgets" module and declare "RefreshGadgets" properly. If you really have to do that, send me E-mail….I forgot your second question, so I'll finish this message and enter another….Richie
#36994From: Lon LeaderOct 22, 1986 11:10 PM
Ritchie, thanks again for your help. I had already done as you suggested and declared a dummy Requester variable and passed it. It does seem to work OK, as it is a string gadget that I wanted refreshed to show a new message, and it does. Seems like 'cheating' to me, but what the heck! Boy, I wish someone would come out with a book on programming the ==>AMIGA<== in Modula-2. When you don't know 'C' you spend a LOT of time trying to translate the examples in the Intuition manual into Modula-2. Also, I am using a book called 'Modula-2, a Seafarer's Guide and Shipyard Manual' as a reference, and find it very readable, but some things it says don't work with TDI. For example, it says if you have two arrays that have the same sizes, types, and index ranges, you can use the assignment operator to copy one to the other… not so with TDI! (Sure would make moving strings around a lot easier… you listening Les Caudle?… is this a feature left out, or a deviation frowned upon?) Thanks, Lon
#37181From: Richard BielakOct 24, 1986 8:21 PM
Hi! I'm familiar with the book "Seafearers guide to M2". There are few mistakes in it. As there is no formal standard for M2 usually Wirth's book "Programming in M2" is used as the final word. In Modula-2 arrays have to be declared of exactly the same type to be assigment compatible. So for example this code is incorrect: VAR a : ARRAY [1..10] OF CHAR; b : ARRAY [1..10] OF CHAR; … a := b; (* This will not compile *) …. What you should do is make up another type. So you can do this: TYPE string = ARRAY [1..10] OF CHAR; VAR a,b : string; …. a := b; (* This is OK *) …. Also, string literals are considered of type "ARRAY [0..size] OF CHAR", so if you want to say "a := 'abcdef';', declare "a" as an array of characters whose range starts at "0". You should be able to perform the assigment, even if the length of "a" is larger than the leangth of the literal. BTW, in "Ami Project" I have an AMiga/M2 column. You may find it helpful (just a little plug)…..take care….Richie
#37181From: Richard BielakOct 24, 1986 8:21 PM
Hi! I'm familiar with the book "Seafearers guide to M2". There are few mistakes in it. As there is no formal standard for M2 usually Wirth's book "Programming in M2" is used as the final word. In Modula-2 arrays have to be declared of exactly the same type to be assigment compatible. So for example this code is incorrect: VAR a : ARRAY [1..10] OF CHAR; b : ARRAY [1..10] OF CHAR; … a := b; (* This will not compile *) …. What you should do is make up another type. So you can do this: TYPE string = ARRAY [1..10] OF CHAR; VAR a,b : string; …. a := b; (* This is OK *) …. Also, string literals are considered of type "ARRAY [0..size] OF CHAR", so if you want to say "a := 'abcdef';', declare "a" as an array of characters whose range starts at "0". You should be able to perform the assigment, even if the length of "a" is larger than the leangth of the literal. BTW, in "Ami Project" I have an AMiga/M2 column. You may find it helpful (just a little plug)…..take care….Richie
#36994From: Lon LeaderOct 22, 1986 11:10 PM
Ritchie, thanks again for your help. I had already done as you suggested and declared a dummy Requester variable and passed it. It does seem to work OK, as it is a string gadget that I wanted refreshed to show a new message, and it does. Seems like 'cheating' to me, but what the heck! Boy, I wish someone would come out with a book on programming the ==>AMIGA<== in Modula-2. When you don't know 'C' you spend a LOT of time trying to translate the examples in the Intuition manual into Modula-2. Also, I am using a book called 'Modula-2, a Seafarer's Guide and Shipyard Manual' as a reference, and find it very readable, but some things it says don't work with TDI. For example, it says if you have two arrays that have the same sizes, types, and index ranges, you can use the assignment operator to copy one to the other… not so with TDI! (Sure would make moving strings around a lot easier… you listening Les Caudle?… is this a feature left out, or a deviation frowned upon?) Thanks, Lon
#36977From: Richard BielakOct 22, 1986 9:49 PM
Hi! The problem with "RefreshGadgets" is the way TDI declared it in the "Gadgets" module. The third parameter is a variable parameter, so you cannot pass a constant. The third parameter should have been delared as a pointer so that you could pass NULL (imported from SYSTEM). In this case try to declare a dummy Requester variable and pass it in. If you're lucky, Intuition will just ignore it……The "right" solution is to re-write the part of the "Gadgets" module and declare "RefreshGadgets" properly. If you really have to do that, send me E-mail….I forgot your second question, so I'll finish this message and enter another….Richie
#36980From: Richard BielakOct 22, 1986 9:55 PM
Hi Lon, I see that you second question was answered by Steve. Variable declared in a procedure are CREATED when procedure is entered, and DESTROYED (!!!) when the procedure exists. All Intuition structures have to exist at all time (while your program is running), otherwise strange things will happen. …hope all this helps……Richie
#36980From: Richard BielakOct 22, 1986 9:55 PM
Hi Lon, I see that you second question was answered by Steve. Variable declared in a procedure are CREATED when procedure is entered, and DESTROYED (!!!) when the procedure exists. All Intuition structures have to exist at all time (while your program is running), otherwise strange things will happen. …hope all this helps……Richie