473,396 Members | 1,966 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,396 software developers and data experts.

dynamic multidimensional array allocation using one(and only one) 'new' call

I have an old set of library,where I used malloc to allocate
multidimensional(2 and 3) arrays,using only one malloc call(using void
pointers).I get the size of the array in run time.Now that I am moving
to C++,I wish to know if it is possible to allocate a
multi-dimensional(say 2 and 3) with just one (and only one) call to
'new'operator.The idea behind this is to have contiguous memory
locations for array data.Thanks in advance,for all your help.

Ram
Jul 22 '05 #1
1 6565
wrote in news:13************************@posting.google.com in
comp.lang.c++:
I have an old set of library,where I used malloc to allocate
multidimensional(2 and 3) arrays,using only one malloc call(using void
pointers).I get the size of the array in run time.Now that I am moving
to C++,I wish to know if it is possible to allocate a
multi-dimensional(say 2 and 3) with just one (and only one) call to
'new'operator.The idea behind this is to have contiguous memory
locations for array data.Thanks in advance,for all your help.

Yes, but always prefer using a standard container to calling
new [].

#include <iostream>
#include <ostream>
#include <iomanip>
#include <vector>

template < typename T >
struct vector2d
{
vector2d(std::size_t n = 0) : m_Order(n), data( n * n ) {}
void set_Order(std::size_t n) { data.resize(n * (m_Order = n)); }

T *operator[](std::size_t off)
{
return &(data[off * m_Order]);
}
T const *operator[](std::size_t off) const
{
return &(data[off * m_Order]);
}

private:

std::size_t m_Order;
std::vector< T > data;
};
int main()
{
using namespace std;

vector2d< int > a(10);
int i, j;

for (i = 0; i < 10; ++i)
{
for (j = 0; j < 10; ++j)
{
a[i][j] = (i + 1) * (j + 1);
}
}

for (i = 0; i < 10; ++i)
{
for (j = 0; j < 10; ++j)
{
cout << setw(4) << a[i][j];
}
cout << endl;
}
}
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

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

Similar topics

47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
53
by: Tomás | last post by:
Some programmers treat arrays just like pointers (and some even think that they're exactly equivalent). I'm going to demonstrate the differences. Firstly, let's assume that we're working on a...
1
by: Daniel Smith | last post by:
Hi, Two things confused me and I hope somebody can point out the wrong part, thanks in advance char * mytest ( const char src) { char *ret; ret = new char; strncpy(ret, src, 5);
31
by: Markus Pitha | last post by:
Hello, I'm using a template to simulate a LinkedList from Java.It works without problems, but when I want to use strings in main.cpp instead of char*, I get the following error message: $...
9
by: anon.asdf | last post by:
In terms of efficieny: Is it better to use multiple putchar()'s after one another as one gets to new char's OR is it better to collect the characters to a char-array first, and then use...
4
by: Urs Thuermann | last post by:
I have some old code I've written several years ago that doesn't compile with newer versions of GCC. The code allocates an array of objects that need to be initialized by calling a constructor...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.