2D arrays:dynamic allocation and freeing

This is a discussion on 2D arrays:dynamic allocation and freeing within the C Programming forums, part of the General Programming Boards category; hi all, The way to dynamically allocate a 2D array is : int a(*)[5]; a=(int (*)[5])malloc(sizeof(int)*5); This will create array ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    bangalore
    Posts
    3

    2D arrays:dynamic allocation and freeing

    hi all,

    The way to dynamically allocate a 2D array is :


    int a(*)[5];

    a=(int (*)[5])malloc(sizeof(int)*5);


    This will create array of pointer to arrays.

    We can access elements as a 2D array
    a[1][1] with access particular element.



    Question:
    1.
    After allocating space for pointers, is it not the case that i need to allocate space for the array they are pointing to?, as we usually do for a pointer to point to array of elements.

    2.
    Is my way of freeing ok

    for(i=0;i<5;i++)
    free(a[i]);

    Because it will free the pointers , what about the elements associated with those pointers.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    > int a(*)[5];
    This is a pointer to an array (not a pointer to a pointer).

    > a=(int (*)[5])malloc(sizeof(int)*5);
    1. Never cast malloc (see the FAQ)
    2. You don't allocate enough memory. The correct call is
    a = malloc ( 5 * sizeof *a );

    > Is my way of freeing ok
    No, there is only one malloc, so there is only one free, and that would be
    free( a );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Location
    bangalore
    Posts
    3

    where have i found int (*)[5];

    Sorry for that message above:


    This is valid syntax.
    Actually, i was going through code for mp3, at that time i found this:

    [/QUOTE][/PHP][/CODE]
    struct mad_stream
    {
    ....
    ....
    unsigned char (*main_data)[MAD_BUFFER_MDLEN];
    ....
    };

    struct mad_stream *stream;

    stream->main_data=(unsigned char (*)[MAD_BUFFER_MDLEN])malloc(MAD_BUFFER_MDLEN);

    [CODE][PHP][QUOTE]

    And this alloates pointers to array of characters i.e. strings.
    In this array lots of strings can be stored.

    Am I wrong with the above statement?
    If yes, do correct me.

    regards
    bravetanveer

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you bothering with all this convoluted crap? Why don't you just use a pointer to a pointer, malloc N poitners, then malloc M bytes for each?
    Code:
    unsigned char **twod;
    int x;
    
    twod = malloc( N * sizeof( unsigned char * ) );
    for( x = 0; x < N; x++ )
        twod[x] = malloc( M * sizeof( unsigned char ) );
    
    ...do stuff with stuff...
    
    for( x = 0; x < N; x++ )
        free( twod[x] );
    free( twod );
    And is your preview button broken, or are you just lazy? Or what about your edit button? Your tags are backwards. Go read the announcements again.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    > And this alloates pointers to array of characters i.e. strings.
    Not quite - all those parentheses matter, and translating them into words requires a good deal of precision.

    If you have
    Code:
    char *q = malloc( 10 * sizeof *q );
    q is a pointer to an array of 10 characters
    It is the dynamically allocated equivalent of char q[10]

    If you have
    Code:
    char (*p)[20] = malloc( 10 * sizeof *p );
    p is a pointer to an array of 10 array-of-20-characters.
    It is the dynamically allocated equivalent of char p[10][20]

    If you have
    Code:
    char **p = malloc( 10 * sizeof *p );
    p is a pointer to an array of 10 pointers to char.
    It is the dynamically allocated equivalent of char *p[10]

    However, by itself it is not able to store strings, so you need (as per Quzah's post) to allocate some more memory
    Code:
    for ( i = 0 ; i < 10 ; i++ ) p[i] = malloc( 20 * sizeof *p[i] );
    Now you have the dynamically allocated equivalent of char p[10][20]

    Now both types of p can be used to store up to 10 strings, each up to 20 characters in length. So for example, this is valid for both of them
    Code:
    strcpy( p[0], "hello world" );
    So why choose char (*p)[20] over char **p ?
    Well the pointer to an array is much easier to allocate (just one malloc call), but the downside is that every string will have space for 20 characters whether you want them or not.
    The pointer to a pointer on the other hand is more complicated to allocate, but you get the freedom to allocate memory according to the length of each string stored.

    > stream->main_data=(unsigned char (*)[MAD_BUFFER_MDLEN])malloc(MAD_BUFFER_MDLEN);
    If this is actual C code, then it is broken.
    1. You don't cast malloc in C
    2. There is no sizeof in the malloc call, so all you've managed to achieve with this call is the equivalent of a pointer to unsigned char array[1][MAD_BUFFER_MDLEN];
    Essentially, you've added a dimension for no reason at all.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Location
    bangalore
    Posts
    3

    Thanks

    hi salem,

    Thanks for that detailed explanation of pointer and arrays.

    I got some real good knowledge.

    regards
    bravetanveer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Increasing memory allocation in function
    By Ramses800 in forum C Programming
    Replies: 3
    Last Post: 12-16-2008, 04:30 AM
  2. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 04:56 AM
  3. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 07:36 AM
  4. memory allocation and freeing
    By Jase in forum Linux Programming
    Replies: 1
    Last Post: 05-25-2003, 06:26 AM
  5. dynamic allocation question
    By vale in forum C++ Programming
    Replies: 1
    Last Post: 08-26-2001, 04:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21