C/Visual C - Problemas con new y delete

 
Vista:

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.

Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Problemas con new y delete

Publicado por chuidiang (677 intervenciones) el 21/05/2002 04:45:00
El problema está en el strcpy.
El sizeof(*dire) es un sizeof(char) = 1 byte, no la longitud de la cadena. Estas reservando menos espacio del que necesitas.

Para hacerlo bien tienes que usar la función strlen()

dir = new char [ strlen(dire) + 1];

Se bueno.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Problemas con new y delete

Publicado por Pablo (3 intervenciones) el 21/05/2002 05:26:13
Lo que pasa es que soy novato, do you understand?
Agradezco profundamente tu cordialidad y sigan asi por los siglos de los siglos porque este foro se la banca. Gracias.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar