GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: Add Line Numbers


headaches by rcl
From the header --

#ifndef _MOUNTAIN_H
#define _MOUNTAIN_H

#include <iostream>

using namespace std;
        
class Mountain
{
public:
        Mountain();
        Mountain(const Mountain&);
        ~Mountain();
        
        void set(const char *);
        
        char *country;
        int height;
};              
                
#endif

From the implementation --


// copy constructor
Mountain::Mountain(const Mountain &m)
{       
        this->country = NULL;
        height = -1;
        
        if(m.country)
        {
                cout << "m.country is " << m.country << " strlen of m.country is " << strlen(m.country) << endl;
                cout.flush();
                this->country = new char [14]; // strlen(m.country) + 1];
//              strcpy(country, m.country);
        }

        height = m.height;
}

Another function from the implementation --

void Mountain::set(const char *s)
{
        char *token2;
        int token2len;
        
        char *p; // temp pointer to help us traverse our string
        
        // this is meant to accept a null terminated string with a mountain in the format --
        // Finger Rock#United States#6475
        // We need to parse this and ignore the first field, and make sure we get the 2nd and 3rd into the
        // appropriate places
        
        if(country)
        {
                // clear out memory if we have already allocated some
                delete [] country;
                country = NULL;
        }

        if(s)
        {
                if((p = strstr(s, "#")) != NULL) // if NULL then malformed, no first field
                {
                        // otherwise we are pointing to the first #...
                        p++; // advance to the first character of the 2nd field

                        if((token2 = strstr(p, "#")) != NULL)
                        {
                                token2++; // advance to the actual field

                                if(token2)
                                {
                                        // if not null then we have a second field so lets get it
                                        // token1len is now the address of the #, doing subtraction we can figure out the length
                                        // of the actual string
                                        token2len = int(token2) - int(p);

// !!!!!!! notice here that I am doing the same type of thing !!!!!
                                        country = new char [token2len];
// !!!!!!! notice here that I am doing the same type of thing !!!!!

                                        snprintf(country, token2len, "%s", p);//token2); // copy only as much of the string as we want from offset p into the country string
                                        cout << "country = " << country << " ";
                                }
                        }

                        p = token2; // p now points to the first character of token3

                        if(p) // if p is not null then we have a string here, lets turn it into the height
                        {
                                height = atoi(p);
                                cout << "height (int) is - " << height << " ";
                        }
                }
        }
}


From the debugger --


% gdb ./main
GNU gdb 6.0
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i586-pc-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".

(gdb) run
Starting program: /home/rcl/cs310/proj2/main 
warning: Unable to find dynamic linker breakpoint function.
GDB will be unable to debug shared library initializers
and track explicitly loaded dynamic code.
In loop, parsing Granite Peak#United States#12799 -- key = Granite Peak country = United States height (int) is - 12799 
m.country is United States strlen of m.country is 13
Granite Peak