473,394 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

alloc 2d array with new

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);
}
Jul 23 '05 #1
4 3679
"aurgathor" <sp********@you.com> 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/)
Jul 23 '05 #2
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.
Jul 23 '05 #3

"Michael Mair" <Mi**********@invalid.invalid> wrote in message
news:39*************@individual.net...

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.
Jul 23 '05 #4

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.

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: sNOiSPAMt | last post by:
7.20.3#1 If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value,...
6
by: Vulcan Fire | last post by:
Hi Why is that int *p = 12; core dumps & not char *p = "some_str";
10
by: Ryan Wang | last post by:
Hi, here is the function: int sum(int a,int b) { int c; c = a + b; return c; }
3
by: -DG- | last post by:
I'm still trying to figure out some of the nuances of access to legacy Win32 DLLs. I need to alloc buffers to be used by the Win32 DLLs. I know that pinning a managed pointer can lead to...
5
by: Janning Vygen | last post by:
Hi, tonight my database got corruppted. before it worked fine. since two days i do the following tasks every night psql -c 'CLUSTER;' $DBNAME psql -c 'VACUUM FULL ANALYZE;' $DBNAME ...
1
by: divya_rathore_ | last post by:
I have a problem regarding template classes. I am trying to write a template class which allocates dynamic memory for 2D/3D arrays. Consider a template class to allocate a 2D array: //DynAlloc.h...
17
by: Joe Smith | last post by:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #define BUF_MAX 80 int main (void) { char buf;
0
by: craig.conboy | last post by:
Using MC++ .Net 2.0, with the CLR Profiler (from Microsoft) I am seeing that 3 blocks of memory, each ~48MB in size, being allocated by some code that initializes a managed array. It is a jagged...
1
by: =?Utf-8?B?SkE=?= | last post by:
I use a method for threading that instantiates an object that is a wrapper to a DLL (written in C). The wrapper class is passed a byte array, and then does GCHandle handle =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.