473,396 Members | 1,671 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.

allocating dynamic memory

hi there, i've got a problem:
can i create a dynamic array like this?

pMap = new char[mapX][mapY];

gcc (my compiler) sais, it's wrong...
i know that dynamic memory is more often used in this way:

pMap = new char[mapX];

but i need a second element...
can somebody help?
Jul 22 '05 #1
7 1553

"Andreas Lassmann" <bu***********@hotmail.com> wrote in message
news:pa****************************@hotmail.com...
hi there, i've got a problem:
can i create a dynamic array like this?

pMap = new char[mapX][mapY];

gcc (my compiler) sais, it's wrong...
i know that dynamic memory is more often used in this way:

pMap = new char[mapX];

but i need a second element...
can somebody help?


One way is to do this:

char** pMap = new char*[mapY]; // does char* need () around it?
for (int i = 0; i < mapY; ++i)
pMap[i] = new char[mapX];

and later,

for (int i = 0; i < mapY; ++i)
delete [] pMap[i];
delete [] pMap;

-Howard

Jul 22 '05 #2
Howard wrote:
"Andreas Lassmann" <bu***********@hotmail.com> wrote in message
news:pa****************************@hotmail.com...
hi there, i've got a problem:
can i create a dynamic array like this?

pMap = new char[mapX][mapY];

gcc (my compiler) sais, it's wrong...
i know that dynamic memory is more often used in this way:

pMap = new char[mapX];

but i need a second element...
can somebody help?

One way is to do this:
[...]


Two FAQs are related to this thread, I believe: #5.5 and #16.15.

V
Jul 22 '05 #3
Howard wrote:
"Andreas Lassmann" <bu***********@hotmail.com> wrote in message
news:pa****************************@hotmail.com...
hi there, i've got a problem:
can i create a dynamic array like this?

pMap = new char[mapX][mapY];

gcc (my compiler) sais, it's wrong...
i know that dynamic memory is more often used in this way:

pMap = new char[mapX];

but i need a second element...
can somebody help?

One way is to do this:

char** pMap = new char*[mapY]; // does char* need () around it?
for (int i = 0; i < mapY; ++i)
pMap[i] = new char[mapX];

and later,

for (int i = 0; i < mapY; ++i)
delete [] pMap[i];
delete [] pMap;

-Howard


But your memory isn't contiguous.

I'd allocate th rows as a big block:

char *pMapData = new char[mapX * mapY]
char **pMap = new char[mapY];
for (int = 0 ; i < mapY; ++i)
pMap[i] = pMapData + (i * mapX);

Then you only have 2 delete[] calls, and the memory for the 2D array is
contiguous.
Or possibly, you could use a pointer to an array.
Jul 22 '05 #4

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:OV******************@newsread1.mlpsca01.us.to .verio.net...
Howard wrote:


One way is to do this:
[...]


Two FAQs are related to this thread, I believe: #5.5 and #16.15.


Okay, okay, I know... I answered a question that's already in the FAQ (and
didn't answer it as well or as completely either, I might add). But just to
get my own stab back at you, Victor, this is from #5.5:
Note #1: Please don't give them the location of the appropriate FAQ. E.g.,
don't say, "Look at FAQ [10.3]" or "Look in section [10]". It's the old
give-them-a-fish vs. teach-them-to-fish problem.
Neener neener! :-) (Sorry, I just couldn't resist)

-Howard
Jul 22 '05 #5

"red floyd" <no*****@here.dude> wrote in message
news:h0******************@newssvr13.news.prodigy.c om...
can i create a dynamic array like this?

pMap = new char[mapX][mapY];

One way is to do this:

char** pMap = new char*[mapY]; // does char* need () around it?
for (int i = 0; i < mapY; ++i)
pMap[i] = new char[mapX];

and later,

for (int i = 0; i < mapY; ++i)
delete [] pMap[i];
delete [] pMap;

-Howard

But your memory isn't contiguous.


Nope. But that wasn't a specified requirement. I was just giving one way
to do it, not neccessarily the best.

I'd allocate th rows as a big block:

char *pMapData = new char[mapX * mapY]
char **pMap = new char[mapY];
for (int = 0 ; i < mapY; ++i)
pMap[i] = pMapData + (i * mapX);

Then you only have 2 delete[] calls, and the memory for the 2D array is
contiguous.

Yep, something like that's probably better.

Or possibly, you could use a pointer to an array.


Or a vector!

Anyway, the FAQ explains the whole thing quite well. And, for the OP,
that's available at:

http://www.parashift.com/c++-faq-lite/

-Howard
Jul 22 '05 #6

"red floyd" <no*****@here.dude> wrote in message
news:h0******************@newssvr13.news.prodigy.c om...
Howard wrote:
"Andreas Lassmann" <bu***********@hotmail.com> wrote in message
news:pa****************************@hotmail.com...
hi there, i've got a problem:
can i create a dynamic array like this?

pMap = new char[mapX][mapY];

[snip]
I'd allocate th rows as a big block:

char *pMapData = new char[mapX * mapY]
char **pMap = new char[mapY];
for (int = 0 ; i < mapY; ++i)
pMap[i] = pMapData + (i * mapX);

Then you only have 2 delete[] calls, and the memory for the 2D array is
contiguous.
Or possibly, you could use a pointer to an array.


I think better than what everyone else advised so far,
would be to use vector<vector<char> > or perhaps
vector<string>

Don't muck around with raw pointers unless you must.

$.02,
-Mike
Jul 22 '05 #7
Check out the following link for good examples and multiple method for
creating a dynamic 2D array.

http://www.tek-tips.com/faqs.cfm?fid=5575

Jul 23 '05 #8

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

Similar topics

6
by: soni29 | last post by:
hi, i'm reading a c++ book and noticed that the author seems to allocate memory differently when using classes, he writes: (assuming a class called CBox exists, with member function size()): //...
10
by: Jakob Bieling | last post by:
Hi, Whenever allocating memory using operator new or operator new I feel like I should only use it very sparingly, because otherwise memory is wasted (by additional overhead needed to manage all...
11
by: fivelitermustang | last post by:
Actually, how would I go about allocating a four-dimensional dynamic array? I only know how to make two dimensional dynamic arrays: double **v; v = new double*; for (int i=0; i<C; i++) { v =...
4
by: Sameer | last post by:
Hello Group, This is one problem in programming that is troubling me. there is a segmentation fault just before creating memory to a structure ..i.e, just after the "allocating memory "...
15
by: fix | last post by:
Hi all, I am writing a program using some structs, it is not running and I believe it is because there's some memory leak - the debugger tells me that the code causes the problem is in the malloc...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
3
by: Amit_Basnak | last post by:
Dear Friends I have the follwoing function "tss_fe_get_own_info" which has the arguments as shows below tss_fe_get_own_info(char *user_id, tss_user_profile_t **p_buf_UserSecInfo, error_status_t...
12
by: filia&sofia | last post by:
For example I would like to dynamically allocate memory for linked list (or complex struct etc.) that has not maximum number of elements. All that I can remember is that I have to have allocated...
6
by: arashmath | last post by:
Hi everyone, I have a problem with large dynamic memory allocating. this is my code (in briefness): #include <stdio.h> #include <mem.h> #include <assert.h> #define SML_BOUNDS_CHECK
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.