Struct problems

This is a discussion on Struct problems within the C++ Programming forums, part of the General Programming Boards category; Hi ive been trying to create a simple struct within another struct within another works create can access all the ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    9

    Unhappy Struct problems

    Hi ive been trying to create a simple struct within another struct within another works create can access all the data members fine. My problem is simply, How can i have a dynamic struct array from within a struct?

    for example:
    Code:
    struct Sats
    {
    	enum sat_type{station, astroid, moon};
    	int height;	// height it orbits from planet
    };
    struct Planets
    {
    	float radius;						// radius of planet
    	int days;						// how many days does the planet have a year?
    	int distance;						// distance it orbits from star
    	
    	Sats sats[no_of_sats];
    };									
    struct star_system
    {
    	Planets planet[no_of_planets];
    };
    what i want to be able to do is use this structure to load data from a file. the no_of_planets and no_of_sats would be loaded from the file along with data for the other data members. the data could be different everytime the program loads or even be changed after its loaded.

    Regards,
    Paul

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    9
    opps forgot to mention the error i am getting:
    "error C2057: expected constant expression"

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    You can't have arrays defined at run-time that way. You would need to allocate the memory dynamically, or use the std::vector class, which does it for you.

    Research the Standard Template Library (STL) vector class. It should solve your problem.

    (And, btw, you misspelled asteroid.)

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    9
    cheers i'll do that.

    lol i didn't think it looked right, it's gettin late and i was to tired to see the mistake

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    9
    hmm... the STL is overly complicated for what i want to do, plus i would rather become more expereinced with the basics of c++. How could i dynamically create a struct arry? I havn't tried much dynamic memory stuff...

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Actually, the STL is by far the preferred way, and if basic C++ courses neglect STL, they shouldn't. STL is pretty much the core of modern C++ programs, it's not an advanced topic nor an unnecessary one.

    Dynamic memory allocation will require you to create a constructor, destructor, assignment operator, and copy constructor for each structure that has dynamic memory, to make your classes robust -- otherwise, you'll be programming in horrible style which can easily lead to memory leaks.

    The pre-STL way, besides being outdated, is more complex than std::vector.

    Here's the minimum code you'd need for a (somewhat) safe pre-STL. If there are no exceptions thrown, it doesn't leak memory. It does NOT guarantee strong exception safety:

    Code:
    
    class Planet{
    private:
        int no_of_sats;
        Sats * sats;
    public:
        float radius;
        int days;
        int distance;
    
        Planet(int n = 0) : no_of_sats(n),sats(NULL){
            if (n) sats = new Sats[n];
        }
    
        ~Planet(){
            delete[] sats;
        }
    
        Planet(Planet& p2): no_of_sats(p2.no_of_sats),radius(p2.radius),
                            days(p2.days), distance(p2.distance), sats(NULL){
            if (no_of_sats){
                sats = new Sats[no_of_sats];
                for (int i = 0; i < no_of_sats; i++)
                    sats[i] = p2.sats[i];
            }
        }
    
        Planet operator= (Planet & p2){
            delete[] sats;
            no_of_sats = p2.no_of_sats;
            radius = p2.radius;
            days = p2.days;
            distance = p2.distance;
            if (no_of_sats){
                sats = new Sats[no_of_sats];
                for (int i = 0; i < no_of_sats; i++)
                    sats[i] = p2.sats[i];
            }
            else sats = NULL;
            return *this;
        }
    
        Sat & Sats(int i){
            return sats[i];
        }
    
    };
    I recommend against this method. You should understand it, be able to read it, and know why it works (and when it doesn't). However, this code is more complicated and not as useful as the vector implementation, which can be very, very simple. Vectors will be correctly copied, so you don't need to worry about copying or assignment, and they're freed so you don't need to worry about destructors.
    Last edited by Cat; 06-11-2003 at 07:38 PM.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The particularly dangerous part which cat was alluding to was the array you have in your structure. For scalar types, a shallow copy (simple assignment as the compiler would automatically make for you) will be sufficient. For arrays, they will not.

    In your structure, 'sats' is a pointer to a block of memory of given size, not the block of memory itself. A shallow copy would simply assign the pointer in the new struture to the same block of memory (not create a new block and then copy all the members over). When this happens, if the first object is destroyed, the block of memory is freed, and available to be reassigned. Therefore, reading from it may given you the data written by a completely different part of your program, and similarly writing to it may cause part of your program to enter an illegal state.

    For a small program, you may never notice the memory leak, but it has the potential to crash the program, and often, in larger systems, it will.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  3. weird typedef struct problems
    By olidem in forum C Programming
    Replies: 3
    Last Post: 07-28-2008, 02:59 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM

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