Thread: Making array abstract datatype problem

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    21

    Making array abstract datatype problem

    Hello!

    I am trying to create my own abstract datatype of an array, and I have a problem when I want to create it. My function prototype is this;
    Code:
    return? createArray(int numberOfDimnesions, int* dimensionSizes);
    This is how it works: The user enters the number of dimensions the user want, and sends a int pointer holding all the sizes of every array.

    The problem though is say the user want a 4-dimensional array. Then the "first" array must be pointer to poiner to pointer to pointer looking like this
    Code:
    array*** theArray = malloc(10*sizeof(array**));
    and how can I make a function that can do that? What I am trying to say is that the first array can be infinity pointer to pointer etc. Is there a datatype that works for everything like void* that can achieve this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > What I am trying to say is that the first array can be infinity pointer to pointer etc.
    Well this isn't what abstraction is.

    Consider
    Code:
    typedef struct myArrayADT {
      int numDimensions;  // how many dimensions
      int *dimensionSizes;  // size of each dimension
      size_t elementSize; // size of the data, eg sizeof(int)
      char *data; // where it's all stored
    } myArrayADT_t;
    
    
    myArrayADT_t *createArray(int numberOfDimnesions, int* dimensionSizes, size_t elemSize ) {
      myArrayADT_t *result = malloc( sizeof(*result) );
    
      // calculate how much memory the entire array needs
      size_t memSize = elemSize;
      for ( i = 0 ; i < numberOfDimnesions ; i++ ) {
        memSize *= dimensionSizes[i];
      }
      result->data = malloc( memSize );
    
      // fill in rest of result->members as appropriate
      return result;
    }
    You also need things like
    void destroyArray( myArrayADT_t *array );
    void readArray( myArrayADT_t *array, ... ); // you figure out what other params you need
    void writeArray( myArrayADT_t *array, ... );
    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.

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    21
    Okay, but I have two questions. What is elemSize that you are passing into the createArray-function? Is that the size of the data to be stored in the array? Isnt it possible to just have a void pointer in the structure thus making it able to store any data? Also, will I be able to use normal array syntax on this array? You know like result[4][5][2] etc?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > Okay, but I have two questions. What is elemSize that you are passing into the createArray-function? Is that the size of the data to be stored in the array?
    Yes it is, like so
    Code:
    int dimens[] = { 2, 3, 4, 5 };
    myArrayADT_t *intArray = createArray(4, dimens, sizeof(int) );
    myArrayADT_t *doubleArray = createArray(4, dimens, sizeof(double) );
    > Isnt it possible to just have a void pointer in the structure thus making it able to store any data?
    Sure you can make it a void* if you want, but at some point you need to do address arithmetic on it in order to access elements of the array.
    This arithmetic is a lot easier if the pointer is char* to begin with.

    > Also, will I be able to use normal array syntax on this array? You know like result[4][5][2] etc?
    No.
    That's why you need read/write functions taking an instance of your ADT as a parameter.

    You can do this
    Code:
    // creates a dynamic version of result[5][6][7];
    // someCreateFunction is hard-coded to always make [x][6][7] arrays.
    int (*result)[6][7] = someCreateFunction(5);
    and be able to do access result[4][5][2] right out of the gate, but that is very far from being abstract.
    C doesn't allow you to create types at run-time, so you can't create the correct type for the dimens in the first example in order to be able to do subscripting directly.
    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.

  5. #5
    Registered User
    Join Date
    Sep 2016
    Posts
    21
    Ok thanks a lot for the info! Hope your personal conflict with void main gets solved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LinkedList based abstract Datatype for Pairs and such.
    By Solour in forum C++ Programming
    Replies: 10
    Last Post: 03-02-2013, 10:15 AM
  2. Replies: 7
    Last Post: 09-23-2012, 03:06 PM
  3. What is the datatype? -- possible array decay?
    By steals10304 in forum C Programming
    Replies: 2
    Last Post: 10-12-2009, 09:28 PM
  4. C# Converting one datatype array to another
    By kevins963 in forum C# Programming
    Replies: 2
    Last Post: 09-13-2009, 10:11 PM
  5. Array of Base(Abstract) Class Problem
    By Aramil in forum C++ Programming
    Replies: 10
    Last Post: 03-14-2008, 02:58 PM

Tags for this Thread