Correct allocation of memory inside methods/functions

Good night,

The problem is the following, I am making a wrapper object of sockets in c++, since the ones in c are somewhat ugly, and not quite OOP.
I can't use boost sockets since the project I am currently working on, can only use the libraries found in ubuntu 12.04 repositories, and the ubuntu 12.04 repositories are on boost-1.46.
My solution was the following, three classes, AbstractSocket, ServerSocket, and Socket. AbstractSocket is the superclass of both ServerSocket and Socket. The following is the class definition of each of these classes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class AbstractSocket {
  public:
    AbstractSocket();
    AbstractSocket(const int file_descriptor, const struct addrinfo* local_address, 
                   const int local_port, const struct addrinfo* remote_address, 
                   const int remote_port, const SocketState state);
    virtual ~AbstractSocket();
  protected:
    int file_descriptor_;
    struct addrinfo* local_address_;
    int local_port_;
    struct addrinfo* remote_address_;
    int remote_port;
};

class Socket : public AbstractSocket {
  public:
    Socket();
    virtual ~Socket();
    Socket(const Socket& other);
    Socket& operator=(const Socket& other);
    Socket(const AbstractSocket& other);
    bool open(const std::string& hostname, const int port);
    bool close();
    int send(unsigned char* buffer, int buffer_size);
    int recv(unsigned char* buffer, int buffer_size);
    int send_all(unsigned char* buffer, int buffer_size);
    int recv_all(unsigned char*& buffer, int &buffer_size);
    int get_error();
  private:
    int error_;
};

class ServerSocket : public AbstractSocket {
  public:
    ServerSocket();
    virtual ~ServerSocket();
    ServerSocket(int back_log);
    int get_error();
    bool open(int port);
    bool close();
    bool accept(Socket*& client_socket);
  private:
    int error_;
    int back_log_;
};


My problem and question is the following, on ServerSocket::accept method I have to do a "client_socket = new Socket(abstractSocket);", and in Socket::recv_all method I do something like "buffer = new unsigned char[bytes_to_receive];".
These two situations are somewhat problematic to me, because I don't wish to make the caller of the methods responsible for deleting this memory.

Therefore, is there an elegant way to retrieve heap allocated space from a function/method? Or better yet, is there an elegant way of telling the caller of the method, that the memory will have to be deleted?

I thought of placing an "unsigned char* buffer" inside Socket class, and to delete the memory allocated in the destructor and on consecutive calls to recv_all, but i don't believe this is the solution, and it doesn't solves me the problem for the "accept" method.

Thanks in advance for any answer.
Last edited on
Wrap the pointer in an smart pointer, or better don't use pointers.
To handle dynamic arrays use std::vector

¿why can't you bool ServerSocket::accept( Socket & ) ?
The reason why I cant bool ServerSocket::accept(Socket &) is because I need to change the file descriptor, and I don't want to place a public method in class Socket that allows to change the file_descriptor.
¿how does passing a pointer would make a difference?
Topic archived. No new replies allowed.