Thread: Can it be detected if stack space is about to expire?

  1. #16
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    I already have a structure (A std::vector of std::map `s encapsulated into a 'scope' class ....(..than may translate into a dynamic array of binary trees..AFAIK)) in which all variables are stored.
    A new map is pushed back each time a new scope appears...and is popped when the scope exits.
    The relevant portion of the code is...
    Code:
        std::string function::operator()(list& l)
        {
            std::string result;
            if(l.size()!=arg_list.size())throw(exception("Wrong no. of Args."));
    
            std::map<std::string,std::string> scope_map;
            auto x=arg_list.begin(); auto y = l.begin();
            for(;x!=arg_list.end();x++,y++)  //<--This loop makes the map relating the values of the variable to their name
            {
                std::string foo(*x),bar(*y);
                scope_map[foo]=bar;
            }
            var_scope.new_local(scope_map);  //var_scope is declared in "env.h"
            
            if(compiled)
            {
                result = (*body_c)(arg_list);
            }
            else
            {
                result = body_l.eval();
            }
                    
            var_scope.exit_scope(); //Pops the variable map of the current scope.
            return result;
        }
    The scope class can be found here (..but it is fairly obvious what it does.) : Confusing Template error

    This is my usage of the 'virtual' stack represented with the scope class.
    I'd try both approaches(linking this somehow to the machine stack... and detecting if the function will be 'bad' before executing it).
    For the later, I have a good idea how to implement it so that catches most problems.
    But I've no idea how to use this data structure to establish a relation to the machine stack.
    Last edited by manasij7479; 12-02-2011 at 05:03 PM.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    mana! Don't use manual popping! This isn't exception safe!
    I suggest you model your program by using a "scope" class for each scope. That way you will make your code exception safe.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    I suggest you model your program by using a "scope" class for each scope. That way you will make your code exception safe.
    Err.. what do you mean by that ? I can't understand the difference between the two... in logic.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Currently, you have some parent object, so you do

    parent.enter_scope(...);
    parent.leave_scope(...);

    Instead of that, a better way to do it is

    Scope new_scope;
    // Nothing. Object will go out of scope when we leave scope.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    Currently, you have some parent object, so you do

    parent.enter_scope(...);
    parent.leave_scope(...);

    Instead of that, a better way to do it is

    Scope new_scope;
    // Nothing. Object will go out of scope when we leave scope.
    But that doesn't give me a centralized way of finding values.
    (Not applicable for functions) .. but some MACROs in lisp allow setting a local variable which can be accessed from all 'child' scopes.
    Handling that would become problematic with your way... while in what I have, I can just call the find() method of the global scope object .

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    Quote Originally Posted by Elysia View Post
    mana! Don't use manual popping! This isn't exception safe!
    How do you know?

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by manasij7479 View Post
    But that doesn't give me a centralized way of finding values.
    (Not applicable for functions) .. but some MACROs in lisp allow setting a local variable which can be accessed from all 'child' scopes.
    Handling that would become problematic with your way... while in what I have, I can just call the find() method of the global scope object .
    You just need to make sure that whenever you would do a manual pop, you would force some object to go our of scope.
    I urge you to think about that design a little. Nothing is more painful than code that fails when a certain path in your code is taken.

    Quote Originally Posted by whiteflags View Post
    How do you know?
    By definition.
    Whether it works or not is another matter.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    You just need to make sure that whenever you would do a manual pop, you would force some object to go our of scope.
    Well.. as the manual popping takes place almost at the end of the scope here, and the map I'm pushing into it is itself local and not dynamically allocated, I think it is guaranteed.
    Did you mean something else?

  9. #24
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    Quote Originally Posted by Elysia View Post
    By definition.
    Whether it works or not is another matter.
    Well that was unhelpful of you. I assume you mean it isn't exception safe because of how new_local is written.

    Couldn't you just catch the exception, release anything that needs to be destroyed and then rethrow?

  10. #25
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Make sure you use a deque for the stack instead of a vector, to allow a larger size.

    You should make it so that an interpreted function call does not add to the program stack at all. So an interpreted recursive loop should result in running out of memory, never stack space.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #26
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I think stack or vector or dequeue are all inferior to a linked list of scopes.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #27
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Elysia View Post
    mana! Don't use manual popping! This isn't exception safe!
    I suggest you model your program by using a "scope" class for each scope. That way you will make your code exception safe.
    You don't necessarily want to pop something off on an exception. An exception would mean something is wrong with the interpreter. The state of the interpreted program stack should probably be left alone, to allow backtracing and better logging.

    An exception in user code is a different story, but that shouldn't trigger an actual exception.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  13. #28
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by brewbuck View Post
    I think stack or vector or dequeue are all inferior to a linked list of scopes.
    Why? You're only ever popping or pushing, so you don't need insertion in the middle. Using a linked list would just add memory overhead. Deque (not vector) will instead allocate large chunks of memory that only contain stored data, without any additional usage per stored object (only per chunk).

    Deque is perfect for stacks and queues.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #29
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by King Mir View Post
    An exception in user code is a different story, but that shouldn't trigger an actual exception.
    Why ? IMHO, that is the point of using exceptions here...and the best way to refuse acknowledging syntactically wrong input...and showing the corresponding error message within the exception object.

    (Well, I'm going to just replace the internal storage of the scope class to deque and list successively just to see what happens ..This is why I like C++! )
    Last edited by manasij7479; 12-02-2011 at 05:50 PM.

  15. #30
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    The way I've seen it done, specifically in Bison, which is a parser generator, is to have a state for each position in the grammar, and a switch on which token could come next. An improper token would generate an error, but no stack unwinding is needed to handle it. It then jumps to a state in which the error can be handled, first eating additional faulty input by a specified rule (for instance until newline).

    EDIT:But that's not actually what I meant my a user exception.
    I mean that if there is an exception in the code (if your language allows for it), then you want to manually pop elements off the stack until you get to the error handler.
    Last edited by King Mir; 12-02-2011 at 06:28 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack Smashing Detected
    By halexh in forum C Programming
    Replies: 13
    Last Post: 05-19-2010, 02:10 AM
  2. *** stack smashing detected ***
    By chakra in forum C Programming
    Replies: 2
    Last Post: 06-09-2009, 09:12 PM
  3. *** stack smashing detected ***
    By Martin_HS in forum C Programming
    Replies: 9
    Last Post: 05-29-2009, 04:01 AM
  4. help on *** stack smashing detected ***
    By jodelson in forum C Programming
    Replies: 8
    Last Post: 08-16-2007, 06:25 PM
  5. Managing Stack space?
    By Aidman in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2003, 02:29 PM