Allegro.cc Forums » Programming Questions » odd values with multidimensional arrays

 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 = new int[2]; } conditions[0][0] = ALWAYS; conditions[0][1] = 0; ALWAYS is defined as 0then when i got to retrieve the valuetextprintf_ex(screen, font, 150, 220, makecol(255, 255, 255), -1, "conditions: %d", conditions[0][0]); i get values like 4042624any ideas what im doing wrong?edit:: i figured out that its giving me the memory address... so how do i get the value instead? -----------Adam Kromm
 Onewing Member #6,152 August 2005 How are you defining conditions? ------------Solo-Games.org | My Tech Blog: The Digital Helm
 adamk kromm Member #5,432 January 2005 ummm, if your refering to ALWAYS then like this#define ALWAYS 0 -----------Adam Kromm
 Onewing Member #6,152 August 2005 No, I'm talking about your multi-dimensional array called "conditions". ------------Solo-Games.org | My Tech Blog: The Digital Helm
 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 = 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. ---It's Ridge Racer! RIIIIIDGE RAAAAACER!
 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? __________In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de SnepscheutMMORPG's...Many Men Online Role Playing Girls - Radagar"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski
 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. -----------Adam Kromm
 TeamTerradactyl Member #7,733 September 2006 int* conditions[];should be the same as int **conditions;I don't know why your code isn't working, though
 Dustin Dettmer Member #3,935 October 2003 for(int i=0;i = new int[2]; } conditions[0][0] = ALWAYS; conditions[0][1] = 0; Should be: for(int i=0;i = new int[2]; conditions[0] = ALWAYS; conditions[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. 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. __________In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de SnepscheutMMORPG's...Many Men Online Role Playing Girls - Radagar"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski
Matt Weir
Member #7,476
July 2006

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 = 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; 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'...

 Matt Weir Member #7,476 July 2006 Edit:
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)

 1 #include 2 #include 3 #include 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; 15 conditions = new int*[NumOfConditions]; 16 for(int i=0;ipush_back(new int[2]); 19 conditions = new int[2]; 20 } 21 dialogs = new vector; 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[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[1]).c_str()); 53 textprintf_ex(screen, font, 150, 150, makecol(255, 255, 255), -1, "here: %d", 2); 54 return dialogs->at(conditions[1]); 55 } 56 } 57 } 58 string dialog(""); 59 return dialog; 60 };

----------