i create the array like this:
NumOfConditions = 1; NumOfDialogs = 1; for(int i=0;i<NumOfConditions;i++) { conditions<i> = new int[2]; } conditions[0][0] = ALWAYS; conditions[0][1] = 0;
ALWAYS is defined as 0
then when i got to retrieve the value
textprintf_ex(screen, font, 150, 220, makecol(255, 255, 255), -1, "conditions: %d", conditions[0][0]);
i get values like 4042624
any ideas what im doing wrong?
edit:: i figured out that its giving me the memory address... so how do i get the value instead?
]]>How are you defining conditions?
]]>ummm, if your refering to ALWAYS then like this
#define ALWAYS 0
]]>No, I'm talking about your multi-dimensional array called "conditions".
]]>I think you should do it like this:
int *conditions; conditions = new int[NumOfConditions]; for(int i=0;i<NumOfConditions;i++) { conditions<i> = new int[2]; }
And then get rid of it with delete [] conditions;I'm not 100% sure, since I haven't really worked with dynamic arrays in C++ that much (and especially not with multidimensional ones). But try it and see for yourself.
]]>Do you have something like this somewhere in your code?int **conditions;If no then how do you define/declare the contitions variable?
]]>that could be it.. i was using
int* conditions[];
EDIT:: i change
int* conditions[];
to:
int** conditions;
without changing anything else it crashes at runtime.
if i change
int* conditions[];
to:
int* conditions;
and add:
conditions = new int[NumOfConditions];
i get compile time errors about converting an int* to int
...
so far i still havent found the solution.
]]>int* conditions[];
should be the same as int **conditions;
I don't know why your code isn't working, though
for(int i=0;i<NumOfConditions;i++) { conditions<i> = new int[2]; } conditions[0][0] = ALWAYS; conditions[0][1] = 0;
Should be:
for(int i=0;i<NumOfConditions;i++) { conditions<i> = new int[2]; conditions<i>[0] = ALWAYS; conditions<i>[1] = 0; }
And yes, you must do delete [] conditions[i]; as well as delete [] conditions;
Lots of [] symbols.
Its kind of scary because delete conditions[i]; will compile and might even run ok. But eventually the memory corruption will catch up with you and then you're really screwed.
[edit]
without changing anything else it crashes at runtime.
It could be that it detected your error of using delete when you should be using delete [] and switched it for you. But a pointer to a pointer and theres no way for it to know it should switch.
]]>Do you have conditions = new int[NumOfConditions];in your code? If you are having random values then I would guess you don't have.
]]>Hope this helps! I remember being stuck on this once....
1 | #define ALWAYS 0 |
2 | |
3 | void forumHelp() |
4 | { |
5 | int i; |
6 | |
7 | int numOfConditions = 1; |
8 | int **conditions; |
9 | |
10 | conditions = new int*[numOfConditions]; |
11 | |
12 | for (i = 0; i < numOfConditions; i++) |
13 | conditions<i> = new int[2]; |
14 | |
15 | conditions[0][0] = ALWAYS; |
16 | conditions[0][1] = 42; |
17 | |
18 | textprintf_ex(screen, font, 200, 300, makecol(255, 255, 255), -1, "Conditions: %d", conditions[0][1]); |
19 | |
20 | readkey(); |
21 | |
22 | for (i = 0; i < numOfConditions; i++) |
23 | delete[] conditions<i>; |
24 | |
25 | delete[] conditions; |
26 | |
27 | return; |
28 | } |
Things to watch for: the array 'conditions' is an array of POINTERS TO POINTERS, thus the **
To delete the array properly you have to delete all the last dimension elements first and then back to the first. (that's what the for loop is at the end)
Edit: Forgot the 'define'...
]]>EDIT:
]]>Edit:
]]>ok well i got it to work using vectors and also using the code Matt Weir posted. But it still crashes at this line:
if(worldconditions[j] == conditions<i>[0])
worldconditions[j] i checked right before the if statement and its equal to 0 which is also what conditions<i>[0] is... but it doesnt make it to the next line.
here is the code that has the error (the whole file)
1 | #include <allegro.h> |
2 | #include <string> |
3 | #include <vector> |
4 | #include "dialog.h" |
5 | #include "NPC.h" |
6 | |
7 | using namespace std; |
8 | |
9 | NPC::NPC() |
10 | { |
11 | NumOfConditions = 1; |
12 | NumOfDialogs = 1; |
13 | |
14 | //conditions = new vector<int*>; |
15 | conditions = new int*[NumOfConditions]; |
16 | for(int i=0;i<NumOfConditions;i++) |
17 | { |
18 | //conditions->push_back(new int[2]); |
19 | conditions<i> = new int[2]; |
20 | } |
21 | dialogs = new vector<string>; |
22 | |
23 | conditions[0][0] = ALWAYS; |
24 | conditions[0][1] = 0; |
25 | dialogs->push_back("Testing 123"); |
26 | }; |
27 | //14802501404 |
28 | NPC::~NPC() |
29 | { |
30 | }; |
31 | |
32 | std::string NPC::talk(int worldconditions[]) |
33 | { |
34 | int raylength = sizeof( worldconditions )/sizeof( worldconditions[0] ); |
35 | bool done = false; |
36 | textprintf_ex(screen, font, 150, 200, makecol(255, 255, 255), -1, "here: %d", 1); |
37 | textprintf_ex(screen, font, 150, 210, makecol(255, 255, 255), -1, "worldconditions: %d", worldconditions[0]); |
38 | textprintf_ex(screen, font, 150, 220, makecol(255, 255, 255), -1, "conditions: %d", conditions[0][0]); |
39 | textprintf_ex(screen, font, 150, 230, makecol(255, 255, 255), -1, "conditions: %d", conditions[0][1]); |
40 | |
41 | for(int i=NumOfConditions;i>0 && !done;i--) |
42 | { |
43 | textprintf_ex(screen, font, 150, 180, makecol(255, 255, 255), -1, "here: %d", 2); |
44 | for(int j=0;j<raylength && !done;j++) |
45 | { |
46 | textprintf_ex(screen, font, 150, 190, makecol(255, 255, 255), -1, "here: %d", 3); |
47 | textprintf_ex(screen, font, 150, 240, makecol(255, 255, 255), -1, "worldconditions: %d", worldconditions[j]); |
48 | if(worldconditions[j] == conditions<i>[0]) |
49 | { |
50 | done = true; |
51 | textprintf_ex(screen, font, 150, 150, makecol(255, 255, 255), -1, "here: %d", 1); |
52 | textprintf_ex(screen, font, 150, 150, makecol(255, 255, 255), -1, "test: %s", dialogs->at(conditions<i>[1]).c_str()); |
53 | textprintf_ex(screen, font, 150, 150, makecol(255, 255, 255), -1, "here: %d", 2); |
54 | return dialogs->at(conditions<i>[1]); |
55 | } |
56 | } |
57 | } |
58 | string dialog(""); |
59 | return dialog; |
60 | }; |
]]>
I don't have much time at the mo but I suspect the value of 'raylength' isn't quite right. You might be trying to look at an invalid index in you 'conditions' array. Make the program print out the values of i & j before the 'if' statement and check the last values before the program crashes.
Matt.
]]>ok, after a few little ajustments the crash point has moved to this line
textprintf_ex(screen, font, 150, 160, makecol(255, 255, 255), -1, "test: %s", dialogs->at(conditions[0][1]).c_str());
(i changed the i to a 0 to make sure that wasnt the problem but it still dies)
EDIT:: i found my problem i was int the for loop i was going from the max value of the array and going down through the array (if it had one member i was starting at 1) but if there was only one member (like in this case) the element would be at location 0 not 1!
]]>