Guaranteed Padding?

This is a discussion on Guaranteed Padding? within the C++ Programming forums, part of the General Programming Boards category; Given a class of the form Code: class DataVector { public: DataVector(int i, int j, int k){ _vals[0] = i; ...

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

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    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 12:55 PM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    5,851
    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 05:41 PM.
    Right 98% of the time, and don't care about the other 3%.

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, 12:40 AM
  3. Prob with padding in BMP image files
    By tin in forum Game Programming
    Replies: 2
    Last Post: 01-09-2006, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21