Thread: Guaranteed Padding?

  1. #1
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138

    Guaranteed Padding?

    Given a class of the form

    Code:
    class DataVector
    {
         public:
              DataVector(int i, int j, int k){
                   _vals[0] = i;
                   _vals[1] = j;
                   _vals[2] = k;
              }
    
              int* data(void){return _vals;}
    
         private:
              int _vals[3];
    }
    and assuming there is _no_ other data in the class, do the language specs guarantee the following snippet is valid?

    Code:
    std::vector<DataVector> myVec(2);
    myVec[0] = DataVector(0,1,2);
    myVec[1] = DataVector(3,4,5);
    
    int* dP = myVec[0].data();
    std::cout<<dP[0]<<" "<<dP[1]<<" "<<dP[2]<<" "<<dP[3]<<" "<<dP[4]<<<<" " <<dP[5];
    //should output 0 1 2 3 4 5

    The reason I'm implementing the code this way is that I may need to pad the 3 dimensional vector to 16 bytes with an extra element depending on some external factors that are currently out of my control. If there are better ways of doing this, I'm open to suggestions.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Not at all.

    Each instance of DataVector is a completely separate instance.

    There is no reason for them to be contiguous, nor any reason for one to follow the other in memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    However, correct me if I'm wrong, std::vectors use a contiguous block of memory and store their own copy of the data. Are you implying that the data itself may be stored non-contiguously but that pointers to the data are stored contiguously?

    Also, unless I'm missing something, 34.3 here seems to be referring to a similar case, yes?
    Last edited by golfinguy4; 01-03-2011 at 01:55 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    OIC, you're making copies of the DataVector when you assign the elements of your vector.

    Your DataVector's will be contiguous (as per the link you posted).

    The problem remains however that you don't know how much padding there is at the end of each instance of DataVector. Now since your struct contains only one data element (and it's a POD), there doesn't seem to be any need for the compiler to add any padding.

    I think practically, the answer is "no padding", but there is no guarantee of that.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Ah ok, you were referring to the data in the class itself. Makes sense.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by golfinguy4 View Post
    The reason I'm implementing the code this way is that I may need to pad the 3 dimensional vector to 16 bytes with an extra element depending on some external factors that are currently out of my control. If there are better ways of doing this, I'm open to suggestions.
    I suggest you add a member function, say print() to the DataVector class. This might do something like;
    Code:
    void DataVector::print(std::ostream &s)
    {
         s << _vals[0] << ' ' << _vals[1] << ' ' << _vals[2];
    }
    To print a vector of them, you might do;
    Code:
    for(std::vector<DataVector>::iterator i = myVec.begin(), end = myVec.end(); i != end; ++i)
    {
         std::cout << ' ';
         (*i).print(std::cout);
    }
    std::cout << '\n';
    This way, the only thing that needs to change when you change your DataVector class is its implementation of print().

    No need to worry about padding at all.

    Incidentally, it is a bad idea to use underscores as the first character in any identifier in your code, unless you are implementing the standard library for your compiler. The C++ standard reserves such identifiers for use by the implementation, and your code gives undefined behaviour if you use reserved identifiers.
    Last edited by grumpy; 01-03-2011 at 06:41 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printf format specifier padding
    By Blasz in forum C Programming
    Replies: 3
    Last Post: 07-08-2010, 11:26 PM
  2. string padding and replacement functions
    By George2 in forum Tech Board
    Replies: 4
    Last Post: 11-19-2006, 01:40 AM
  3. Prob with padding in BMP image files
    By tin in forum Game Programming
    Replies: 2
    Last Post: 01-09-2006, 08:23 AM
  4. Replies: 5
    Last Post: 04-16-2004, 01:29 AM
  5. Structure Padding, pragma pack...
    By P.Phant in forum C Programming
    Replies: 4
    Last Post: 06-04-2003, 05:56 AM