Problemas con new y delete
Publicado por Pablo (3 intervenciones) el 21/05/2002 01:29:37
Hola, les cuento que estoy desarrollando una biblioteca en linux para trabajar con sockets en C++ pero tengo problemas con asignacion dinamica de memoria, me tira "violacion de segmento", les muestro el codigo, pruebenlo y por favor corrijan cualquier error se lo desean, aqui va:
------------------------------------ Nhost.h----
#ifndef _NHOST_H_
#define _NHOST_H_
namespace SocketII
{
class NHost
{
public:
NHost(const char*);
const char *dameIP() throw(const char*);
~NHost();
private:
const char *dir;
};
}
#endif // _NHOST_H_
------------------------------------------fin nhost.h
-------------------------------------------------nhost.cpp
#include "./includes/nhost"
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstring>
using namespace std;
using namespace SocketII;
NHost::NHost(const char *dire)
{
dir=new char[sizeof(*dire)+1];
dir=strcpy((char*)dir,dire);
}
const char *NHost::dameIP() throw(const char*)
{
struct hostent *h;
if((h=gethostbyname(dir))==NULL)
{
switch(h_errno)
{
case HOST_NOT_FOUND:
throw "gethostbyname(): h_errno == HOST_NOT_FOUND";
break;
case NO_ADDRESS:
throw "gethostbyname(): h_errno == NO_ADDRESS";
break;
case NO_RECOVERY:
throw "gethostbyname(): h_errno == NO_RECOVERY";
break;
case TRY_AGAIN:
throw "gethostbyname(): h_errno == TRY_AGAIN";
break;
}
return NULL;
}
else
{
return inet_ntoa(*((struct in_addr*)h->h_addr));
}
}
NHost::~NHost()
{
delete [] dir;
}
---------------------------------------------fin nhost.cpp
---------------------------------------------test.cpp
#include "includes/nhost"
#include <iostream>
using namespace std;
using namespace SocketII;
main()
{
NHost er("1234567890123456");
try{
er.dameIP();
}catch(const char *e)
{
cout << "Excepcion: " << e << endl;
}
}
------------------------------------fin test.cpp
Gracias.
------------------------------------ Nhost.h----
#ifndef _NHOST_H_
#define _NHOST_H_
namespace SocketII
{
class NHost
{
public:
NHost(const char*);
const char *dameIP() throw(const char*);
~NHost();
private:
const char *dir;
};
}
#endif // _NHOST_H_
------------------------------------------fin nhost.h
-------------------------------------------------nhost.cpp
#include "./includes/nhost"
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstring>
using namespace std;
using namespace SocketII;
NHost::NHost(const char *dire)
{
dir=new char[sizeof(*dire)+1];
dir=strcpy((char*)dir,dire);
}
const char *NHost::dameIP() throw(const char*)
{
struct hostent *h;
if((h=gethostbyname(dir))==NULL)
{
switch(h_errno)
{
case HOST_NOT_FOUND:
throw "gethostbyname(): h_errno == HOST_NOT_FOUND";
break;
case NO_ADDRESS:
throw "gethostbyname(): h_errno == NO_ADDRESS";
break;
case NO_RECOVERY:
throw "gethostbyname(): h_errno == NO_RECOVERY";
break;
case TRY_AGAIN:
throw "gethostbyname(): h_errno == TRY_AGAIN";
break;
}
return NULL;
}
else
{
return inet_ntoa(*((struct in_addr*)h->h_addr));
}
}
NHost::~NHost()
{
delete [] dir;
}
---------------------------------------------fin nhost.cpp
---------------------------------------------test.cpp
#include "includes/nhost"
#include <iostream>
using namespace std;
using namespace SocketII;
main()
{
NHost er("1234567890123456");
try{
er.dameIP();
}catch(const char *e)
{
cout << "Excepcion: " << e << endl;
}
}
------------------------------------fin test.cpp
Gracias.
Valora esta pregunta


0