memory allocation for two dimensional array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Hanley

    #1

    memory allocation for two dimensional array

    I need to allocate memory dynamically for a two dimensional array. It's
    basically a pointer to an array of 2 element arrays: I declared it as long
    int ** arr. The number of two element arrays are unknown at compile time.

    I am not quite sure if I allocated memory properly for this (I am sure I
    didn't).

    arr = malloc((sizeof( long int)*2)*number_ of_elements)

    when I debug it, I have access to arr[0][0], arr[1][0], arr[2][0], etc...
    but not arr[0][1]. It tells me it can't access memory which leads me to
    believe I don't quite have my memory allocation and/or declaration correct.

    I have tried a few different ways, but I obviously haven't found the right
    way to do it yet.

    Any suggestions?

    Thanks a bunch!

    John


  • Ravi Uday

    #2
    Re: memory allocation for two dimensional array


    "John Hanley" <jdhanley@telus planet.net> wrote in message
    news:1091416554 .601966@proxy2. srv.ualberta.ca ...[color=blue]
    > I need to allocate memory dynamically for a two dimensional array. It's
    > basically a pointer to an array of 2 element arrays: I declared it as[/color]
    long[color=blue]
    > int ** arr. The number of two element arrays are unknown at compile time.
    >
    > I am not quite sure if I allocated memory properly for this (I am sure I
    > didn't).
    >
    > arr = malloc((sizeof( long int)*2)*number_ of_elements)
    >[/color]
    Have you tried looking into C-FAQ section.
    Allocating 2D arrays are nicely discussed there

    - Ravi


    Comment

    • Emmanuel Delahaye

      #3
      Re: memory allocation for two dimensional array

      John Hanley wrote on 02/08/04 :[color=blue]
      > I need to allocate memory dynamically for a two dimensional array.[/color]


      It's a FAQ.

      --
      Emmanuel
      The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

      "C is a sharp tool"

      Comment

      Working...