dynamic 2 D array with malloc??

Discussion in 'C Programming' started by VijaKhara@gmail.com, Mar 15, 2007.

  1. Guest

    hi all,

    i am trying to create a dynamic 2D array with size N x 3 (N will be
    put in as a parameter) using the following code:

    int **xyz;
    int i,N;

    N=30000;
    xyz=malloc(3*sizeof(int*));
    for (i=0; i<N;i++)
    xyz=malloc(sizeof(int)*N);


    the program ran too slow and finally error appeared when i increased
    to 6288:
    "Unhandled exception in proj1.exe :0xC0000005:Access violation.

    it seems a "run out off memory error" . my pc has 600Mb RAM and around
    1Gb free in harddisk.

    Iam wondering if my array is too large that my pc's RAM can't handle?
    it is amazing.

    plz help??
     
    , Mar 15, 2007
    #1
    1. Advertising

  2. Chris Dollin Guest

    wrote:

    > i am trying to create a dynamic 2D array with size N x 3 (N will be
    > put in as a parameter) using the following code:
    >
    > int **xyz;
    > int i,N;
    >
    > N=30000;
    > xyz=malloc(3*sizeof(int*));
    > for (i=0; i<N;i++)


    Um. Shouldn't that N be 3?

    > xyz=malloc(sizeof(int)*N);


    Yes, it should. BOOM today.

    PS usual remarks about preferred style of mallocation.

    --
    Chris "lucky man" Dollin
    "Anything can happen in the next half-hour." /Stingray/
     
    Chris Dollin, Mar 15, 2007
    #2
    1. Advertising

  3. wrote:

    > i am trying to create a dynamic 2D array with size N x 3 (N will be
    > put in as a parameter) using the following code:
    >
    > int **xyz;
    > int i,N;


    by convention uppercase is reserved for macros.


    > N=30000;
    > xyz=malloc(3*sizeof(int*));


    since 3 is fixed you could do

    int *xyz[3];

    if you use malloc() then test the return value


    > for (i=0; i<N;i++)
    > xyz=malloc(sizeof(int)*N);


    um. You allocate 3 int*s then you try to index 30,000 items in
    this block of memory.

    You meant:

    for (i = 0; i < 3; i++)
    {
    xyz = malloc(sizeof (int) * N);
    if (xyz[0] == NULL)
    handle_error();
    }

    note how judicious use of whitespace imprves readability.


    > the program ran too slow and finally error appeared when i increased
    > to 6288:
    > "Unhandled exception in proj1.exe :0xC0000005:Access violation.
    >
    > it seems a "run out off memory error" . my pc has 600Mb RAM and around
    > 1Gb free in harddisk.
    >
    > Iam wondering if my array is too large that my pc's RAM can't handle?
    > it is amazing.


    just because your PC has 600M doesn't mean the OS will let you have it
    all.

    try this

    p = malloc(3 * N * sizeof(int*));

    if (p == NULL)
    printf ("can't malloc that!!\n);


    --
    Nick Keighley


    - Yes it works in practice - but does it work in theory?
     
    Nick Keighley, Mar 15, 2007
    #3
  4. "Nick Keighley" <> wrote in message
    > wrote:
    >
    >> i am trying to create a dynamic 2D array with size N x 3 (N will be
    >> put in as a parameter) using the following code:
    >>
    >> int **xyz;
    >> int i,N;

    >
    > by convention uppercase is reserved for macros.
    >

    N is an exception. There is a strong convention for using N to hold a count.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Mar 15, 2007
    #4
  5. Malcolm McLean said:

    >
    > "Nick Keighley" <> wrote in message
    >> wrote:
    >>
    >>> i am trying to create a dynamic 2D array with size N x 3 (N will be
    >>> put in as a parameter) using the following code:
    >>>
    >>> int **xyz;
    >>> int i,N;

    >>
    >> by convention uppercase is reserved for macros.
    >>

    > N is an exception. There is a strong convention for using N to hold a
    > count.


    That's the first I've heard of such a convention. When did that happen?

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Mar 15, 2007
    #5
  6. Ian Collins Guest

    Malcolm McLean wrote:
    >
    > "Nick Keighley" <> wrote in message
    >
    >> wrote:
    >>
    >>> i am trying to create a dynamic 2D array with size N x 3 (N will be
    >>> put in as a parameter) using the following code:
    >>>
    >>> int **xyz;
    >>> int i,N;

    >>
    >>
    >> by convention uppercase is reserved for macros.
    >>

    > N is an exception. There is a strong convention for using N to hold a
    > count.
    >

    Where?

    --
    Ian Collins.
     
    Ian Collins, Mar 15, 2007
    #6
  7. Joe Wright Guest

    wrote:
    > hi all,
    >
    > i am trying to create a dynamic 2D array with size N x 3 (N will be
    > put in as a parameter) using the following code:
    >
    > int **xyz;
    > int i,N;
    >
    > N=30000;
    > xyz=malloc(3*sizeof(int*));
    > for (i=0; i<N;i++)
    > xyz=malloc(sizeof(int)*N);
    >
    >
    > the program ran too slow and finally error appeared when i increased
    > to 6288:
    > "Unhandled exception in proj1.exe :0xC0000005:Access violation.
    >
    > it seems a "run out off memory error" . my pc has 600Mb RAM and around
    > 1Gb free in harddisk.
    >
    > Iam wondering if my array is too large that my pc's RAM can't handle?
    > it is amazing.
    >
    > plz help??
    >

    You are not programming the problem. Three rows of 30,000 ints should be
    doable like this..

    int **xyz;
    int i, r = 3, c = 30000;

    xyz = malloc(r * sizeof *xyz);
    for (i = 0; i < r; ++i)
    xyz = malloc(c * sizeof **xyz);

    What do you think?

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Mar 15, 2007
    #7
  8. Ben Pfaff Guest

    "Malcolm McLean" <> writes:

    > "Nick Keighley" <> wrote in message
    >> wrote:
    >>> int i,N;

    >>
    >> by convention uppercase is reserved for macros.
    >>

    > N is an exception. There is a strong convention for using N to hold a count.


    Really? What base of code is it that makes use of this
    convention? I have not encountered it, and I think of myself as
    someone who has read a lot of code.
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
     
    Ben Pfaff, Mar 15, 2007
    #8
  9. Joe Wright said:

    <snip>

    > int **xyz;
    > int i, r = 3, c = 30000;
    >
    > xyz = malloc(r * sizeof *xyz);
    > for (i = 0; i < r; ++i)
    > xyz = malloc(c * sizeof **xyz);
    >
    > What do you think?


    I think xyz could be NULL at the point where you deref it. Why, what do
    /you/ think? :)

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Mar 15, 2007
    #9
  10. In article <>,
    Richard Heathfield <> wrote:

    >>>> int i,N;


    >>> by convention uppercase is reserved for macros.


    >> N is an exception. There is a strong convention for using N to hold a
    >> count.


    >That's the first I've heard of such a convention. When did that happen?


    In my experience it's much more often lowercase. But it raises a good
    point: conventions often conflict. If you're using C to implement
    some published algorithm there's no particular reason why C's
    conventions should trump the algorithm's. You just have to judge
    which will be most convenient for readers and maintainers of the
    code.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Mar 15, 2007
    #10
  11. Old Wolf Guest

    On Mar 16, 10:21 am, "Malcolm McLean" <>
    wrote:
    > "Nick Keighley" <> wrote in message
    >
    > > by convention uppercase is reserved for macros.

    >
    > N is an exception. There is a strong convention for using N to hold a count.


    I've never seen it. I do use lower case 'n' for a count in
    throwaway code, but never upper case.
     
    Old Wolf, Mar 15, 2007
    #11
  12. Flash Gordon Guest

    Richard Tobin wrote, On 15/03/07 22:27:
    > In article <>,
    > Richard Heathfield <> wrote:
    >
    >>>>> int i,N;

    >
    >>>> by convention uppercase is reserved for macros.

    >
    >>> N is an exception. There is a strong convention for using N to hold a
    >>> count.

    >
    >> That's the first I've heard of such a convention. When did that happen?

    >
    > In my experience it's much more often lowercase. But it raises a good
    > point: conventions often conflict. If you're using C to implement
    > some published algorithm there's no particular reason why C's
    > conventions should trump the algorithm's. You just have to judge
    > which will be most convenient for readers and maintainers of the
    > code.


    Unless the convention of the algorithm uses both 'N' and 'n' I would say
    that the case conventions of C should be used. After all, someone
    reading the code who knows the algorithm is unlikely to be confused by a
    change of case, and if they know C as well they are likely to know the C
    conventions as well and so expect it.
    --
    Flash Gordon
     
    Flash Gordon, Mar 15, 2007
    #12
  13. In article <-gordon.me.uk>,
    Flash Gordon <> wrote:

    >> In my experience it's much more often lowercase. But it raises a good
    >> point: conventions often conflict. If you're using C to implement
    >> some published algorithm there's no particular reason why C's
    >> conventions should trump the algorithm's. You just have to judge
    >> which will be most convenient for readers and maintainers of the
    >> code.


    >Unless the convention of the algorithm uses both 'N' and 'n'


    .... that's just the case I was thinking of. But there are also
    conventions like vectors being in capitals where you might want to use
    case to distinguish even when there is no name clash.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Mar 16, 2007
    #13
  14. Flash Gordon Guest

    Richard Tobin wrote, On 16/03/07 01:19:
    > In article <-gordon.me.uk>,
    > Flash Gordon <> wrote:
    >
    >>> In my experience it's much more often lowercase. But it raises a good
    >>> point: conventions often conflict. If you're using C to implement
    >>> some published algorithm there's no particular reason why C's
    >>> conventions should trump the algorithm's. You just have to judge
    >>> which will be most convenient for readers and maintainers of the
    >>> code.

    >
    >> Unless the convention of the algorithm uses both 'N' and 'n'

    >
    > ... that's just the case I was thinking of.


    I would say the algorithm uses a horrible convention in that case, one
    that makes it harder to talk (as opposed to write) about it. Talking you
    would have to keep saying "capital N", "lower case n" and the like.

    > But there are also
    > conventions like vectors being in capitals where you might want to use
    > case to distinguish even when there is no name clash.


    If the distinction is not obvious from the name then I would add vect_
    to the start of the names. I still do not see it confusing someone
    familiar enough with the algorithm to know the conventions it uses, and
    someone not familiar with the algorithm but familiar with C will find it
    easier.
    --
    Flash Gordon
     
    Flash Gordon, Mar 16, 2007
    #14
  15. In article <-gordon.me.uk>,
    Flash Gordon <> wrote:

    >>> Unless the convention of the algorithm uses both 'N' and 'n'


    >> ... that's just the case I was thinking of.


    >I would say the algorithm uses a horrible convention in that case, one
    >that makes it harder to talk (as opposed to write) about it. Talking you
    >would have to keep saying "capital N", "lower case n" and the like.


    Mathematicians (and physicists etc) are used to this. They generally
    say "big N" and "little n".

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Mar 16, 2007
    #15
  16. Guest

    Thank you very much. I have fixed the problem/ Now I am confused how
    to free this 2D array:


    If my code is as follows:

    int **xyz;
    int i, r = 3, c = 30000;


    xyz = malloc(r * sizeof(*int));
    for (i = 0; i < r; ++i)
    xyz = malloc(c * sizeof **xyz);

    how to free this memory block:

    for (i=0;i<r;i++)
    free xyz;

    does it work?

    and one more question what is difference between :

    for (i = 0; i < r; ++i)

    and

    for (i = 0; i < r; i++)

    I often use the later.

    Thanks



    On Mar 16, 10:01 am, (Richard Tobin) wrote:
    > In article <-gordon.me.uk>,
    > Flash Gordon <> wrote:
    >
    > >>> Unless the convention of the algorithm uses both 'N' and 'n'
    > >> ... that's just the case I was thinking of.

    > >I would say the algorithm uses a horrible convention in that case, one
    > >that makes it harder to talk (as opposed to write) about it. Talking you
    > >would have to keep saying "capital N", "lower case n" and the like.

    >
    > Mathematicians (and physicists etc) are used to this. They generally
    > say "big N" and "little n".
    >
    > -- Richard
    > --
    > "Consideration shall be given to the need for as many as 32 characters
    > in some alphabets" - X3.4, 1963.
     
    , Mar 16, 2007
    #16
  17. Chris Dollin Guest

    wrote:

    > and one more question what is difference between :
    >
    > for (i = 0; i < r; ++i)
    >
    > and
    >
    > for (i = 0; i < r; i++)


    There's no visible difference. (Since the value of `++i` or
    `i++` is never accessed, the fact that they're different doesn't
    make any difference.)

    > I often use the later.


    I use `i += 1`; as a general rule, I use the ++ forms when the
    value is used, and the += form when it isn't.

    (Yes, I know that the += form has a value.)

    --
    Chris "electric hedgehog" Dollin
    "People are part of the design. It's dangerous to forget that." /Star Cops/
     
    Chris Dollin, Mar 16, 2007
    #17
  18. Flash Gordon Guest

    Richard Tobin wrote, On 16/03/07 16:01:
    > In article <-gordon.me.uk>,
    > Flash Gordon <> wrote:
    >
    >>>> Unless the convention of the algorithm uses both 'N' and 'n'

    >
    >>> ... that's just the case I was thinking of.

    >
    >> I would say the algorithm uses a horrible convention in that case, one
    >> that makes it harder to talk (as opposed to write) about it. Talking you
    >> would have to keep saying "capital N", "lower case n" and the like.

    >
    > Mathematicians (and physicists etc) are used to this. They generally
    > say "big N" and "little n".


    Doesn't mean it isn't horrible and doesn't make it harder ;-)
    --
    Flash Gordon
     
    Flash Gordon, Mar 16, 2007
    #18
  19. user923005 Guest

    >From the C-FAQ:

    6.16: How can I dynamically allocate a multidimensional array?

    A: The traditional solution is to allocate an array of pointers,
    and then initialize each pointer to a dynamically-allocated
    "row." Here is a two-dimensional example:

    #include <stdlib.h>

    int **array1 = malloc(nrows * sizeof(int *));
    for(i = 0; i < nrows; i++)
    array1 = malloc(ncolumns * sizeof(int));

    (In real code, of course, all of malloc's return values would
    be checked.)

    You can keep the array's contents contiguous, at the cost of
    making later reallocation of individual rows more difficult,
    with a bit of explicit pointer arithmetic:

    int **array2 = malloc(nrows * sizeof(int *));
    array2[0] = malloc(nrows * ncolumns * sizeof(int));
    for(i = 1; i < nrows; i++)
    array2 = array2[0] + i * ncolumns;

    In either case, the elements of the dynamic array can be
    accessed with normal-looking array subscripts: arrayx[j]
    (for 0 <= i < nrows and 0 <= j < ncolumns).

    If the double indirection implied by the above schemes is for
    some reason unacceptable, you can simulate a two-dimensional
    array with a single, dynamically-allocated one-dimensional
    array:

    int *array3 = malloc(nrows * ncolumns * sizeof(int));

    However, you must now perform subscript calculations manually,
    accessing the i,jth element with array3[i * ncolumns + j]. (A
    macro could hide the explicit calculation, but invoking it would
    require parentheses and commas which wouldn't look exactly like
    multidimensional array syntax, and the macro would need access
    to at least one of the dimensions, as well. See also question
    6.19.)

    Yet another option is to use pointers to arrays:

    int (*array4)[NCOLUMNS] = malloc(nrows * sizeof(*array4));

    but the syntax starts getting horrific and at most one dimension
    may be specified at run time.

    With all of these techniques, you may of course need to remember
    to free the arrays (which may take several steps; see question
    7.23) when they are no longer needed, and you cannot necessarily
    intermix dynamically-allocated arrays with conventional,
    statically-allocated ones (see question 6.20, and also question
    6.18).

    Finally, in C9X you can use a variable-length array.

    All of these techniques can also be extended to three or more
    dimensions.

    References: C9X Sec. 6.5.5.2.
     
    user923005, Mar 16, 2007
    #19
  20. CBFalconer Guest

    wrote:
    >
    > Thank you very much. I have fixed the problem/ Now I am confused
    > how to free this 2D array: If my code is as follows:
    >
    > int **xyz;
    > int i, r = 3, c = 30000;
    >
    > xyz = malloc(r * sizeof(*int));
    > for (i = 0; i < r; ++i)
    > xyz = malloc(c * sizeof **xyz);
    >
    > how to free this memory block:
    >
    > for (i=0;i<r;i++)
    > free xyz;
    >
    > does it work?


    Please don't top-post. Your answer belongs after, or intermixed
    with, the snipped material which you quote.

    No, it doesn't work. You have to reverse all the malloc actions.
    So the appropriate code would be:

    for (i = 0; i < r; i++) free xyz;
    free(xyz);

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Mar 16, 2007
    #20
    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. John
    Replies:
    13
    Views:
    710
  2. ravi
    Replies:
    0
    Views:
    457
  3. Peter
    Replies:
    34
    Views:
    1,969
    Richard Tobin
    Oct 22, 2004
  4. porting non-malloc code to malloc

    , Feb 18, 2005, in forum: C Programming
    Replies:
    3
    Views:
    485
    Walter Roberson
    Feb 19, 2005
  5. Johs32

    to malloc or not to malloc??

    Johs32, Mar 30, 2006, in forum: C Programming
    Replies:
    4
    Views:
    328
    Captain Winston
    Mar 30, 2006
Loading...

Share This Page