Jump to content

localizing variables?


cadman6735

Recommended Posts

And of course as usual :roll:, most of this can be found on WikiPedia already.

 

  1. http://en.wikipedia.org/wiki/Stack-based_memory_allocation
  2. http://en.wikipedia.org/wiki/Stack_%28data_structure%29
  3. http://en.wikipedia.org/wiki/Call_stack

Note in 2: how much more difficult is it to create this basic data structure in C++. Especially if you use the Linked List method, notice (in the push function) it needs to allocate memory (as large as the custom stack item record):

STACK *node = malloc(sizeof(STACK));  /* create a new node */

And then it needs to release that memory when it's not needed anymore in the pop function:

STACK *top = *head;
// ....
free(top);

In lisp that entire implementation (29 lines in C++) would be:

(setq *stack* nil)
(defun push (val / )
 (setq *stack* (cons val *stack*))
)
(defun pop ( / val )
 (setq val (car *stack*)
       *stack* (cdr *stack*)
 )
 val
)

10 lines ... Glad you're using lisp now aren't you? 8)

Link to comment
Share on other sites

  • Replies 52
  • Created
  • Last Reply

Top Posters In This Topic

  • irneb

    10

  • Se7en

    8

  • Lee Mac

    8

  • cadman6735

    7

*cough* C++ uses NEW and DELETE not MALLOC and FREE (Sorry, i knew several CS' and one CS professor who would berate me every time i would mix the two..."Get that C crap out of the C++ code"); Your example would be using C code (Ive worked on both a queue and a stack in C++).

 

IMHO I would imagine that a better example of POP and PUSH for AutoLISP would be something along the lines of:

(defun list-push (sym lst) 
 ;; push an item first into existing list.
 ;; 
 ;; Example: 
 ;;    (list-push 'one 'masterlist)
 ;;    > (ONE)
 ;;    > !masterlist
 ;;    > (ONE)
 ;; OR
 ;;    (setq mylist '(2 3 4))
 ;;    (list-push 1 'mylist)
 ;;    > (1 2 3 4)
 ;;    > !mylist
 ;;    > (1 2 3 4)
 ;; 
 ;; By: Se7en
 ;;    (inspired by Vladimir Nesterovsky)
 ;; 10.24.05
 (set lst (cons sym (eval lst))) )

(defun list-pop (lst / ~tval )
 ;; pop (remove) the first item from a list and redefne 
 ;; the var containing that list. 
 ;; 
 ;; Example:
 ;; Given a list called "masterlist" with the 
 ;; value of: (TWO ONE)
 ;;
 ;; !masterlist
 ;; (TWO ONE)
 ;;
 ;; (list-pop 'masterlist)
 ;; TWO
 ;;
 ;; (list-pop 'masterlist)
 ;; ONE
 ;;
 ;; !masterlist
 ;; nil
 ;;
 ;; By: Se7en
 ;;    (inspired by Vladimir Nesterovsky)
 ;; 10.24.05
 (setq ~tval (eval lst))
 (set lst (cdr ~tval))
 (car ~tval) )

Link to comment
Share on other sites

You're correct, I should've noted that the code on WikiPedia is actually using plain old C. Although those functions still perform the same things in C++. For me the new and delete methods have more relevance when you start into Object Orientation (but that's just semantics on my part, and way beyond anything discussed here). So I'd say to "your" CS' why use C++ only methods when you're writing a normal C function inside C++?

 

Your functions are a lot closer to exactly what's shown in the C samples. :thumbsup:

 

Not to mention you've also indicated how to simulate output arguments in lisp. Usually with other languages you'd declare an argument as either in, or out, or both (in the header of the function). Lisp doesn't have such idea, but you can quote the variable and then use the set (instead of setq) to modify the value contained therein. In this way you can have your function can modify multiple variables without needing to know their names when writing your function. Usually you'd only calculate a new value for one variable so when your function returns it gets setq'd to the original variable. But say you had 2 (or more) which needs modification in the same go? You'd either modify them as if global (but then you have to have their names hardcoded into your function), or as Se7en's function does - eval to get the value and set to change it, with a quoted argument during the call.

Link to comment
Share on other sites

> why use C++ only methods when you're writing a normal C function inside C++?

 

Ah, but there you go creating those pesky memory leaks we discussed. Basically, If you dont use NEW then the constructor and destructor wont be called "automatically" (so to speak) and either the client or the designer needs to handle the...Its a lot more complicated in C then in C++. Go one or the other. True most C++ compilers support C functions but some are either overloaded, added too, improved, etc. and are only there for legacy reasons not for mixing the two.

 

yes, NEW and DELETE are more for dynamic memory management within classes but...

Edited by Se7en
sp3lling
Link to comment
Share on other sites

  • 3 months later...

I have a question regarding localized variables so I am replying to this old pots.

 

Is there a way to check what variables are active in acad (non localized). I have a bunch of lisp routines that I was not very diligent on localizing to variables on, I need to go back and do that now, and it would be nice if I could some function to list all the variables that are not localized once the routine's have been run.

 

Thanks,

Bruce

Link to comment
Share on other sites

if you make some changes to your vlide, you can find out what variables are global by using ctrl+shift+c

Tools - Environment Options - General Options - Diagnostics tab, first box

Link to comment
Share on other sites

I highly recommend going through all your programs and making sure everything is local, I just had a program that had one variable that wasnt localized and it caused the entire routine to fail, took me 2 1/2 hours to figure out that is what it was.

Link to comment
Share on other sites

A word to the wise, If you cut and paste the variables this way and place them in the defun brackets, do not let it place PAUSE in there, especially if you use it un-quoted in an insert command within the lisp....

Link to comment
Share on other sites

yes, sometimes it thinks things are global variables that are Autocad defined, not user defined, you should check to make sure they are variables you actually specified to be created. I think a few vlax functions will show up that way as well, and possibly even "t"

Link to comment
Share on other sites

A word to the wise, If you cut and paste the variables this way and place them in the defun brackets, do not let it place PAUSE in there, especially if you use it un-quoted in an insert command within the lisp....

 

I've too been inadvertently caught by that one...

 

Same applies to $value, $reason, $key, $data and anything showing up blue...

Link to comment
Share on other sites

If you're prepared to spend some time, you can get hold of all the global stuff (Lisp-wise) through the atoms-family function. Unfortunately this lists absolutely everything, including all defuns (also the built-in stuff). But I think ccowgill's method's the easiest - just (as mentioned) don't trust it in every instance.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...