Like Tree1Likes
  • 1 Post By laserlight

filling Structure dynamically

This is a discussion on filling Structure dynamically within the C++ Programming forums, part of the General Programming Boards category; I have a Struct containing about a 300 variables Code: struct Test { int variable1; int variable 2; . . ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    4

    filling Structure dynamically

    I have a Struct containing about a 300 variables
    Code:
    struct Test 
    { 
       int variable1;
       int variable 2;
        .
        .
        .
       int variable300;
    
    }
    and i have a string containing all the values needed to fill the struct.
    is there a way to dynamically fill the data without having to set them as
    Code:
     Test.variable1 = String1;
     Test.variable2 = String2;
                        .
                        .
                        .
    Test.variable300 = String300;
    can we loop through the structures variables and fill them ?

    Thx

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Yeah, there is:
    Code:
    struct Test
    {
        int variable[300];
    };
    Devoted my life to programming...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    Quote Originally Posted by alaash
    I have a Struct containing about a 300 variables
    "That is why you fail." - Yoda

    Why don't you use an array or some container?
    rags_to_riches likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    actually the structure doesn't have the same variable type , it has arrays , and different variable types. in the example above , i was just trying to deliver the idea behind the question.
    Anyways, i found the solution it can be done using union to merge to structs (the original structure , and another one containing just an array) where they will both write to the same memory thereby writing to the second struct will modify the first.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,438
    Quote Originally Posted by alaash View Post
    Anyways, i found the solution it can be done using union to merge to structs (the original structure , and another one containing just an array) where they will both write to the same memory thereby writing to the second struct will modify the first.
    That won't work if the compiler inserts padding into structs. Practically, most compilers introduce padding by default for performance reasons, and the amount of padding between two consecutive elements in a struct is compiler-dependent. And that's assuming your structs are PoD (Plain Old Data). There is virtually no way your approach will work for non-PoD types, or if any of the struct members are non-PoD.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    You could put all those members into a std::tuple ... but for 300 different types , it is somewhat unrealistic to write.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    2,503
    Quote Originally Posted by manasij7479 View Post
    You could put all those members into a std::tuple ... but for 300 different types , it is somewhat unrealistic to write.
    and compiling it would likely take hours. I seem to recall that compile time for tuples increases exponentially as you add more template parameters.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    Quote Originally Posted by alaash
    actually the structure doesn't have the same variable type , it has arrays , and different variable types. in the example above , i was just trying to deliver the idea behind the question.
    Well, okay. What on earth (or outside of it) is this structure?

    Besides, if you can bear the tedious effort of declaring all those members, you can bear the tedious effort of assigning to them individually.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Out of curiosity: What is this überstruct you're working with. I don't think I ever saw a struct, object or what have you with more than maybe 20 instance variables...

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by alaash View Post
    I have a Struct containing about a 300 variables
    Okay I'm going to stop you right there. Whatever you were going to ask after that point is not particularly important. This is the primary problem right here.
    300 is beyond excessive. That's beyond the 1 in a million chance that you might have stumbled upon the one situation where it actually makes sense to do something a certain unusual way. Straight up, that is not how to program, least of all how to program in C++.
    You may as well be asking how to clean an entire airport runway with a toothbrush. We can't answer such a question except by suggesting that you need a different approach.

    That said, you'll now need to give more information about what is in this structure. Also, how is this structure used? Is it a singleton, or are there many instances? Is it used polymorphically, or to represent more than one possible thing?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help filling a structure with data from a file.
    By System_159 in forum C++ Programming
    Replies: 41
    Last Post: 09-27-2006, 06:39 PM
  2. Filling a structure
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 12-05-2005, 03:00 AM
  3. Quick way of filling structure?
    By voodoo3182 in forum C Programming
    Replies: 3
    Last Post: 08-05-2005, 07:28 AM
  4. Filling an array in a structure
    By thephreak6 in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:05 PM
  5. dynamically allocate a structure
    By dgraver01 in forum C Programming
    Replies: 3
    Last Post: 07-11-2002, 02:23 PM

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