Passing 2D dynamic arrays

This is a discussion on Passing 2D dynamic arrays within the C Programming forums, part of the General Programming Boards category; Hi all, Can you please point out to me how can I correctly pass a 2d dynamically allocated array into ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    55

    Passing 2D dynamic arrays

    Hi all,

    Can you please point out to me how can I correctly pass a 2d dynamically allocated array into a function? My problem is that I cannot know the size of the two dimensions beforehand, and I always read that the siez of the last dimension must always be passe with the array.

    I've managed to get round this by using something like:
    Code:
    /* function declaration */
    void function(double **array2d, int rows, int cols)
    
    /* calling the function from main */
    function(array2d, rows, cols)
    This works, but valgrind says that there is an error (which I cannot understand what it's saying on the output). I can verify that this is passed as intended (all values in the 2D array are ok), but I'm sure there's something wrong and that there should be a better way to do this.

    Can anyone please explain to me a better way to do this? (if how I've done this above is indeed wrong)

    Many thanks!

    Spiros

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    It depends on how you do it. If you allocate an array of pointers and from that memory chunks, then you need to pass a T**.
    If you allocate one big chunks of memory to account for the dimensions, then you need to pass T*.
    C is a harsh mistress.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    55
    Thanks Elysia,

    The way I'm allocating 2d arrays is this:
    Code:
        arr2d=malloc(rows*sizeof(*arr2d));
        if (arr2d==NULL) {
            printf("Could not allocate data array of size %i\n",rows*sizeof(*arr2d));
            return NULL;
        }
        for (i=0; i<rows; i++) {
            arr2d[i]=malloc(cols*sizeof(*arr2d[i]));
            if (arr2d[i]==NULL) {
                 printf("Could not allocate array data[%i] of size %i\n",i, cols*sizeof(*arr2d[i]));
                 return NULL;
            }
        }
    So if I've understood right then passing a pointer to pointer is the appropriate way. Am I right?

    Would you then say that there's nothing wrong with

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That looks correct. I'm guessing arr2d is defined as an int**. You should be able to pass that in as you have it in your first post. Is valgrind complaining about the function call, or something you do inside the function?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    A simple test case, and your actual valgrind errors (not "I got some errors I don't understand") would go a long way.
    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
    Nov 2004
    Posts
    55
    after couple of days of fiddling with the code, I found out that I was trying to read out of bounds of an array on the last iteration inside a loop.... That's a silly mistake that I shouldn't have done in the first place. Anyway, this makes me to check these things more vigorously to prevent this from happening again.

    Thank you all!

    Spiros

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault AND 2d dynamic arrays
    By lord in forum C++ Programming
    Replies: 3
    Last Post: 08-04-2008, 02:07 PM
  2. Passing 2D arrays
    By samGwilliam in forum C++ Programming
    Replies: 1
    Last Post: 01-21-2006, 09:04 PM
  3. Dynamic 2D arrays question
    By MadStrum! in forum C Programming
    Replies: 7
    Last Post: 02-08-2003, 05:54 AM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 10:02 PM
  5. dynamic arrays and structures
    By godofbabel in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2002, 04:45 PM

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