Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-11-2010, 11:55 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 243
benoitr007 is on a distinguished road
Default N00b question about arrays

Hello,

I know it's no big deal, but I was wondering... Why do we have to initialize arrays with a size, for example it won't let me do this:

Code:
NSString *category[];
But it lets me do this:

Code:
NSString *category[0];
However, I have no idea what the size of the array actually changes, because even if I put 0 it works and I can save values such as:

category[0] = @"Hello";
category[1] = @"How are you";
category[2] = @"Blah blah";

and retrieve them correctly after. What am I missing? Thank you very much!
benoitr007 is offline   Reply With Quote
Old 09-12-2010, 12:08 AM   #2 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by benoitr007 View Post
Hello,

I know it's no big deal, but I was wondering... Why do we have to initialize arrays with a size, for example it won't let me do this:

Code:
NSString *category[];
But it lets me do this:

Code:
NSString *category[0];
However, I have no idea what the size of the array actually changes, because even if I put 0 it works and I can save values such as:

category[0] = @"Hello";
category[1] = @"How are you";
category[2] = @"Blah blah";

and retrieve them correctly after. What am I missing? Thank you very much!
Using the [] bracket notation is the C-style syntax, which causes (in this case) a fixed-size array of NSString * instances to be allocated. In order for the compiler to allocate the correct amount of storage for the data, it has to know the size of the array, which is why the value is required in between the square brackets.

If you need a variable sized array, just use NSMutableArray.

Also, the example code that you provided where you access elements 0, 1 and 2 or your array is stomping memory outside of the array. The code may appear to work correctly, but since the compiler hasn't actually allocated the correct amount of space for the array, you're actually writing over memory that is beyond the bounds of whatever space has been allocated.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 09-12-2010, 12:18 AM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 243
benoitr007 is on a distinguished road
Default

Quote:
Also, the example code that you provided where you access elements 0, 1 and 2 or your array is stomping memory outside of the array. The code may appear to work correctly, but since the compiler hasn't actually allocated the correct amount of space for the array, you're actually writing over memory that is beyond the bounds of whatever space has been allocated.
Right, I noticed that shortly after. Thank you very much for your answer!
benoitr007 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 349
9 members and 340 guests
cgokey, givensur, iGamesDev, jenniead38, mraalex, PixelInteractive, raihan.zbr, Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:00 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0