new unique char inside each object

1
2
3
4
5
6
7
8
9
10
11
12
13
struct ClientInfo {


	WSABUF Buffer;

	ClientInfo() {
		Buffer.buf = new char[2000];
		Buffer.len = 2000;
	}

	

};



Every new object that is created from this struct uses the same location for "Buffer.buf". When I create an object from this struct I want to have a unique "Buffer.buf" ready for each creation, which I dont get at the moment. Maybe someone have time to give me a small explantation on why?

I know I can create the object first, then assign "new char[2000] to objectName.Buffer.buf and the assign will be unique for each object. But is it possible to have this ready and done when the object is created?
Last edited on
Every new object that is created from this struct uses the same location for "Buffer.buf".
The code you have there will make each ClientInfo().Buffer.buf point to a different array of 2000 chars. I don't know what you might be seeing that makes you think they all point to a single array.


1
2
3
4
5
6
7
8
9
10
11
ClientInfo C;
map <int, ClientInfo> Clients;

Clients[2] = C; // Doesn't this create a copy of the object and store it in Clients[2]?
Clients[2].Buffer.buf[0] = 'a';

Clients[22] = C;
Clients[22].Buffer.buf[0] = 'c'; // This changes the value of Clients[2], i dont know why


cout << Clients[2].Buffer.buf[0]; // = 'c' 
If you just want to allocate new ClientInfos for keys 2 and 22 of the map you don't need lines 4 and 7, simply
1
2
Clients[2].Buffer.buf[0] = 'a';
Clients[22].Buffer.buf[0] = 'c';

That said, your definition of ClientInfo leaves a lot to be desired. Here's my version:
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
struct ClientInfo {
    WSABUF Buffer;
    std::vector<char> data;

    ClientInfo(): data(2000){
        this->reset_Buffer();
    }
    ClientInfo(const ClientInfo &other){
        *this = other;
    }
    ClientInfo(ClientInfo &&other){
        *this = std::move(other);
    }
    const ClientInfo &operator=(const ClientInfo &other){
        this->data = other.data;
        this->reset_Buffer();
        return *this;
    }
    ClientInfo(ClientInfo &&other){
        this->data = std::move(other.data);
        this->reset_Buffer();
        return *this;
    }
private:
    void reset_Buffer(){
        this->Buffer.buf = &this->data[0];
        this->Buffer.len = this->data.size();
    }
};
Last edited on
What I dont understand is (in my example) How the assign on "client 22" could affect "client 2"

Two different objects, but when I changed "clients 22 buf" to 'c' it changed "clients 2 buf" as well
You're copying an object into another, where the class of both contains a pointer and doesn't have a user-defined copy constructor or assignment operator. The result is that you're copying one pointer onto another. It's essentially the same as
1
2
3
4
5
Clients[2].Buffer.buf = new char[2000];
Clients[2].Buffer.buf = C.Buffer.buf;
//...
Clients[22].Buffer.buf = new char[2000];
Clients[22].Buffer.buf = C.Buffer.buf;
Topic archived. No new replies allowed.