Thread: Initialize struct vector

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Initialize struct vector

    Hi,

    How to set the size of a struct vector?

    I got until this:

    Code:
    typedef struct DES_ks
    {
        union
        {
            unsigned char cblock[8];
            unsigned long deslong[2];
        } ks[16];
    } DES_key;
    
    vector<DES_key> v =
     { // start of initializer list for struct example
     { // start of initializer-list for ks
        {0}, // initializes first element of the union
        {0} // initializes second element of the union
     } };
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by Ducky View Post
    Hi,

    How to set the size of a struct vector?
    This _compiles_ for me. No idea whether it works as expected.

    Code:
    #include <vector>
    
    
    using namespace std;
    int main(int argc, char *argv[])
    {
        struct data 
        {
            int data[2];
            int value[3];
        };
    
    
        vector<data> v (30, 
                { 
                 {0, 0},
                 {3, 4, 5},
                }
                );
    
    
        return 0;
    }
    Edit: Yeah, works as expected.
    Last edited by Aslaville; 05-06-2016 at 07:34 AM.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks!
    So the 30 goes for the union array ks[16]; ?
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    No the 30 will be the size of the vector. Note Alsaville's code is using a structure not a union so the values in the braces initialize the two arrays of the first element of the vector, the rest of the vector elements are default initialized.

    Jim

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    So how do you initialize a union inside a struct because this one wont compile for me.
    I get "error C2143: syntax error: missing ';' before '{'"

    Code:
    typedef struct DES_ks
    {
        union
        {
            unsigned char cblock[8];
            unsigned long deslong[2];
        } ks[16];
    } Data;
    
    
     vector<Data> v = ( 200,
     { { 
        {0}, 
        {0}, 
     } } );
    Last edited by Ducky; 05-07-2016 at 12:09 AM.
    Using Windows 10 with Code Blocks and MingW.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think your misunderstanding lies here:
    Code:
    {0}, // initializes first element of the union
    {0} // initializes second element of the union
    Here is what the C++ standard has to say about unions:
    Quote Originally Posted by C++11 Clause 9.5 Paragraph 1
    In a union, at most one of the non-static data members can be active at any time, that is, the value of at most one of the non-static data members can be stored in a union at any time. (...) The size of a union is sufficient to contain the largest of its non-static data members. Each non-static data member is allocated as if it were the sole member of a struct.
    So, your attempt to initialise the first element and the second element is bogus: at any given time, it is as if the union only has one element, even if in fact it has more than one element.

    From what I see, you only need to write:
    Code:
    auto v = std::vector<Data>(200);
    This will already value initialise the Data elements.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Ducky View Post
    So how do you initialize a union inside a struct because this one wont compile for me.
    I get "error C2143: syntax error: missing ';' before '{'"

    Code:
    typedef struct DES_ks
    {
        union
        {
            unsigned char cblock[8];
            unsigned long deslong[2];
        } ks[16];
    } Data;
    
    
     vector<Data> v = ( 200,
     { { 
        {0}, 
        {0}, 
     } } );
    This syntax is bogus. You should know that to call the constructor of a class, you should write

    Type NameOfVar(ArgumentsToConstructor);

    Hence, you should write

    Code:
    vector<Data> v(200,
     { { 
        {0}, 
        {0}, 
     } } );
    (Assuming everything else is right.)
    Also there's no need for typedef whatsoever. This isn't C, so let's simplify this. Instead, we can write:

    Code:
    struct Data
    {
        union
        {
            unsigned char cblock[8];
            unsigned long deslong[2];
        } ks[16];
    };
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Awesome, now its working!

    Thanks Laserlight and Elysia for the detailed explanations!
    Last edited by Ducky; 05-07-2016 at 02:41 AM.
    Using Windows 10 with Code Blocks and MingW.

  9. #9
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by Ducky View Post
    Awesome, now its working!

    Thanks Laserlight and Elysia for the detailed explanations!
    What is working ? I have been waiting to see the solution

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Its a large C program that I transformed into c++.

    Replaced mallocs with vectors.
    Using Windows 10 with Code Blocks and MingW.

  11. #11
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by Ducky View Post
    Its a large C program that I transformed into c++.

    Replaced mallocs with vectors.
    I meant how did you solve the above issue ?

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Quote Originally Posted by Aslaville View Post
    I meant how did you solve the above issue ?
    Just like Laserlight showed me:

    Code:
    vector<DATA> ks1(200);
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initialize std::vector in .h?
    By SterlingM in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2011, 12:32 PM
  2. Initialize struct in function
    By lordkrandel in forum C Programming
    Replies: 19
    Last Post: 04-17-2011, 12:37 AM
  3. Replies: 2
    Last Post: 10-18-2010, 02:55 AM
  4. Overwrite initialize or predefine value in C struct
    By flyer87 in forum C Programming
    Replies: 1
    Last Post: 06-16-2010, 12:28 AM
  5. How to initialize variable in struct?
    By gogo in forum C Programming
    Replies: 2
    Last Post: 04-19-2003, 09:53 AM

Tags for this Thread