Thread: another pointer question....

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    29

    Question another pointer question....

    i've got a question... i want to make a REAL small scripting language by using a void * vector. i would like to know if this code is safe (causing memory leaks maybe?) and if there is any disadvantage for creating variables on the heap only.
    also, i would like to know what you think about it.
    here is my code:
    Code:
    vector<void *> pointers;
    cout<<"Enter a type and a name"<<endl;
    cout<<"Example: int a, string d"<<endl;
    cin.getline(var,100);
    vars=var;
    for(int l=0;l<vars.length();l++){
    if(vars.substr(l,1)==" "){
    pos=l;
    }
    }
    if(type=="int"){
    pointers.push_back(new int);
    cout<<"Value:";cout.flush();
    cin>>(*((int*)pointers[i]));
    cout<<*((int*)pointers[i])<<endl;
    i++;
    }
    
    anyways, thanks for any replay
    Last edited by gftmc; 08-14-2006 at 03:57 AM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Your code doesn't properly parse the input. I assume that this is just some pseudocode.
    Apart from that. If you insert just void pointers into your vector you will loose the type information. You would have to keep the typeinformation in another vector in parallel.
    Since this is supposed to be a scripting language I would suggest some kind of symbol table
    something like this

    Code:
    struct var {
        int type; // possibly an enum
        void * data;
    }
    map<string, var*> symtable;
    this way you could easily lookup the variables via their name.
    A much better solution would be to have a VariableBase class and derive the concrete variable classes from this baseclass and insert pointers to the VariableBase into your symboltable.
    Kurt
    Last edited by ZuK; 08-14-2006 at 04:33 AM.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    29
    this is just a part of my code. i wanted to post a small, important code, not all the I/O procedure. anyway, thanks for the reply ill try it.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Quote Originally Posted by ZuK
    Since this is supposed to be a scripting language I would suggest some kind of symbol table
    something like this

    Code:
    struct var {
        int type; // possibly an enum
        void * data;
    }
    map<string, var*> symtable;
    If the data types aren't too complex, a union of them would be simpler than a void pointer.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Scrap both void pointers and unions, use a Boost.Variant or Boost.Any.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    29
    of course this is possible, but then where is the chalenge? anyway, ill try a union, never realy worked with them before

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >of course this is possible, but then where is the chalenge?
    The challenge is gone, and good riddance. That's the point of abstraction. If you're writing this for yourself and never intend to release it then do what you want. Writing obtuse code for educational purposes is good. But if you're writing it for other people to use then you'd be wise to look for the best solutions, not the most challenging ones.
    My best code is written with the delete key.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    ... and using boost.any or boost.variant is winning the challenge of good taste
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM