program corruption

I am making a game, I am using vectors to store npc data. I run my program in a way that it gets a segmetation fault, but instead I get this (https://drive.google.com/file/d/0B_o-VUdaT8XiQjJGSWZGVHdBVEk/view?usp=sharing)

my code for main.cpp is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include "map.h"
    G_map game_map;
#include "npc.h"
game_npc npc;


using namespace std;

int main()
{
    npc.initialize(10);
    npc.teleport_to_game(11/*player*/,9/*x*/,9/*z*/);
    cout << "Hello world!" << endl;
    return 0;
}


my code for npc.h is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <vector>
#include <cmath>


class game_npc{
private:
    float base_hp=20;
    float base_distance=10;

    std::vector<float> hp;
    std::vector<float> view_distance;
    std::vector<float> X;
    std::vector<float> Z;
    std::vector<bool> spotted;
    std::vector<float> SX;
    std::vector<float> SZ;

public:
    //int npcs=10;
    int initialize(int);
    void teleport_to_game(int,float,float);
    bool inti=false;


};


int game_npc::initialize(int npcs){

    for(int i=0;npcs>i;i++){
        hp.push_back(base_hp);
        view_distance.push_back(base_distance);//adds new element with info thats in () each loop time
        spotted.push_back(false);
    }
    X.resize(npcs);
    Z.resize(npcs);
    SX.resize(npcs);
    SZ.resize(npcs);
    inti=true;

}


void game_npc::teleport_to_game(int _p, float _x, float _z){

X[_p]=_x;
Z[_p]=_z;

}


my code for map.h is not being used right now.

I was wondering what could cause this, why it is here. Also when the value is not 11 it works fine, even though if it is higher it does not do a sig_fault.

Thank you,
popa
Last edited on
I took a look at your stack trace, you didn't compile with -g, so there's no symbol about yout program. You need to fix that.

Your data structure looks little odd, you probably mean:
1
2
3
4
5
6
    struct state
    {
        float hp, view_distance, X, Z, SX, SZ;
        bool spotted;
    };
    std::vector<state> data;

Anyway, the actual crash is:
1
2
3
4
5
void game_npc::teleport_to_game(int _p, float _x, float _z)
{
    X[_p]=_x;
    Z[_p]=_z;
}
where _p is 11, and X.size() is 10.

You can catch this at runtime with:
1
2
3
4
5
void game_npc::teleport_to_game(int _p, float _x, float _z)
{
    X.at(_p) =_x;
    Z.at(_p) =_z;
}


You could start by passing reasonable args:
1
2
3
4
5
6
7
int main()
{
    npc.initialize(10);
    npc.teleport_to_game(9/*player*/,9/*x*/,9/*z*/);
    cout << "Hello world!" << endl;
    return 0;
}
Last edited on
Why are these vectors anyways?

1
2
3
4
5
6
7
    std::vector<float> hp;
    std::vector<float> view_distance;
    std::vector<float> X;
    std::vector<float> Z;
    std::vector<bool> spotted;
    std::vector<float> SX;
    std::vector<float> SZ;
They are going to be used later in the program dev., I put them there to use in later functions.
kbw, I like your idea of the data structure, but how would I implement it? Some code would be appreciated!
The code's already there, but you'd use it as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class game_npc
{
private:
    struct state
    {
        float hp, view_distance, X, Z, SX, SZ;
        bool spotted;

        state() :
	        hp{}, view_distance{}, X{}, Z{}, SX{}, SZ{}, spotted{}
	    {}
    };
    std::vector<state> data;

    float base_hp=20;
    float base_distance=10;

public:
    int initialize(int);
    void teleport_to_game(int,float,float);
    int npcs=10;
    bool inti=false;
};

int game_npc::initialize(size_t npcs){

    for (size_t i = 0; i != npcs; ++i)
    {
        state s;
        s.hp = base_hp;
        s.view_distance = base_distance;
        // s.spotted is already false
        data.push_back(s);
    }

    init = true;
}

There is normally no need for public variables and initialisation is best done in a constructor.
Avoid putting the member function definitions in the .h file. These should be put in separate .cpp files which get compiled separately then linked in to your final product.

As for the vectors, I would look at this the other way around. You have a whole host of vectors, each one to handle one piece of information for each NPC. So all the information stored in the vectors at position 1 would all refer to character 1.

I would implement this so that each NPC would be stored in its own object. Then I would have a separate class that has a vector of NPC's; this class would manage all the logic for sizing the vector, and giving references to the various NPC's that it is storing.

This way, each NPC could manage its own data and be given member functions which would manipulate the one character without risking affecting others. You only have to write vector management code once, and not for each attribute of your NPC. If you want to rewrite the way NPC's are doing things, this will not affect your vector management. etc.

You would access info this way:
npc.GetAt(3).GetHP()
rather than:
npc.GetHP(3)

Oh, yeah. That double free message tends to indicate that you freed unallocated memory, that is memory that was already freed. It is a little more specific than a segmentation violation.
Last edited on
> I run my program in a way that it gets a segmetation fault,
you run your program in a way to invoke undefined behaviour.
Undefined behaviour is undefined
THis is a project that has pieces from multiple computers, and I'm going to merge the files/peices together under your suggestion David.
Last edited on
Line 12 calls npc.initialize(10); which calls X.resize(npcs); to resize X to 10 items.

Then at line 13 you call npc.teleport_to_game(11/*player*/,9/*x*/,9/*z*/); which calls X[_p]=_x; to set X[11]. Since X has only 10 items X[11] doesn't exist and the program crashes.
Topic archived. No new replies allowed.