#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Post Need to access data stored in memory as 2d array


    Hi!

    I have data stored in memory which is a raw image frame for a video. The pointer to the first pixel as well as the dimensions of the frame are available to me as arguments for my function.

    I want to access these pixels as a 2d array in the function. I know it can be accessed using pointer arithmetic but I specifically need to access it as src[i][j].

    Is this possible? How do I do it?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    Is the number of columns in the 2D array a compile time constant?
    If so, you can simply cast your pointer with
    pixel (*twod)[COLUMS] = ptr;
    Then use twod[row][col] to access the image.

    If not, you have to allocate an array of pointers (so begin with pixel** twod;), which you initialise with pointer arithmetic.
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    No the # of columns is not a compile time constant. What do I do after declaring **twod ?
    Originally Posted by salem
    Is the number of columns in the 2D array a compile time constant?
    If so, you can simply cast your pointer with
    pixel (*twod)[COLUMS] = ptr;
    Then use twod[row][col] to access the image.

    If not, you have to allocate an array of pointers (so begin with pixel** twod;), which you initialise with pointer arithmetic.
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    Something like
    twod = malloc( rows * sizeof(*twod) );

    Then
    for ( r = 0 ; r < rows ; r++ ) twod[r] = pointer_expression_to_each_row;
    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
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    Thanks! But I don't want to use malloc as I already have the data and I do not want to waste memory and processor cycles copying that data to the new location.
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    Then you're going to need a modern compiler which supports variable length arrays.
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    void foo ( void *p, int rows, int cols ) {
      int (*arr)[cols] = p;
      for ( int r = 0 ; r < rows ; r++ ) {
        for ( int c = 0 ; c < cols ; c++ ) {
          printf("%d ", arr[r][c] );
        }
        printf("\n");
      }
    }
    
    int main ( ) {
      int a[2][2] = { { 1, 2 }, { 3, 4 } };
      int b[3][3] = { { 0 } };
      foo(a,2,2);
      foo(b,3,3);
      return 0;
    }
    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
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    I don't think my compiler supports that as I am coding for a dsp. But do you think using realloc instead of malloc would work?
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    malloc was off the table in your previous post - what changed?

    And no, realloc won't do you any good in this case.

    Perhaps you should explain why manual subscripting isn't allowed (apart from "I don't like it").

    Writing array[row][col] doesn't give you any special magic over row*colsize+col. You're just doing manually what the compiler would generate for you.
    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

IMN logo majestic logo threadwatch logo seochat tools logo