|
odd values with multidimensional arrays |
adamk kromm
Member #5,432
January 2005
|
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? ---------- |
Onewing
Member #6,152
August 2005
|
How are you defining conditions? ------------ |
adamk kromm
Member #5,432
January 2005
|
ummm, if your refering to ALWAYS then like this #define ALWAYS 0 ---------- |
Onewing
Member #6,152
August 2005
|
No, I'm talking about your multi-dimensional array called "conditions". ------------ |
Kauhiz
Member #4,798
July 2004
|
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. --- |
HoHo
Member #4,534
April 2004
|
Do you have something like this somewhere in your code?int **conditions;If no then how do you define/declare the contitions variable? __________ |
adamk kromm
Member #5,432
January 2005
|
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. ---------- |
TeamTerradactyl
Member #7,733
September 2006
|
int* conditions[];
|
ImLeftFooted
Member #3,935
October 2003
|
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; 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] Quote: 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. |
HoHo
Member #4,534
April 2004
|
Do you have conditions = new int[NumOfConditions];in your code? If you are having random values then I would guess you don't have. __________ |
Matt Weir
Member #7,476
July 2006
|
Hope this helps! I remember being stuck on this once....
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'... |
adamk kromm
Member #5,432
January 2005
|
EDIT: ---------- |
Matt Weir
Member #7,476
July 2006
|
Edit: |
adamk kromm
Member #5,432
January 2005
|
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)
---------- |
Matt Weir
Member #7,476
July 2006
|
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. |
adamk kromm
Member #5,432
January 2005
|
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! ---------- |
|