# Thread: Changing a Structures Members array size

1. ## Changing a Structures Members array size

This is a simple question, so it wont take up too much of anyones time.

The Structure 'fd_set' is defined as so:
Code:
```typedef struct fd_set {
u_int fd_count;
SOCKET fd_array[FD_SETSIZE];
} fd_set;```
So the question I ask you is IF my method of increasing the size of 'fd_array' is stable. Here is my method explaining my logic behind it:
Code:
```//Lets say that we want fd_array to have 3 index's, 0-2.
fd_set *Set; //Pointer to RealSet eventually.
BYTE RealSet[(3 * 256)+4+4];
//256 is the sizeof fd_array, 4 is the size of fd_count, and the other 4 is the size of the pointer(being Set);
Set = (fd_set *)RealSet;
//Now we can store(without causing a memory leak) 3 SOCKET members in fd_array, if we store something it actually is stored into RealSet, Set is simply a pointer to RealSet.
Set->fd_array[0] = Sock1; //Example
//Or we could use:
Set[0].fd_array[0] = Sock1;```
I believe that my method works fine, however, I have two questions regarding my method:
#1. Should RealSet have the size of the Pointer(being 4) also added on to its size?
#2. When increasing the size of a member in a structure, should I be using the size of the member, or the size of the structure(when multiplying. In this case I used 256 which is the size of the fd_array, and 260 is the size of fd_set, and 4 is the size of fd_count, and 4 is the size of the pointer to fd_set).

The reason I ask these questions is to make sure that I will not cause memory leaks in the future when I may have slightly more complicated structures. Thankyou.

2. I don't believe so. First off, when you cast the bytes to an fd_set, you are going to lose the extra bytes due to trimming. Second, all of the functions using fd_sets will constantly use expressions like "sizeof(fd_set)", again which would trim your bytes. Ok. So why not just redefine FD_SETSIZE? But then again, it looks like you're just trying to create an array of fd_set's, is this true? Then just do:
fd_set Set[3];

Finally, when you add bytes to the end of a structure, remember that the compiler may or may not place padding between the variables, resulting in a bad deal altogether. Still, if you must try so, there are better options. Consider:

Code:
```class big_fd_set : public fd_set {
public:
BYTE extra_bytes[VALUE];
fd_set& operator() (){
return *this;
}
};```

Popular pages Recent additions