#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    31
    Rep Power
    16

    Keeping track of a list of objects


    Hi, and thanks in advance for any help!

    I am trying to create an array of pointers which will keep track of a series of objects created. I'm getting an exception error in the try block.. mostly because I don't know what i'm doing. (hense my plea for help). What I want is an array to keep track of created objects for later manipulation. Again, TIA.

    the class
    PHP Code:
    class CCatList  
    {
    public:
        
    CCatList();
        
    virtual ~CCatList();
        
    void setCatName(char *);
    private:
        
    char *storedcategoryname;

    }; 
    ...
    PHP Code:
    void CCatList::setCatName(char *name)
    {
        
    storedcategoryname name;

    the function
    PHP Code:
    void CNLDocsDlg::OnPopulateCatList()
    {
        
    m_lbCatList.ResetContent();

        
    FILE *stream;
        
    char buffer[200];
        
    int  ich;
        
    bool iscat FALSE;
        
    CCatList *catListMgr[5];                        // array of pointers to objects
        
    int idx=0;

        
    /* Open file to read line from: */
        
    if( (stream fopen"DOCIX_PE.DAT""r" )) == NULL )
            exit( 
    );

        
    i=0;
        while(
    ch != EOF){
            
            if((
    ch getc(stream)) != '\n'){
                if(
    ch != '>'){   
                    
    buffer[i++] = (char)ch;
                } else {
                    
    iscat TRUE;
                }
            } else {                                    
    // at end of line
                
    buffer[i++] = '\0';                        // close up this buffer and print
                
    if(iscat == TRUE){
                    
    m_lbCatList.AddString(buffer);
                    
    iscat FALSE;
                    
    CCatList *catlist = new CCatList(); // create a new pointer to object
                    
    catlist->setCatName(buffer);        // give our new object a category name
                    
    try {
                        *
    catListMgr[idx++] = *catlist;    // keep a list of category objects
                    
    }
                    catch(...){
                        
    AfxMessageBox("Exception Error");
                    }
                }
                
    i=0;
            }
        }
        
    fclosestream );
        
    OnPopulateDocList();


  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    31
    Rep Power
    16
    the
    color="#006600">
    was put in by the [php] tag... it doesn't exist in my code.
  4. #3
  5. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    248
    I don't see where you are ever allocating memory for the storedcategoryname variable in your class. What you have now will copy the pointer to 'buffer' which, of course, will change the next time you use it. You should either allocate a 200 byte char array for storedcategoryname in your class or malloc (or new) one based on the length of the buffer (be sure to free (or delete) it in the destructor). You would then do a strcpy in setCatName(). If you choose to use strings, you can then do what you are trying to do.

    You are also wide open to a buffer overflow because you never check to see if there are too many input characters from your input file.

    You also exit with a value of zero if your file does not open properly. Zero is considered 'everything is OK' exit code, and I expect that is not what you want. You should print out an error message and exit with a non-zero code.

    You also appear to be terminating your buffer incorrectly (though that is up to you). You do this: "buffer[i++] = '';" when I think you should be doing this: "buffer[i++] = '\0';" (NULL terminating your array).

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    31
    Rep Power
    16
    Thank you for your quick response.

    I actually do terminate buffer correctly with '\0'; but for some reason pasting the code between the \[php\] tags removed the buffer terminator.

    I have changed the setCatName() to use strcpy() as well, I have an cerr << now when it fails to open file. (still have not checked for buffer overruns yet). As well, I am now using a char array for storedcategoryname, rather than simply a char pointer.

    The main problem still persists though, in that I get an exception error in the try {} block. I'm really really weak in my pointer accumen... mostly throwing pointers willy nilly in order to compile and via trial and error. Do you, or anyone else, see a glaring ommission with my use of the line:
    [php] *catListMgr[idx++] = *catlist;

    [php]

    Thank you!
  8. #5
  9. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    248
    Your allocation (I didn't pay attention to it before, got caught up in the other errors) is for a set of 5 pointers to CCatList, not actually 5 of the objects. I suspect if you just drop the asterix you will be fine. You could, however, make a loop and do a new for each element in the array, but I think that is gross overkill (and you would have to do matching deletes).

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo