Connecting Tech Pros Worldwide Help | Site Map

alloc 2d array with new

  #1  
Old July 23rd, 2005, 02:58 AM
aurgathor
Guest
 
Posts: n/a
I'm trying to allocate a 2d array in runtime
that can subsequently be accessed like
arr[x][y], to no avail. Is there any to
do that in C++? If yes, how?

TIA

This is what I got thus far:

#include <iostream>

struct sq_T {
short X;
short Y;
int ID;
};

void maker ( int x, int y ) {
sq_T *display = new sq_T[x*y];

// test code
for (int j = 0; j < y; j++)
for (int i = 0; i < x; i++)
display[(j * x) + i].ID = i + (j*x);

for (int j = 0; j < y; j++) {
for (int i = 0; i < x; i++) {
cout << display[(j * x) + i].ID << " ";
}
cout << endl;
}
}


void main () {
maker(5,7);
}


  #2  
Old July 23rd, 2005, 02:58 AM
Victor Bazarov
Guest
 
Posts: n/a

re: alloc 2d array with new


"aurgathor" <spam-me-if@you.com> wrote...[color=blue]
> I'm trying to allocate a 2d array in runtime
> that can subsequently be accessed like
> arr[x][y], to no avail. Is there any to
> do that in C++? If yes, how?
> [..][/color]

This is covered in the FAQ (http://www.parashift.com/c++-faq-lite/)


  #3  
Old July 23rd, 2005, 02:58 AM
Michael Mair
Guest
 
Posts: n/a

re: alloc 2d array with new


aurgathor wrote:[color=blue]
> I'm trying to allocate a 2d array in runtime
> that can subsequently be accessed like
> arr[x][y], to no avail. Is there any to
> do that in C++? If yes, how?[/color]

Start at
http://www.parashift.com/c++-faq-lit...html#faq-16.16
and read also the next couple of FAQs.

-Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
  #4  
Old July 23rd, 2005, 02:58 AM
aurgathor
Guest
 
Posts: n/a

re: alloc 2d array with new



"Michael Mair" <Michael.Mair@invalid.invalid> wrote in message
news:39he66F62f81qU1@individual.net...[color=blue]
>
> Start at
> http://www.parashift.com/c++-faq-lit...html#faq-16.16
> and read also the next couple of FAQs.
>[/color]

Thanks, I guess it would've taken a little time to figure all that out.


  #5  
Old July 23rd, 2005, 02:59 AM
Axter
Guest
 
Posts: n/a

re: alloc 2d array with new



aurgathor wrote:[color=blue]
> I'm trying to allocate a 2d array in runtime
> that can subsequently be accessed like
> arr[x][y], to no avail. Is there any to
> do that in C++? If yes, how?
>
> TIA
>
> This is what I got thus far:
>
> #include <iostream>
>
> struct sq_T {
> short X;
> short Y;
> int ID;
> };
>
> void maker ( int x, int y ) {
> sq_T *display = new sq_T[x*y];
>
> // test code
> for (int j = 0; j < y; j++)
> for (int i = 0; i < x; i++)
> display[(j * x) + i].ID = i + (j*x);
>
> for (int j = 0; j < y; j++) {
> for (int i = 0; i < x; i++) {
> cout << display[(j * x) + i].ID << " ";
> }
> cout << endl;
> }
> }
>
>
> void main () {
> maker(5,7);
> }[/color]


Check out the following link:
http://www.tek-tips.com/faqs.cfm?fid=5575

It has several more efficient methods for creating 2D array.

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pinning Pointers with __pin in Managed C++ Hexar Anderson answers 3 November 17th, 2005 05:01 PM
Using GCHandle with Enums Hexar Anderson answers 1 November 17th, 2005 04:45 AM
Need grayscale image class help. QuasiChameleon answers 6 August 31st, 2005 10:15 PM