Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
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.
 

kpua

macrumors 6502
Jul 25, 2006
294
0
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.
 

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
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.


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

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
and in the case of a matrix? How is such a beast created?
mat[][] to access.

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.
 

garethlewis2

macrumors 6502
Dec 6, 2006
277
1
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.
 

garethlewis2

macrumors 6502
Dec 6, 2006
277
1
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.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
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.

Well since he's using Cocoa, it doesn't look like that will be much of a problem ;)
 
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.

As opposed to avoiding compatibiltiy issues by ignoring ISO standards and putting all your compiler flags in README files rather than say, a makefile?
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
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.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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
 

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
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

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.