no dynamic memory allocation in obj-c?

Dogcow

Registered
I declared an integer array in my header file:

int *data;

and am now trying to set the size of that array once I pull the dimensions from two textfields:

data = new int[x][y];

but Project Builder complains that 'new' is undeclared. Why doesn't this work? Any way to get around this...?

Thanks in advance,
-Dogcow "moof!"
 
Hmm...

There is dynamic memory allocation in Objective C, but I think you're mixing syntaxes.

NSObject *myObject;
int *x;

myObject = [[NSObject new] init];

x = malloc(256 * sizeof(int));

For the NSObject we're saying "call the method new on an NSObject, and then call init on the returned value". We can do this because NSObject's have those functions defined.

For the integer we're saying "Give me a chunk of memory of 256 times the size of an integer and store the address in x". Integers don't have methods associated with them.
 
Code:
new
is C++, not C - your code would work if the extension of the file was .mm, as you'd then be using Objective-C++.
 
Back
Top