Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Roof Top Pew Wee

Array with new question.

This topic is 6087 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having some trouble with some array code. Here's the simplified version which I think will point out the problem. WORD* surfacesToSave[84]; for(int i = 0; i < 84; i++) { surfacesToSave[ i] = new WORD [100]; } Here's my first question. Would this create a 2 dimentional array (84 * 100)? Second, what do I get from myArray[0]? I'm guessing the address of the start of the first "row" of WORDS. Third, could I do this: myArray[0][5] to get the 6th WORD as if it were a 2D array? The most important question is the second one. I am saving a file after I create these arrays and the function call is: outfile.write(reinterpret_cast< char* >(surfacesToSave[j]), cornerSurfaceWidth*ddsd.dwWidth*sizeof(WORD)); The first argument in the write is the address of an object to my knowledge. However, I've noticed that when ya don't give the write() function an address it doesn't write anything. Well, my file sizes aren't affected at all whether the writing of the WORD arrays are there or not, so I'm guessing that surfacesToSave[j] is not a valid address. Thanks for any help. --Vic-- Edited by - Roof Top Pew Wee on November 21, 2001 11:53:24 PM Edited by - Roof Top Pew Wee on November 21, 2001 11:54:26 PM

Share this post


Link to post
Share on other sites
Advertisement
Not really, but kind of. It creates an array of pointers. 2D arrays can be implemented that way, if you want though.

A pointer to a block of memory containing 100 WORDS, so yes, the first "row".

Nope, it''s not a 2D array. There''s no guarantee of where in memory each "row" is allocated or what other magic is included in the memory block. It''s not contiguous, which is what myArray[0][5] would assume. It might work (or appear to work) for the first row, but don''t put it to the test. It''s just wrong

--

MP3 Dancer

Get A Stripper on your desktop

Share this post


Link to post
Share on other sites
quote:

WORD* surfacesToSave[84];
for(int i = 0; i < 84; i++)
{
surfacesToSave[ i] = new WORD [100];
}



Nope, that won''t do it. Here''s one of 2 ways to create a dynamic 2d array.

  
WORD **surfacesToSave = new WORD*[84];
for (int i = 0; i < 84; i++)
{
surfacesToSave[i] = new WORD[100];
}


That''s the only way I know how. Apparently there is a huge debate on which way is the correct way to do it though.

I''m learning, just like the best of us...

Share this post


Link to post
Share on other sites
Isn''t that exactly what he did, only that you allocated the array of pointers on the heap whereas he put it on the stack??

Share this post


Link to post
Share on other sites
Oh boy. Soulkeeper, I haven''t tried your code yet, but I have a feeling it will work. I don''t understand the reasoning behind using double pointers like that, but it usually solves the prolbems I have . I will try it and write back. Thanks.

--Vic--

Share this post


Link to post
Share on other sites
Alas, the code doesn't work with the save. The contents of the array isn't being. My file size is still the same size as before.

--Vic--

Edited by - Roof Top Pew Wee on November 22, 2001 12:10:19 PM

Share this post


Link to post
Share on other sites
Red Ant: I tried it Roof Top's way and it gave me a conversion error that I was unable to fix. I didn't take very long trying to get it work though, so I don't know.

Roof Top: First let me answer the other original questions that I didn't get to before.

2. Yup, looks like an address to me.

3. That would give the 6th WORD of the 1st row, yes.

I think I may have an idea about the write problem you are having. I tried it the way you did it, with a much simpler test, and it didn't work quite the way I thought it would. Here's my test:
      
#include <iostream>

#include <fstream>

using namespace std;

int main()
{
int numCols,numRows;

cout << "How big on the X? ";
cin >> numCols;
cout << "\nHow big on the Y? ";
cin >> numRows;

// Create it

int **matrix = new int*[numRows];

for (int i = 0; i < numRows; i++)
{
matrix[i] = new int[numCols];
}

// Zero it

for (i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
matrix[i][j] = 0;
}
}

cout << matrix[0];

cout << "\nCreated successfully!" << endl << endl;

for (i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
cout << matrix[i][j];
}
cout << endl;
}

ofstream fout("test.dat");

for (i = 0; i < numCols; i++)
{
fout.write(reinterpret_cast< char * >(&matrix[0][i]), sizeof(matrix[0][i]));
}


fout.close();

ifstream fin("test.dat");

int **test = new int*[numRows];

for ( i = 0; i < numRows; i++)
{
test[i] = new int[numCols];
}

for (i = 0; i < numCols; i++)
{
fin.read(reinterpret_cast< char * >(&test[0][i]), sizeof(test[0][i]));
}

for (i = 0; i < numCols; i++)
{
cout << test[0][i] << " ";
}
cout << endl;

fin.close();
return 0;
}



Basically, I think you probably will get what you want to do by using a for loop to loop through the columns and save each one individually, loading them the same way.

When I tried it the way you did it, write() only saved the first element of the row.

I'm learning, just like the best of us...


Edited by - Soulkeeper on November 22, 2001 6:36:41 PM

Edited by - Soulkeeper on November 22, 2001 6:37:20 PM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!