bug-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

segmentation fault with g++


From: Admir Efendic
Subject: segmentation fault with g++
Date: Wed, 21 Feb 2001 15:16:26 +0100

Hi!

Following code works fine on Win2000 compiled with Borland C++ Builder 4.0
Pro.
On my linux machine it causes segmentation fault.

Environment:
RedHat 7.0
gcc 2.96
glibc 2.2.9

Compiled with: g++ -ansi -Wall -o LGURLParser LGURLParser.cpp

listing LGURLParser.h:
--------------------------------------------------------

#ifndef __LG_URL_PARSER
#define __LG_URL_PARSER

class LG_URLParser
{
private:
        char*                   m_url;
        char*                   m_host;
        unsigned short  m_port;
        char*                   m_protocol;
        char*                   m_document;

public:
        LG_URLParser(const char* url);
        ~LG_URLParser();

        bool                            Parse();

        void                            SetURL(const char* url);

        const char*                     GetHost();
        unsigned short          GetPort();
        const char*                     GetProtocol();
        const char*                     GetDocument();

};

#endif


listing LGURLParser.cpp
----------------------------------------------------------

#include <string.h>
#include <stdlib.h>

#include <iostream.h>

#include "LGURLParser.h"


LG_URLParser::LG_URLParser(const char* url)
{
        m_url           =   new char(256);
        m_host          =   new char(256);
        m_port          =   80;
        m_protocol      =   new char(256);
        m_document      =   new char(256);

        strcpy(m_url,url);
}

LG_URLParser::~LG_URLParser()
{
        delete m_url;
        delete m_host;
        delete m_protocol;
        delete m_document;
}

void LG_URLParser::SetURL(const char* url)
{
        strcpy(m_url,url);
}

const char* LG_URLParser::GetHost()
{
        return (const char*)m_host;
}

unsigned short LG_URLParser::GetPort()
{
        return m_port;
}

const char* LG_URLParser::GetProtocol()
{
        return (const char*)m_protocol;
}

const char* LG_URLParser::GetDocument()
{
        return (const char*)m_document;
}


bool LG_URLParser::Parse()
{
        bool ret        = true;

        char* tok;
        char* host_temp;
        char* host_tok;


        try
        {
                tok = strtok(m_url,"://");

                if(tok)
                {
                        m_protocol = tok;
                }

                tok = strtok(NULL,"/");

                if(tok)
              {
                        strcpy(host_temp,(const char*)tok);
        }

                tok = strtok(NULL,"\0");

                if(tok)
                {
                        strcpy(m_document,(const char*)tok);
                }

                if(strlen(host_temp))
                {
                host_tok = strtok(host_temp,":");

                        if(host_tok)
                        {
                                strcpy(m_host,(const char*)host_tok);
                        }
                        host_tok = strtok(NULL,"\0");

                        if(host_tok)
                        {
                                m_port = atoi(host_tok);
                        }
                }
        }
        catch(...)
        {
                ret = false;
        }

        return ret;
}



int main(int argc, char* argv[])
{
    cout << "constructing..." << endl;

        LG_URLParser* parser = new
LG_URLParser("http://vai.home.se:12000/index.html";);

        cout << "parsing..." << endl;

        if(     parser->Parse())
        {
                cout << "protocol: " << parser->GetProtocol() << endl;
                cout << "host: " << parser->GetHost() << endl;
                cout << "port: " << parser->GetPort() << endl;
                cout << "document: " << parser->GetDocument() << endl;
        }

        delete parser;
        return 0;
}


-------------------------------------------------------------------------

I even tried to run it on RedHat 6.2 with egcs-2.91.66 and glibc-2.1.3-21,
without any success. I may have done errors in code, due to lack of time(!),
but I am mostly confused about the fact that it runs on windows w/o probs.
Since I am supposed to develop on UNIX, it would be nice if someone could
mail a solution to me =)

Cheerz.
Admir Efendic, Stockholm, Sweden







reply via email to

[Prev in Thread] Current Thread [Next in Thread]