allocating dynamic memory

A

Andreas Lassmann

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?
 
H

Howard

Andreas Lassmann said:
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 = new char[mapX];

and later,

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

-Howard
 
V

Victor Bazarov

Howard said:
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
 
R

red floyd

Howard said:
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 = new char[mapX];

and later,

for (int i = 0; i < mapY; ++i)
delete [] pMap;
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 = 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.
 
H

Howard

Victor Bazarov said:
Howard said:
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
 
H

Howard

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 = new char[mapX];

and later,

for (int i = 0; i < mapY; ++i)
delete [] pMap;
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 = 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
 
M

Mike Wahler

red floyd said:
Howard said:
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 = 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top