0

Sorry for the confusing title...

I've been playing around with C++, working on a project to parse a game's (Kerbal Space Program) save file so I can modify it and eventually send it over a network. I'm stuck with storing an unknown number of vessels and crew members, so I need to have an array of unknown size.

Is this possible? I figured having a pointer to an array would be the way to go.

I have:

class SaveFileSystem 
{
    string version;
    string UT;
    int activeVessel;
    int numCrew;
    ??? Crews; // !!
    int numVessels;
    ??? Vessels; // !!
}

Where Crews and Vessels should be arrays of structures:

struct Crew 
{
    string name;
    //Other stuff
};

struct Vessel
{

    string name;
    //Stuff
};

I'm guessing I should have something like:

    this->Crews = new ???;
    this->Vessels = new ???;

in my constructor to initialize the arrays, and attempt to access it with:

this->Crews[0].name = "Ship Number One";

Does this make any sense??? I'd expect the "???"'s to involve a mess of asterisk's, like "*struct (*)Crews" but I have no real idea. I've got normal pointers down and such, but this is a tad over my head... I'd like to access the structures like in the last snippet, but if C++ doesn't like that I could do pointer arithmetic.

I've looked into vectors, but I have an unhealthy obsession with efficiency, and it really pains me how you don't know what's going on behind it.

12
  • c++ has list and vector as a variable-number element array Commented Sep 9, 2012 at 21:26
  • 3
    Your "unhealthy obsession" is more of an "unhealthy refusal" to understand what a vector is. Commented Sep 9, 2012 at 22:02
  • 1
    You "don't know what's going on behind it" because you are struggling with pointers, arrays and heap. That's all vectors really do, and they do it cleverly. When you use vectors correctly, they are very efficient. Now, if you are always obsessed with efficiency, ask yourself how much it matters. I suggest you code for the problem at hand and do not over-engineer your solutions at the expense of clarity. When you actually NEED efficiency, you'll damn-well know it. Commented Sep 9, 2012 at 22:22
  • But, unhealthy it is. Definitely. (@paddy perhaps, sometimes, deques would be even more clever, but your point is correct as it stands) Commented Sep 9, 2012 at 22:24
  • 2
    I recently wrote a blog entry about some of what's going on inside of std::vector, that (among other things) has a bit about why it's probably more efficient than what you're trying to do instead. Commented Sep 9, 2012 at 22:24

2 Answers 2

8

Use vectors. Vectors are a standard C++ class which are dynamically resizable and carry their size with them. Using vectors your class becomes this

class SaveFileSystem 
{
    string version;
    string UT;
    int activeVessel;
    vector<Crew> Crews;
    vector<Vessel> Vessels;
}

Your unhealthy obsession with vectors is misplaced. Seriously I doubt if they are any less efficient than anything you might code. Do you think the writers of vector for your compiler were incompetent?

I see you are happy to use string. Why does your argument about efficiency and not knowing what is going on behind the scenes not apply to string? It's exactly the same situation.

Sign up to request clarification or add additional context in comments.

10 Comments

It's a bit different, though. String is easier to use than it's pure array counterpart. A vector is different, with its iterators and whatnots. You also don't know how much memory is being allocated, or a simple way to access its elements. I appreciate the effort, though.
@user1658731 So, you have an unhealthy obsession with not wanting to learn iterators "and whatnots"?
I don't like them. You have to push back and can't jump around from place to place.
@user1658731 Don't swear. Just read a little more. Assumption is the mother of all f*ckups and certainly of a bad reputation on a QA site like this.
@Geeny: No one is yelling at you. THIS WOULD BE YELLING AT YOU. What everyone has been trying to tell you is that memory management for arrays of items are nowhere near as trivial as people think it is. The writers of std::vector put in a lot of thought to make sure their std::vector class doesn't catastrophically die with the presence of exceptions, etc. The fact that you demonstrate that you don't understand how std::vector works means you will not be able to achieve the same attention to detail as the writers of std::vector have achieved.
|
3

Using std::vector or std::list would probably be the better (and more efficient) way to go, but if you really want to use pointers and new and such, you can do it like this:

Declaring the variables:

Crew* Crews;
Vessel* Vessels;

In your constructor:

// initialize numCrew and numVessel before doing this:
this->Crews = new Crew[numCrew];
this->Vessels = new Vessel[numVessel];

You can then access the arrays as wanted.

After you have done want you want with the arrays, delete them with

delete[] this->Crews;
delete[] this->Vessels;

The [] are essential.

Also note, that using "this->" is not necessary in most cases.

4 Comments

I know that, read the first line of my answer. The question asks for these things.
Thank you! It compiles and runs without exploding, I probably just need to debug a little.
@user1658731 why do you need to debug it a little? Are you unsure of whether it works? Why?
It's crashing on the "Other Stuff" I'm reading. I'm trying to use fstream to input directly into a bool value, which I guess is bad. I also had a problem with names which had more than one word... It's cool, I just got the program to compile like an hour ago... Using Visual Studio's debugger, the data is being stored correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.