PDA

View Full Version : c-arrays and struct




MrFusion
Sep 12, 2007, 01:27 PM
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.

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.
@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
Sep 12, 2007, 02:04 PM
Arrays in C are accessed simply by using a pointer to the first element in the array. Hence the following are essentially equivalent:

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
Sep 12, 2007, 06:03 PM
Arrays in C are accessed simply by using a pointer to the first element in the array. Hence the following are essentially equivalent:

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
Sep 12, 2007, 07:42 PM
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:

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
Sep 13, 2007, 07:57 AM
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.

AlmostThere
Sep 13, 2007, 08:25 AM
C doesn't let you use the [] syntax to declare arrays of dynamic size.

Yes, it does (although see the gcc comments at http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html and you may need to check the necessary command line switches).

garethlewis2
Sep 13, 2007, 09:28 AM
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
Sep 13, 2007, 09:37 AM
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 ;)

AlmostThere
Sep 13, 2007, 10:39 AM
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
Sep 13, 2007, 12:33 PM
I still think the easiest and simplest way (and still have high perf) is to use a struct with a handle in it:

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
Sep 13, 2007, 03:01 PM
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
Sep 15, 2007, 03:27 PM
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.