c-arrays and struct

Discussion in 'Mac Programming' started by MrFusion, Sep 12, 2007.

  1. MrFusion macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #1
    Being a (mostly) self taught programmer, I am not sure what the good practices are concerning struct's and c-array's.
    I know how to define a struct and arrays inside a function.

    Code:
    typedef struct {
    	double x;
    	double y;
    	double z;
    } position;
    
    @implementation MyClass :NSObject {
    -(void) test {
        position somePositions[3][2] = {{{1,2,3},{3,4,5}},{{1,2,3},{3,4,5}},{{1,2,3},{3,4,5}}}
     somePositions[1][2].z = 44;
    //etc.
    }
    }
    But not how to declare one of unknown length in the @interface.
    Code:
    @interface MyClass :NSObject {
        position allpositions[]
    
    }
    Yes, there are NSMutableArray's, but these are not very convenient for things like storing matrices (or doing matrix calculations).

    All help and lessons are appreciated.

    PS. Unfortunately I don't have my obj-c book close by for the next several days.
     
  2. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #2
    Arrays in C are accessed simply by using a pointer to the first element in the array. Hence the following are essentially equivalent:

    Code:
    int myArray[ARRAY_LENGTH];
    int * myArrayPtr;
    myArray is a pointer to ARRAY_LENGTH words allocated either in static memory or the stack.
    myArrayPtr is a pointer to nothing... yet. Allocating that memory is up to you.

    C doesn't let you use the [] syntax to declare arrays of dynamic size. You have to declare such an array as a pointer and malloc memory for it at runtime. Once you have the memory allocated, you can use the [] syntax to access elements of the array just as you normally would.
     
  3. MrFusion thread starter macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #3

    and in the case of a matrix? How is such a beast created?
    mat[][] to access.
     
  4. Krevnik macrumors 68040

    Krevnik

    Joined:
    Sep 8, 2003
    #4
    Pointer to a pointer:

    int **myMatrixPtr;

    Allocating it is another matter entirely:
    Code:
    myMatrixPtr = malloc( sizeof(int *) * xDimension );
    for( i = 0; i < xDimension; i++ )
        myMatrixPtr[i] = malloc( sizeof(int) * yDimension );
    
    And then there is the nightmare of freeing that bastard.
     
  5. garethlewis2 macrumors 6502

    Joined:
    Dec 6, 2006
    #5
    You can pass just using

    functionname(struct[][3], int size)

    As C only need to one of the dimensions for memory access. I believe it is that one I haven't got my K&R book with me.

    Why are you trying to do this with structures. Your using Objective-C, why don't you put the dim struct into class, which is semantically identical to a structure anyway. Then stick this into a two dimensional NSArray. You can then forget about memory problems as long as you understand reference counting.
     
  6. AlmostThere macrumors 6502a

    #6
  7. garethlewis2 macrumors 6502

    Joined:
    Dec 6, 2006
    #7
    If he did that, his code will completely and totally unportable. Not to mention unless he puts a specific line in the readme for how to configure the compiler, code written with these flags in mind would never compile anywhere else.
     
  8. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #8
    Well since he's using Cocoa, it doesn't look like that will be much of a problem ;)
     
  9. AlmostThere macrumors 6502a

    #9
    As opposed to avoiding compatibiltiy issues by ignoring ISO standards and putting all your compiler flags in README files rather than say, a makefile?
     
  10. Krevnik macrumors 68040

    Krevnik

    Joined:
    Sep 8, 2003
    #10
    I still think the easiest and simplest way (and still have high perf) is to use a struct with a handle in it:

    Code:
    typedef struct {
      int x, y;
      int **data;
    } MrMatrix;
    
    MrMatrix *CreateMatrix(int x, int y) {
      // Allocate here, storing x and y into the struct
    }
    
    void FreeMatrix(MrMatrix *matrix) {
      // Break down here, using x and y to help guide how to dealloc the matrix
    }
    
    // Access matrix example using this template:
    MrMatrix *myMatrix;
    
    myMatrix = CreateMatrix(3, 3);
    
    myMatrix->data[2][2];
    
    Easy, portable, and your data access will be faster than using NSArray, without stepping outside ISO.
     
  11. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #11
    Hi

    If you don't mind running your code throught the Objective-C++ compiler you could implement your matricies using C++ classes (and leave the rest of the code in Objective-C). This would allow you to define operators for matrix addition, scalar multiplication etc. Whether this would be of any help or interest to you I don't know. Anyway, if you were to do it this way then you might want to define a column vector class and use this as the basis for your matrix class and operators.

    b e n
     
  12. MrFusion thread starter macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #12
    I haven't yet taking the time to delve into xCode and its compiler settings etc.
    So I rather not do anything that requires anything beyond cmd-B or cmd-R.
    Certainly not .make files or different compilers. It's probably easy to do, but I need to alloc time for it.
     

Share This Page