Computer Science Canada

2D array problem.

Author:  liangchenen [ Mon May 02, 2005 5:14 pm ]
Post subject:  2D array problem.

on the stage 2 last problem. I did something like this

int main()
{
int grid[1000][1000];
cout<<"hi";
return 0;
}

then, after I compiled the program, the program didn't output the "hi", i was wondering why, then I made the variables global, then it worked. can anybody explain why it works/ doesn't work in detail?

Author:  wtd [ Mon May 02, 2005 6:57 pm ]
Post subject:  Re: 2D array problem.

liangchenen wrote:
on the stage 2 last problem. I did something like this

int main()
{
int grid[1000][1000];
cout<<"hi";
return 0;
}

then, after I compiled the program, the program didn't output the "hi", i was wondering why, then I made the variables global, then it worked. can anybody explain why it works/ doesn't work in detail?


Well, keep in mind that we when you allocate space for a 1,000 x 1,000 array, you've allocated somewhere between 4 and 8 MB of memory. On the heap (regular memory), this wouldn't be too horrible.

It is, however, a lot of memory to allocate on the stack, which is where this allocation is taking place.

Author:  Andy [ Tue May 03, 2005 6:20 pm ]
Post subject: 

three is a limit on how big an array is.. i had this problem during the ccc this year, turns out the max size was like [512][512] or something like it

Author:  liangchenen [ Wed May 04, 2005 12:19 pm ]
Post subject:  solutions?

How could I solve this problem?
do I just make the variables global?
or do I just make dynamic arrays like
int *array = new int [100];
but how could I do this for 2D arrays?

Author:  wtd [ Wed May 04, 2005 12:33 pm ]
Post subject: 

c++:
int *arr = new int[100][100];
for (int i(0); i < 100; ++i)
{
   arr[i] = new int[100];
}


But if you're looking to create a matrix, I suggest creating a class which wraps an array.

c++:
template <typename _t, size_t _r, size_t _c>
class matrix
{
   private:
      _t *data;
   public:
      matrix() : data(new _t[_r * _c]) { }
      ~matrix() { delete [] data; }

      size_t rows() const { return _r; }
      size_t cols() const { return _c; }

      _t operator()(size_t r, size_t c) const { return data[r * c]; }
      _t& operator()(size_t r, size_t c) { return data[r * c]; }
};

Author:  Viper [ Tue Sep 20, 2005 12:54 pm ]
Post subject: 

in an array how would you start at a number other than 0 eg
turing>> var f:array 30..100 of int

what would tht b in C++?? (i know i asked this be but i couldent find it again)

Author:  wtd [ Tue Sep 20, 2005 2:21 pm ]
Post subject: 

Viper wrote:
in an array how would you start at a number other than 0 eg
turing>> var f:array 30..100 of int

what would tht b in C++?? (i know i asked this be but i couldent find it again)


You don't. Arrays in C and C++ and many other languages (the only exceptions I can think of off the top of m head are Ada and Eiffel) always start with an index of zero.

Author:  Andy [ Thu Sep 22, 2005 1:59 pm ]
Post subject: 

y not just subtract 30 everytime u access it? it's the samething


: