Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic array problem 1

Status
Not open for further replies.

McBugzz

Programmer
Joined
Sep 17, 2002
Messages
90
Location
JP
I need to read some file into a 2d array. The filesize is unknown, but it can vary from bytes to kylobytes, or more. The actual problem is how to initialize a dynamic 2d array. If I write smth like

float* map;

, then I can't write a function that returns map[j] etc.

The program is planned to be smth like this:

class Map
{
public:
//...

private:
float* map; // or some other way of initialising
};

...

Map::Map( void )
{
// the needed sise of the array is unknown yet

// reading the file, calculating it's size

// map = new something - allocating memory

// using map[j] element
}

I know this is a stoopid question, but I can't figure out how it's done.
 
2nd array sounds like pointer to pointer:
Code:
float** x = new float[COLOR=red]*[/color][numOfArrays];
for(i = 0; i < numOfArrays; i++)
{
    x[i] = new float[numberOfElementsInArray_i];
}
the above operations you will incapsulate in your class Map. By the way, there is an STL map(std::map) what you could use.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top