Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Why error?

Why error?
Thread Tools
Fresh-Faced Recruit
Join Date: Mar 2001
Location: Göteborg, Sweden
Status: Offline
Reply With Quote
Mar 22, 2001, 05:21 AM
 
I have just started with cocoa programming and making a little test program just for learning but now it quits "due to signal 10 (SIGBUS)".

The code:
Code:
NSArray *keyArray, *vArray; NSDictionary *aFile; keyArray = [[NSArray alloc] initWithObjects:@"Number", @"Name"]; vArray = [[NSArray alloc] initWithObjects:@"5", @"Fredrik"]; // It's here the error occurs aFile = [[NSDictionary alloc] dictionaryWithObjects:vArray forKeys:keyArray]; [files addObject:aFile];
I have read that it occurs because of you try to write in the memory where you not allowed to but i don't know why it occurs there. Any ideas?
     
Senior User
Join Date: Feb 2001
Location: Rochester, uk
Status: Offline
Reply With Quote
Mar 22, 2001, 09:56 AM
 
Signal 10, as far as i can tell, is the equivalent of Java's NullPointerException - ie, you're trying to get at the contents of an object that's set to null, and so doesn't have any contents.

I have no idea why it's happening there, but all i can say is to try the debugger. set a breakpoint a few lines before it (by clicking in the margins), compile and hit 'Debug'. Then watch each of the values as you walk through.

All words are lies. Including these ones.
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
Mar 22, 2001, 11:39 AM
 
Originally posted by Fredrikp:
I have read that it occurs because of you try to write in the memory where you not allowed to but i don't know why it occurs there. Any ideas?
In C, variable-argument functions (and ObjC methods) don't know how many arguments they're given, so they need some other way to tell when the argument list is over. Each vararg routine needs to set its own policy on that. printf() and NSString's -initWithFormat: methods can tell by the number of replacement tokens in the format string. In the case of NSArray's -initWithObjects:, it's the presense of a "nil" argument -- i.e., it keeps reading arguments until it finds nil. If you don't put a nil in there, there's no telling what could happen, other than you'll likely get a bus error or segmentation fault.

So, just change the two lines to:

keyArray = [[NSArray alloc] initWithObjects:@"Number", @"Name", nil];

vArray = [[NSArray alloc] initWithObjects:@"5", @"Fredrik", nil];
     
Senior User
Join Date: Feb 2001
Location: Rochester, uk
Status: Offline
Reply With Quote
Mar 23, 2001, 05:53 AM
 
you know, that really is a dumb system. wouldn't it be better if the compiler just generated a nil at the end of the list whenever it gets variable arguments? It needn't have any effect on methods who use something else to decide on the length, because they wouldn't bother looking at it.

Ok, so it's arrogant for a two bit little web coder to be telling ANSI what they should do - but that doesn't stop it being dumb.

All words are lies. Including these ones.
     
ali
Forum Regular
Join Date: Sep 2000
Status: Offline
Reply With Quote
Mar 23, 2001, 10:48 AM
 
Variable argument lists have always been a bit troublesome in C... Best to limit their use as much as possible.


I suppose one issue with putting a NULL automatically at the end could be that if the compiler did not see a declaration for the function, it wouldn't do this, and folks who are used to this behavior might be even more confused. In general C is fairly rigid in its rules, and it's probably best for it to stay this way. It just needs to be made it clear in the declaration of functions like this that a NULL is needed.


Ali
     
   
Thread Tools
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -5. The time now is 09:45 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2