irneb Posted September 16, 2010 Share Posted September 16, 2010 And of course as usual , most of this can be found on WikiPedia already. http://en.wikipedia.org/wiki/Stack-based_memory_allocation http://en.wikipedia.org/wiki/Stack_%28data_structure%29 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? Quote Link to comment Share on other sites More sharing options...
Se7en Posted September 16, 2010 Share Posted September 16, 2010 *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) ) Quote Link to comment Share on other sites More sharing options...
irneb Posted September 16, 2010 Share Posted September 16, 2010 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. 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. Quote Link to comment Share on other sites More sharing options...
Se7en Posted September 16, 2010 Share Posted September 16, 2010 (edited) > 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 September 16, 2010 by Se7en sp3lling Quote Link to comment Share on other sites More sharing options...
Snownut Posted January 11, 2011 Share Posted January 11, 2011 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 Quote Link to comment Share on other sites More sharing options...
ccowgill Posted January 11, 2011 Share Posted January 11, 2011 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 Quote Link to comment Share on other sites More sharing options...
Snownut Posted January 11, 2011 Share Posted January 11, 2011 Thanks for the quick response, that saves me a lot!!!!!!!!!!! of time. Bruce Quote Link to comment Share on other sites More sharing options...
ccowgill Posted January 11, 2011 Share Posted January 11, 2011 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. Quote Link to comment Share on other sites More sharing options...
Snownut Posted January 11, 2011 Share Posted January 11, 2011 That is exactly what I am doing right now. Very good advice though. Thanks, Bruce Quote Link to comment Share on other sites More sharing options...
Snownut Posted January 11, 2011 Share Posted January 11, 2011 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.... Quote Link to comment Share on other sites More sharing options...
ccowgill Posted January 11, 2011 Share Posted January 11, 2011 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" Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted January 11, 2011 Share Posted January 11, 2011 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... Quote Link to comment Share on other sites More sharing options...
irneb Posted January 12, 2011 Share Posted January 12, 2011 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.