Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   allocating dynamic memory (http://www.velocityreviews.com/forums/t288333-allocating-dynamic-memory.html)

Andreas Lassmann 01-21-2005 07:31 PM

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?

Howard 01-21-2005 07:36 PM

Re: allocating dynamic memory
 

"Andreas Lassmann" <bumblebee_giz@hotmail.com> wrote in message
news:pan.2005.01.21.19.31.57.998668@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






Victor Bazarov 01-21-2005 07:41 PM

Re: allocating dynamic memory
 
Howard wrote:
> "Andreas Lassmann" <bumblebee_giz@hotmail.com> wrote in message
> news:pan.2005.01.21.19.31.57.998668@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

red floyd 01-21-2005 07:48 PM

Re: allocating dynamic memory
 
Howard wrote:
> "Andreas Lassmann" <bumblebee_giz@hotmail.com> wrote in message
> news:pan.2005.01.21.19.31.57.998668@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.

Howard 01-21-2005 07:51 PM

[OT] Re: allocating dynamic memory
 

"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:OVcId.37599$NC6.1387@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



Howard 01-21-2005 07:58 PM

Re: allocating dynamic memory
 

"red floyd" <no.spam@here.dude> wrote in message
news:h0dId.13418$wZ2.4291@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



Mike Wahler 01-21-2005 07:59 PM

Re: allocating dynamic memory
 

"red floyd" <no.spam@here.dude> wrote in message
news:h0dId.13418$wZ2.4291@newssvr13.news.prodigy.c om...
> Howard wrote:
> > "Andreas Lassmann" <bumblebee_giz@hotmail.com> wrote in message
> > news:pan.2005.01.21.19.31.57.998668@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



axter 02-04-2005 01:59 AM

Re: allocating dynamic memory
 
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



All times are GMT. The time now is 02:32 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.