Jump to content
  • Advertisement
Sign in to follow this  
BloodLust666

2dimentional arrays

This topic is 4829 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

How do i explicitly create a 2 dimentional array? would i create a pointer to a pointer and get it from that? i.e. char** Data; Data = new char*[8]; char i; for (i=1 ; i<8 ; i++) Data = new char[12];

Share this post


Link to post
Share on other sites
Advertisement
Yes, you can do it that way. You can even create triangle shaped (or irregular) 2d arrays. You can then access elements as in normal array (that is with Data[5][3]).

Share this post


Link to post
Share on other sites
Quote:
Original post by EvilKnuckles666
How do i explicitly create a 2 dimentional array?


Need abit more info, are both dimensions constant?, is one dimension constant? are both dimensions variable?

fully static array, both dimensions are constant:


const std::size_t M = ...; // is constant, known at compile-time
const std::size_t N = ...; // is constant, known at compile-time

foo f[M][N];


partially static array, one dimension is constant:


std::size_t M; // not constant, known at run-time
const std::size_t N = ...; // is constant, known at compile-time

// ....

foo (*f)[N] = new foo[M][N];
// ...
delete[] f;


fully dynamic array, all dimensions are variable:


foo** f = ? // all will be revealed below.
// .... ?


Quote:
Original post by EvilKnuckles666
would i create a pointer to a pointer and get it from that?

i.e.
char** Data;
Data = new char*[8];

char i;
for (i=1 ; i<8 ; i++)
Data = new char[12];



Yes you can do that if you want to, the problem is elements are not stored contiguously, thus not as efficient as it could be.

Here is an alternative method that allow elements to be stored contiguously but still viewed as 2d dyanmic array:


#include <cstddef> // size_t
#include <algorithm> // generate_n
#include <iterator> // raw_storage_iterator
#include <cmath> // rand
#include <new> // placement new
#include <iostream> // cout

int main() {

std::size_t M = 8, N = 12; // technically not constant, both variable
std::size_t num_elements = M * N;

// allocate a bunch of uninitialized pointers
char** array_2d = static_cast<char**>(::operator new(sizeof(char*) * M));

// allocate a bunch of uninitialized elements
char* elements = static_cast<char*>(::operator new(sizeof(char) * num_elements));

// initialize 2d array, using placement new operator
for(std::size_t i = 0, j = 0; i < M; ++i, j += N)
::new(array_2d + i) char*(elements + j);

// initialize all elements with random data, raw_storage_iterator uses placement new operator
std::generate_n(std::raw_storage_iterator<char*, char>(elements), num_elements, std::rand);

// now 2d array is set-up
for(std::size_t i = 0; i < M; ++i)
for(std::size_t j = 0; j < N; ++j)
std::cout << static_cast<int>(array_2d[j]) << std::endl;


::operator delete(array_2d); // de-allocate bunch of pointers
::operator delete(elements); // de-allocate bunch of elements
}


[Edited by - snk_kid on May 31, 2005 5:38:40 PM]

Share this post


Link to post
Share on other sites
whoa... all that std:: stuff i have no idea what ur talking about. i don't use std:: at all... not even for my list template, i just created my own :)

Share this post


Link to post
Share on other sites
Quote:
Original post by EvilKnuckles666
whoa... all that std:: stuff i have no idea what ur talking about. i don't use std:: at all... not even for my list template, i just created my own :)


? you talking about std::size_t? size_t is just a type alias (typically for an unsigned integer but is platform dependant), size_t is used in both C and C++ to refer to sizes.

I've haven't used anything else except for the bit to initialize elements with some random data, you can safely ignore that bit.

Share this post


Link to post
Share on other sites
One thing you could do also is just make the array one single dimension and access it like so:


//arrray dimensions
const int sx = 10;
const int sy = 10;

#define pos(x,y,_sx)(x+(_sx*y))

int *array = new int[sx*sy];

array[pos(4,0,sx)] = foo

delete [] array;



Or you could also use vectors


//arrray dimensions
const int sx = 10;
const int sy = 10;

std::vector< std::vector<int> > array;

array.resize( sx );

for(int i = 0;i < sx;++i)
array.resize(sy);

//then you can access it as a regular 2d array
array[4][0] = foo

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!