Char,Struct,Class ? Im Confused.

//Id enjoy help.. :/ Im out of ideas.. i dont know what im doing wrong
// (In the source its a different void wich inserts the data.. not int main Since its a plugin loaded into a programm)
//I located the position where it crashes see: ZData::Find @ the second printf
// .cpp
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
PData **PDatas = 0; // I dont know this 1 either.. (Seems to create PDatas, but of what/to what.. 0..?
void ZData::Startup() //I Init. on start
{
	PDatas = (PData **)malloc(sizeof(PData) * 128);
	memset((void *)PDatas, 0, sizeof(PData) * 128);
		printf("zdata Startup\n");
}
void ZData::Shutdown() //And this on Quit
{
        for (int i = 0; i < 128; ++i)
        {
                if (PDatas[i] != 0)
                {
                        free((void *)PDatas[i]);
                }
        }
        PDatas = 0;
		printf("zdata shutdown\n");
}

PData *ZData::Find(int ID) //tbh i dunno whats this for (Some1 told me i need this.. idk whats it for (
{
	printf("Do you crash!?\n"); //If this message shows up.. i atleast know it crashes after this
	if(!PDatas)
	{
 printf("Do you get PDatas at all?\n"); //doesnt show up.
		return 0;
	}
	if(!PDatas[ID])
	{
      printf("Do you get PDatas ID?\n"); //Crashes here.. this message shows up
		return 0;
	}
	return PDatas[ID];
        printf("Well crash?\n"); 

}
int main(int ID,const char *Nick) {
	PData *p = ZData::Find(ID);
	p[ID].SomeDistance=1;
	p[ID].SomeNumber=1;
	p[ID].ID=ID;
	p[ID].Name=Nick;
}

//.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct PData
{
	int SomeDistance;
	int SomeNumber;
	int ID;
	const char *Name;
	cPlayer *data;
};
class ZData
{
public:
	static void Startup();
	static void Shutdown();
	static PData *Find(int ID);
};


Compiles without any error!
Last edited on
You probably want to call ZData::Startup() in main() before everything else. Moreover you are not allocating (and deallocating) the memory correctly.

You should do something like:

1
2
3
4
5
6
7
void ZData::Startup() //I Init. on start
{
  PDatas = (PData **)malloc(sizeof(PData*) * 128); // Allocate memory for pointers to PData
  for(int i = 0; i < 128; i++ ) {
    PDatas[i] = (PData*)malloc(sizeof(PData) * 128);
  }
}


And change main second argument type:

int main(int ID,const char **Nick)


By the way consider using new/delete instead of malloc/free. They are better for many reasons that you can find here:

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.4


Last edited on
You probably want to call ZData::Startup() in main() before everything else. Moreover you are not allocating (and deallocating) the memory correctly;


Im calling them before anything else but lets try the new/delete

PS:
And change main second argument type:

int main(int ID,const char **Nick)

but if i change it in the int i also have to change it in the struct.. ? If im right.. and if i do that.. whats the reason why ive done that?
Last edited on
Why all of the C constructs in C++ code?
Im calling them before anything else

Where?

but if i change it in the int i also have to change it in the struct.. ? If im right.. and if i do that.. whats the reason why ive done that?


You have to change it because main(...) takes an int and an array of char* (char**) as arguments.
You do not have to modify the struct. Instead do something like:

p[ID].Name=*Nick or p[ID].Name=Nick[0].
And by the way, I strongly suggest you to review arrays and pointers on a book.
Topic archived. No new replies allowed.