Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > alloc 2d array with new

Reply
Thread Tools

alloc 2d array with new

 
 
aurgathor
Guest
Posts: n/a
 
      03-12-2005
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);
}


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-12-2005
"aurgathor" <(E-Mail Removed)> wrote...
> 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?
> [..]


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


 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      03-13-2005
aurgathor wrote:
> 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?


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.
 
Reply With Quote
 
aurgathor
Guest
Posts: n/a
 
      03-13-2005

"Michael Mair" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> Start at
> http://www.parashift.com/c++-faq-lit...html#faq-16.16
> and read also the next couple of FAQs.
>


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


 
Reply With Quote
 
Axter
Guest
Posts: n/a
 
      03-13-2005

aurgathor wrote:
> 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);
> }



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

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
new vs *alloc (Was: difference between calloc() and malloc()) Tristan Wibberley C++ 3 02-14-2008 06:49 PM
Heap Alloc Exception Problem Adam Hartshorne C++ 6 06-02-2005 08:27 AM
??alloc(0) - std interpretation sNOiSPAMt@amu.edu.pl C Programming 7 05-18-2004 10:54 PM
How to increase the memory alloc for aspnet_wp.exe? Bob ASP .Net 1 02-13-2004 04:25 AM
Memory Alloc/Dealloc query B. Gandhi C Programming 6 09-25-2003 04:22 AM



Advertisments