ShrimpBoyTheQuick wrote:
Hi!
I think I understand something here..
In C, I cannot put an unsized array in the .H file and set the size in the init method in the .m file...
Kinda true, but you can dynamically allocate memory at runtime to hold an array of whatever size you want.
What I can do is....
Define a pointer and a size in the .h file...
Create an array of the good size in the .m file and put the memory address of the array in the pointer and use this array with the pointer and the size value...
I don't think the code you posted will work right. I believe the "roomArray" that you're creating inside "initWithWidth" is a local variable that will go away once you exit the "initWithWidth" method. So even though you've stored a pointer to it in "arrayPtr" ultimately you'll be pointing at garbage once that memory gets reclaimed and reused by the runtime system.
What's my problem after this is how do I tell the object to not used the memory of this array after the method is finished?
Take a look at this
discussion of dynamically allocated arrays in C. Basically you'll use
malloc() to allocate memory for your array and then later use free() when you no longer need the array. And since you're dealing with width and height you're needing a two dimensional array which becomes somewhat messier.
Steve