Thread: How to store a huge list of switch-case like pairs in secondary memory ?

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    How to store a huge list of switch-case like pairs in secondary memory ?

    I'm trying to make a console shell for a simple virtual machine I'm making.

    When the shell receives a command, It should call the corresponding function with all the arguments provided with (-option_name argument) syntax.
    But I realized that the list of those functions will grow over time.
    Is there any way store the whole database, such that given the primary key (the command) it would know how to call the corresponding function..?

    I tried to use the STL types like map and set, but could only use them to store the nascent thing in ram...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A map like this perhaps?
    Code:
    #include<iostream>
    #include<iomanip>
    #include<map>
    using namespace std;
    
    void doThis ( void ) {
      cout << "this" << endl;
    }
    void doThat ( void ) {
      cout << "that" << endl;
    }
    typedef void (*fn)(void);
    
    int main(){
      map<string,fn> commands;
      commands["this"] = doThis;
      commands["that"] = doThat;
      commands["this"]();
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Salem View Post
    A map like this perhaps?
    Code:
    #include<iostream>
    #include<iomanip>
    #include<map>
    using namespace std;
    
    void doThis ( void ) {
      cout << "this" << endl;
    }
    void doThat ( void ) {
      cout << "that" << endl;
    }
    typedef void (*fn)(void);
    
    int main(){
      map<string,fn> commands;
      commands["this"] = doThis;
      commands["that"] = doThat;
      commands["this"]();
      return 0;
    }
    I was thinking of putting the whole thing in a disk, but realized that it'd be foolish to hard code function pointers..
    Anyway; putting a ..say.. 10 Mb map into ram would be faster that looking it up on the hard disk every time a command is encountered..

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Anyway; putting a ..say.. 10 Mb map into ram would be faster that looking it up on the hard disk every time a command is encountered.
    Wow - how many commands are you planning to write to get up to a 10Mb map!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    err...I am still in high school, and would implement everything I learn about computers, operating systems, etc.. on my virtual machine...for the next decade or so.! So, I think 10 Mb is a safe bet.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Do you know how much code that is?

    Assuming about 2 instructions average per line of C code, and an average instruction opcode size of 16-bit (I actually don't know about this one), that's 4 bytes per line of C code.

    10MB (assuming you actually meant bytes instead of bits) would be about 2.5 million lines of code.

    Assuming an average speed of 200 lines per day, that's about 12500 days, or about 35 years if you code every single day, give or take an order of magnitude.

    I really don't think this project will interest you for that long...

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Ok... 1 Mb then!...according to your (?)calculation I'd code every other day for 7 yrs ...

    EDIT: Did you consider comments , jokes etc .etc.. ?
    Last edited by manasij7479; 07-03-2011 at 01:31 PM.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Comments would add another few years depending on how verbose they get.

    By the way, function pointers should never be stored across program instances. You aren't guaranteed that your program will be loaded into the same place every time it's run. In fact, modern OSes will intentionally randomize the placement of your program on every run. It's a security feature (harder to write exploits when everything is at a random place every time).

    Even if your program is loaded at the same address every time, when you compile your code again, all the function addresses will likely be all over the place again.

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    By the way, function pointers should never be stored across program instances. You aren't guaranteed that your program will be loaded into the same place every time it's run. In fact, modern OSes will intentionally randomize the placement of your program on every run. It's a security feature (harder to write exploits when everything is at a random place every time).
    That's why I decided to dump my storage idea and started assigning them dynamically.
    Hard coding can never a good idea....It smells fishy..!

    or..do you mean, even the other approach shouldn't work ?

    ..say.. I have a void (*foo)(int);
    and void bar(int);

    What I'm doing now is plainly foo = &bar; using a similar approach as suggested by 'Salem' and it seems to be working.
    Are you categorizing it as undefined behaviour(i.e...setting them at compile time) ?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by manasij7479 View Post
    What I'm doing now is plainly foo = &bar; using a similar approach as suggested by 'Salem' and it seems to be working.
    Are you categorizing it as undefined behaviour(i.e...setting them at compile time) ?
    No, that's perfectly defined. What is undefined is saving that address to hard drive, then loading it back up when the program starts again and try to execute that function call.
    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.

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    saving that address to hard drive, then loading it back up when the program starts again and try to execute that function call
    I just love segfaults ... ..or executing a tail instead of a head.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. list of pairs on integers
    By jacek in forum C Programming
    Replies: 9
    Last Post: 12-15-2009, 08:06 AM
  2. Replies: 1
    Last Post: 11-05-2009, 12:30 AM
  3. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  4. store data from ifstream and store in link list
    By peter_hii in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2006, 08:50 AM
  5. Huge memory leak
    By durban in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2005, 12:10 PM