Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > allocating dynamic memory

Reply
Thread Tools

allocating dynamic memory

 
 
Andreas Lassmann
Guest
Posts: n/a
 
      01-21-2005
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?
 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      01-21-2005

"Andreas Lassmann" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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





 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-21-2005
Howard wrote:
> "Andreas Lassmann" <(E-Mail Removed)> wrote in message
> news(E-Mail Removed)...
>
>>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
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      01-21-2005
Howard wrote:
> "Andreas Lassmann" <(E-Mail Removed)> wrote in message
> news(E-Mail Removed)...
>
>>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.
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      01-21-2005

"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:OVcId.37599$(E-Mail Removed)01.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


 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      01-21-2005

"red floyd" <(E-Mail Removed)> wrote in message
news:h0dId.13418$(E-Mail Removed) 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


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-21-2005

"red floyd" <(E-Mail Removed)> wrote in message
news:h0dId.13418$(E-Mail Removed) om...
> Howard wrote:
> > "Andreas Lassmann" <(E-Mail Removed)> wrote in message
> > news(E-Mail Removed)...
> >
> >>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


 
Reply With Quote
 
axter
Guest
Posts: n/a
 
      02-04-2005
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

 
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
Allocating vector of strings seem to crash. Allocating array ofstrings seems to be ok . Rakesh Kumar C++ 5 12-21-2007 10:42 AM
dynamic memory allocating filox C++ 2 04-24-2006 04:58 PM
Allocating Four Dimensional Dynamic Arrays... fivelitermustang C++ 11 05-23-2004 06:08 AM
basic allocating memory question soni29 C++ 6 09-05-2003 05:45 PM
Pushing memory allocating objects into a vector. hall C++ 4 08-20-2003 09:24 PM



Advertisments