Thread: Changing a Structures Members array size

  1. #1
    Registered User Xei's Avatar
    Join Date
    May 2002

    Question 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:
    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:
    //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. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    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:

    class big_fd_set : public fd_set {
    BYTE extra_bytes[VALUE];
    fd_set& operator() (){ 
      return *this; 
    Last edited by Sebastiani; 11-07-2002 at 07:55 PM.
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting the size of an array of structs
    By steve1_rm in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2008, 06:29 AM
  2. How To Declare and Dynamically Size a Global 2D Array?
    By groberts1980 in forum C Programming
    Replies: 26
    Last Post: 11-15-2006, 09:07 AM
  3. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM
  4. templates, unresolved external error
    By silk.odyssey in forum C++ Programming
    Replies: 9
    Last Post: 06-09-2004, 04:39 PM
  5. Replies: 11
    Last Post: 03-25-2003, 05:13 PM