allocating dynamic memory

Discussion in 'C++' started by Andreas Lassmann, Jan 21, 2005.

  1. 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?
    Andreas Lassmann, Jan 21, 2005
    #1
    1. Advertising

  2. Andreas Lassmann

    Howard Guest

    "Andreas Lassmann" <> wrote in message
    news:p...
    > 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
    Howard, Jan 21, 2005
    #2
    1. Advertising

  3. Howard wrote:
    > "Andreas Lassmann" <> wrote in message
    > news:p...
    >
    >>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
    Victor Bazarov, Jan 21, 2005
    #3
  4. Andreas Lassmann

    red floyd Guest

    Howard wrote:
    > "Andreas Lassmann" <> wrote in message
    > news:p...
    >
    >>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.
    red floyd, Jan 21, 2005
    #4
  5. Andreas Lassmann

    Howard Guest

    [OT] Re: allocating dynamic memory

    "Victor Bazarov" <> wrote in message
    news:OVcId.37599$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
    Howard, Jan 21, 2005
    #5
  6. Andreas Lassmann

    Howard Guest

    "red floyd" <> wrote in message
    news:h0dId.13418$...

    >>
    >>>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
    Howard, Jan 21, 2005
    #6
  7. Andreas Lassmann

    Mike Wahler Guest

    "red floyd" <> wrote in message
    news:h0dId.13418$...
    > Howard wrote:
    > > "Andreas Lassmann" <> wrote in message
    > > news:p...
    > >
    > >>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
    Mike Wahler, Jan 21, 2005
    #7
  8. Andreas Lassmann

    axter Guest

    axter, Feb 4, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hall
    Replies:
    4
    Views:
    431
  2. soni29
    Replies:
    6
    Views:
    396
    Kevin Goodsell
    Sep 5, 2003
  3. fivelitermustang
    Replies:
    11
    Views:
    796
    John Harrison
    May 23, 2004
  4. filox

    dynamic memory allocating

    filox, Apr 24, 2006, in forum: C++
    Replies:
    2
    Views:
    315
    filox
    Apr 24, 2006
  5. Rakesh Kumar
    Replies:
    5
    Views:
    671
    James Kanze
    Dec 21, 2007
Loading...

Share This Page