2dimentional arrays

Started by
5 comments, last by setaglib 18 years, 10 months ago
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];
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
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]).
is there any other ways of creating explicit arrays?

triangle shaped arrays?? what do u mean?

-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
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-timeconst std::size_t N = ...; // is constant, known at compile-timefoo f[M][N];


partially static array, one dimension is constant:

std::size_t M; // not constant, known at run-timeconst 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>   // coutint 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]
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 :)
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
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.
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

This topic is closed to new replies.

Advertisement