Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / Objective-C is such an ugly programming language

Author
Message
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 18th Jun 2010 15:42
I'm supposed to learn Objective-C for work to make iPhone apps, and it's really bugging me. I don't think I've ever seen such an ugly programming language.

What really annoys me is that it's supposed to be a superset of C - i.e., C, but with more. So, you can write a normal function just like in C, like this:


Fair enough. But, if you wanted to write a method for a class, you'd have to write it like this!


Which would be invoked as such:



Why is it that it's a superset of C, but anything it adds to it has to look nothing like C at all? It's really inconsistent and kinda pointless. I also don't see the point in naming parameters, since the variable names of the parameters are good enough names. Not to mention that the first parameter isn't named - technically it's name is the name of the method.


I don't know what went through Steve Jobs' mind when he decided to use this programming language for everything. Too much LSD, Mr. Jobs.

"everyone forgets a semi-colon sometimes." - Phaelax
Jeku
Moderator
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location: Vancouver, British Columbia, Canada
Posted: 18th Jun 2010 20:33
Yes, I agree that Objective-C is horrid.


Senior Web Developer - Nokia
draknir_
18
Years of Service
User Offline
Joined: 19th Oct 2006
Location: Netherlands
Posted: 18th Jun 2010 21:13
Agreed, it's a horrible distortion of C. Fortunately the company I'm working at (also on an iPhone app) has created a C++ engine on top of a thin layer of Obj C. Works great!

Unfortunately I still have to work on a mac (puke) to run the emulator.
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 18th Jun 2010 21:56
draknir_, is it Objective-C++ or something totally different? It sounds kinda cool!


The only part I kinda like about it is the memory management, but at the same time, it gives you so much control that it might as well let you handle it all completely yourself.

"everyone forgets a semi-colon sometimes." - Phaelax
David R
21
Years of Service
User Offline
Joined: 9th Sep 2003
Location: 3.14
Posted: 19th Jun 2010 00:50
News at 11: Language that isn't C doesn't have syntax like C


Quote: "Why is it that it's a superset of C, but anything it adds to it has to look nothing like C at all? "


Really easy answer to that actually: none of the syntax can clash with C. Remember, you can mix with ObjC with C. Not to mention that ObjC was initially a glorified preprocessor not a standalone runtime.

So the syntax is like that to stop ambiguity. If the designers could have reused C syntax I'm sure they would have

09-f9-11-02-9d-74-e3-5b-d8-41-56-c5-63-56-88-c0
Indicium
16
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 19th Jun 2010 01:02
Quote: "News at 11: Language that isn't C doesn't have syntax like C "


I'm yet to see an non-sarcastic post from you.

Rawwrr. Sig Fail.
Newcastle is awesome
David R
21
Years of Service
User Offline
Joined: 9th Sep 2003
Location: 3.14
Posted: 19th Jun 2010 01:20 Edited at: 19th Jun 2010 01:22
Quote: "I'm yet to see an non-sarcastic post from you."


I'm yet to see any non-troll post from you

But sarcasm... from me? Surely you're joking? I don't know what you're talking about

09-f9-11-02-9d-74-e3-5b-d8-41-56-c5-63-56-88-c0
Jeku
Moderator
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location: Vancouver, British Columbia, Canada
Posted: 19th Jun 2010 02:37 Edited at: 19th Jun 2010 02:40
Quote: "News at 11: Language that isn't C doesn't have syntax like C"


Way to state the obvious This is exactly what this whole thread is about... you know, why Objective-C is not like every other popular C-scripted language (PHP, C#, Java, C++, etc.)

Quote: "Really easy answer to that actually: none of the syntax can clash with C. Remember, you can mix with ObjC with C. Not to mention that ObjC was initially a glorified preprocessor not a standalone runtime.

So the syntax is like that to stop ambiguity. If the designers could have reused C syntax I'm sure they would have"


That doesn't make sense. If you can mix Objective-C with C (which I have done), then why can't it clash? I'm not fully understanding that. You're saying it *can't*... but why not? The iPhone API could very well work in C++. As far as I can tell, and I have made an iPhone game so I know a thing or two, there is no advantage of Objective-C over C++.


Senior Web Developer - Nokia
David R
21
Years of Service
User Offline
Joined: 9th Sep 2003
Location: 3.14
Posted: 19th Jun 2010 14:20 Edited at: 19th Jun 2010 14:21
Quote: "That doesn't make sense. If you can mix Objective-C with C (which I have done), then why can't it clash? "


Because initially ObjC was only a preprocessor (OOPC) not a fully fledged language. From the point of view of a preprocessor (and considering you can mix it with C) how would the preprocessor discern the C from the ObjC if the syntax was the same? (obviously it's not just a preprocessor any more, but the initial versions were just that)

I mean you're right in a way because it's not the entire reason it's ugly (it's influenced by Smalltalk which has the same square brackets) but at least part of it is influenced by its heritage of needing to remove ambiguity for preprocessing

09-f9-11-02-9d-74-e3-5b-d8-41-56-c5-63-56-88-c0
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 19th Jun 2010 14:56
I'm certain it's possible to make such a preprocessor - one with the same syntax as C. How many OO preprocessors have there been for DBP that manage to keep the same style of syntax? Quite a few.

"everyone forgets a semi-colon sometimes." - Phaelax
David R
21
Years of Service
User Offline
Joined: 9th Sep 2003
Location: 3.14
Posted: 19th Jun 2010 16:06 Edited at: 19th Jun 2010 16:20
Quote: " How many OO preprocessors have there been for DBP that manage to keep the same style of syntax? Quite a few."


All of which define new syntax though. Which is exactly what ObjC does

You have to keep in mind:

- You (and I) see C++'s approach to OOP syntax is 'normal'. Because we use C++ and because C++ has influenced countless other languages (Java, PHP5's OOP, C#, Perl, D etc.)

- C++ was extremely young and immature (~2 years after initial impl. ) when ObjC was conceived. Again, the notion of its OOP syntax (and the notion that a superset of C has to reuse C's syntax for that matter) would not have been expected nor intended by the designer(s)

Quote: "I'm certain it's possible to make such a preprocessor - one with the same syntax as C."


OK then - rewrite that (ObjC) method declaration in your first post to how you think it should look, with the following constraints:

a) It still has to be a class method and you need to be able to distinguish (completely) between C and ObjC during preprocessing

b) It has to have some syntactical resemblance to SmallTalk (C++ is still a 'zero' at this point so ST is practically the only other example of OOP in existence)

c) You can't use any conventions from C++ at all (after all, C++ is still new and can't really be an influence. That and the fact its approach to OOP syntax is not a widely accepted / known convention)

d) You have to be able to parse (and rewrite) on top of an existing C compiler (you can't make a new compiler)


But overall: I think ObjC looking horrible is a case of "winners writing history". It only looks horrible because we're so used to the idea of C++ and the superset looking almost identical to C itself. If ObjC were the 'winner' it'd be the other way around (Also it's worth noting that C++ is not really a superset of C because a bunch of things in C work completely differently under a C++ compiler - which is something ObjC did not have the luxury of being able to do)


EDIT:
Quote: " you know, why Objective-C is not like every other popular C-scripted language (PHP, C#, Java, C++, etc.)"


Err, because barring C++, all of those things popped up after ObjC did. And as mentioned above, C++ only predates it by ~2 years or so (which wouldn't have been enough for C++ to make a profound or popular impact). They were conceived around the same time and likely did little to influence each other, so it's not as if ObjC is some deliberate ploy to screw you around

09-f9-11-02-9d-74-e3-5b-d8-41-56-c5-63-56-88-c0
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 19th Jun 2010 16:59
DavidR,

Have you seen the way classes are made in Objective-C? They are made in two parts:

The interface:



And the implementation:



As you can see, nothing there looks like C. If you just had a 'class' keyword (if you wanna nit-pick, this isn't C syntax either), you have a hint to the preprocessor. Methods can have the same format as C functions - none of this "-(void) aMethod" crap. The preprocessor could still tell the difference.

So that satisfies constraint (a).
Constraint (b) is unfair, because as I said, as a superset to C, it shouldn't have to resemble SmallTalk - it should have to resemble C.
Constraint (c) is unfair too. In C, you can do member access from a struct object as such: "obj.field". It makes logical sense to call methods in the same way. Furthermore, method calls and function calls should look similar (barring the 'dot' notation), which, of course, resembles C++.

Constraint (d): Here's the exact same thing done for DBP: http://forum.thegamecreators.com/?m=forum_view&t=117927&b=8


My point is that it's not actually that difficult to make a preprocessor for a language that makes it actually resemble the original language. Objective-C doesn't look weird because it's not what we're used to - it's because the bits that it extends to C don't look anything like C (bold because I know you're going to say it would be adding new syntax anyway, like a 'class' keyword).

"everyone forgets a semi-colon sometimes." - Phaelax
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 19th Jun 2010 17:35
TBH, I think most of you are missing David R's point - just because extra keywords were added in C++ or other languages doesn't mean that it has to be done that way.

For example, you could just use a single special character that doesn't already appear in the existing language to signal something new ... kind of like # in the C preprocessor, or @ for Objective C, or like has been done in C++ recently for Lambas.

As for 'style' of OOP additions, way back when, there weren't very many alternatives to choose from - Objective C went with SmallTalk as the most well-known at that point, while C++ was based on the Algol/Simula model.

Neither is right or wrong, they are just ways of expressing the same thing.

All that said though, I find Objective C as ugly as most of you do, but I can also accept that it's because I'm used to something different.

David R
21
Years of Service
User Offline
Joined: 9th Sep 2003
Location: 3.14
Posted: 19th Jun 2010 17:37
Quote: "As you can see, nothing there looks like C."


Yes. If you ignore:

- Semicolons as terminators
- { } scopes
- Exact same primitive keywords
- The notion of definition vs. declaration

Quote: "Constraint (b) is unfair, because as I said, as a superset to C, it shouldn't have to resemble SmallTalk - it should have to resemble C"


Again, C++ set the precedent that a superset of C has to reuse or resemble C. Just because something is a superset does not mean it needs to look like it. I think the only reason for it being a superset in the first place is so that C could be easily built upon without writing a compiler. It isn't because it is supposed to be C.

Also, being like ST was the goal of ObjC, so no, that isn't unfair

Quote: "Constraint (c) is unfair too. In C, you can do member access from a struct object as such: "obj.field". It makes logical sense to call methods in the same way. "


That's a valid point, but again:

- [ ] is a holdover from SmallTalk
- Method calls have absolutely no resemblance (on an impl. level) to C method calls, so arguably having the same syntax would be misleading

Quote: "Constraint (d): Here's the exact same thing done for DBP: http://forum.thegamecreators.com/?m=forum_view&t=117927&b=8"


I ignored the BASIC stuff in your previous post because it's completely incomparable. DBP is inherently unambiguous because every 'thing' and every type of scope start/end has distinct keywords which is a world away from C

Not to mention that a function call is explicitly labelled as such (and it has no notion of declaration vs. definition)

Quote: " doesn't look weird because it's not what we're used to - it's because the bits that it extends to C don't look anything like C "


Because C++ set that precedent! (Of the superset resembling and reusing what is based upon)

09-f9-11-02-9d-74-e3-5b-d8-41-56-c5-63-56-88-c0
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 19th Jun 2010 18:00
You're not getting my point though - if you saw C++ and Objective-C at the same time, both completely new to you, C++ would look more like C. This isn't just a subjective thing.


This is how you call a function in C:
myFunc(a, b, c);

This is how you call a method in C++:
obj.myMethod(a, b, c);


But in Objective-C:
[obj myMethod: a andB: b andC: c];


Clearly, C++ looks more like C - not just because that's what we know, but because it's empirically true.


Now, you think it's impossible to make a preprocessor tell the difference between C++ and C (that is, if you were to extend C into C++ using a preprocessor). So since you're so sure, I will show you how it's done:

The class declarations are easy - find 'class' keyword - so everything between the curly braces is something that needs preprocessing.

The important part is method calls. So, lets say we have this: obj.myMethod(1, 2, 3);
How do you tell this is a method call to process?

Well start by tokenizing obviously, and work through the tokens, adding them to a parse tree.
So far we have this parse tree:


So far this would be perfectly valid in C - it can just be re-assembled as 'obj.myMethod'. But then you find a '('. This tells you that the 'dot' isn't a field access, but actually a method call, so you can change it to something like this:



Now, when re-assembling it, you find a '->' instead of a '.'. This tells the preprocessor not to make it so a standard field access, but make it a function call, such as: MyClass_myMethod(obj, 1, 2, 3);


It's not difficult to do this stuff. The decision to make it look how it looks was a purely aesthetic one (perhaps to make it look more like SmallTalk) - not because they couldn't. And that's what I don't like - they wanted to extend C by mixing it with a totally different language.

"everyone forgets a semi-colon sometimes." - Phaelax
David R
21
Years of Service
User Offline
Joined: 9th Sep 2003
Location: 3.14
Posted: 20th Jun 2010 13:31 Edited at: 21st Jun 2010 01:24
Quote: "Now, you think it's impossible to make a preprocessor tell the difference between C++ and C"


That is not at all what I said


Again, every point you've made is extremely easy to say with hindsight given the respective syntax of C++. The fact that a superset should have to look like C is not some kind of a rule and you're merely expecting/wanting it because C++ does it. Objective C is called Objective C because it adds the concept of objects. It isn't called objective C because it's supposed to look exactly like C, nor does being a superset imply it has to resemble C either

Also - another really important thing you're not considering is that C itself has changed a lot over the past few years. ANSI only got its first standard out in 1989 (3 years after initial ObjC impl.). I don't know whether you've ever seen K&R C but there are a lot of oddities (in method prototypes for example) that make ObjC look much less alien once you see them. C itself was horrible looking until ANSI came along

09-f9-11-02-9d-74-e3-5b-d8-41-56-c5-63-56-88-c0
BearCDP
15
Years of Service
User Offline
Joined: 7th Sep 2009
Location: NYC
Posted: 22nd Jun 2010 07:15 Edited at: 22nd Jun 2010 07:18
I have no problem with Objective-C being what it is(so long as Objective-C has no problem with my opinion of how ugly it is, which is very ugly), but it's a tad frustrating that Apple has kept it as its native development language. There can't have been a whole lot of forces preventing them from writing the Carbon & Cocoa APIs and the iPhone SDK in C++ rather than Objective-C. It would make any Apple platform a lot more approachable to develop native apps for.

That being said, I'll be learning it anyway since Apple's obstinance has made it worth knowing.

You know what's scarier than Objective-C?

[drum roll with screeching violins]

XCODE

Check out this WIP flash game from the Global Game Jam!
David R
21
Years of Service
User Offline
Joined: 9th Sep 2003
Location: 3.14
Posted: 22nd Jun 2010 10:22 Edited at: 22nd Jun 2010 10:25
XCode was scary for the first day or so but once you get used to it is O-K. I agree though, it does have some really messed up problems (in particular when the inspector opens the last build configuration rather than the current build configuration. Caught me out loads of times)

I'm really looking forward to XCode 4 though after seeing the WWDC videos - integrated one-window UI, interface builder built-in, Git support (and a versioning timeline w/ blame view) and LLVM/Clang/LLDB by default. C++ support also seems to be improved

09-f9-11-02-9d-74-e3-5b-d8-41-56-c5-63-56-88-c0
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 22nd Jun 2010 10:59
DavidR,

Objective-C has a very thin runtime engine built into the actual object code, and as far as I'm aware, it always has. How could they possibly do that using a preprocessor?

Still, guess it comes down to opinion. I think C mixed with SmallTalk is intrinsically ugly and was a bad choice to mix such different languages.

"everyone forgets a semi-colon sometimes." - Phaelax
David R
21
Years of Service
User Offline
Joined: 9th Sep 2003
Location: 3.14
Posted: 22nd Jun 2010 11:18 Edited at: 22nd Jun 2010 11:25
Quote: "Objective-C has a very thin runtime engine built into the actual object code, and as far as I'm aware, it always has. How could they possibly do that using a preprocessor?"


By inserting calls to the runtime library and then linking against it

e.g. for calling functions / sending messages I'm guessing you would actually 'register' the methods to some kind of table kept in memory by the runtime (using a method provided by the runtime includes) and then run a lookup using the runtime when it comes to call it (provides function pointers maybe? I'm not entirely sure - but it is certainly possible because I know ObjC's part in GCC was a hacky preprocessor frontend before it was a 'real' module)

Quote: "Still, guess it comes down to opinion. I think C mixed with SmallTalk is intrinsically ugly and was a bad choice to mix such different languages."


Each to their own. I think it's ugly in a 'foreign' kind of way, but then I think every language I've ever learnt is ugly before I 'get it' [and I mean 'get it' = appreciate its history and design rationale. I'm not trying to say you don't understand the lang or anything like that]

09-f9-11-02-9d-74-e3-5b-d8-41-56-c5-63-56-88-c0
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 22nd Jun 2010 13:28
I have discovered that by changing the file extensions from .m to .mm you can make it compile both Obj-C code and C++ code. Hurrah!

"everyone forgets a semi-colon sometimes." - Phaelax
Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 22nd Jun 2010 14:04
That's what I'm doing too, it's quite a popular option that I wish Apple would make more of a standard. Personally I really like XCode, but it's not like I have a point of reference - maybe it's easier for me to just accept the way it works, never used VS or anything like that to compare it with.

OpenGLES with C++ is a great way to develop for iPhone IMO. It's honest, and avoids all that interface builder stuff.


Health, Ammo, and bacon and eggs!
BearCDP
15
Years of Service
User Offline
Joined: 7th Sep 2009
Location: NYC
Posted: 23rd Jun 2010 09:27
XCode is getting git support? That is freakin' sweet!

I just started using it at work since I'm assigned to a small 1-month project writing some C#.NET to communicate with a device over RS-232. We have a lab that I remote desktop into where another guy writes C for the RS-232 device. I just set up a --bare git repo on our intranet, and it's a breeze pushing to that repo and pulling the changes out to the lab for a quick test. Localized revision control is teh coolest thing evar.


Quote: "OpenGLES with C++ is a great way to develop for iPhone IMO. It's honest, and avoids all that interface builder stuff.
"


Have you ever written an i-App with it? That sounds like a really good reason to learn OpenGL.

Check out this WIP flash game from the Global Game Jam!
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 23rd Jun 2010 15:55
What's 'git'? Is this some kind of version control thing? Am I missing something?

"everyone forgets a semi-colon sometimes." - Phaelax
David R
21
Years of Service
User Offline
Joined: 9th Sep 2003
Location: 3.14
Posted: 23rd Jun 2010 16:24 Edited at: 23rd Jun 2010 16:26
Yeah it's version control. Extremely fast distributed version control that's very flexible (and tracks the content of files rather than filenames).

It's really good too - I highly recommend it. Extremely easy to import from SVN too. You lose some aspect of client/server redundancy (every working copy is its own commit endpoint unless hooked up with a remote server) but then SVN etc. shouldn't really be used for 'backup'

09-f9-11-02-9d-74-e3-5b-d8-41-56-c5-63-56-88-c0
BearCDP
15
Years of Service
User Offline
Joined: 7th Sep 2009
Location: NYC
Posted: 23rd Jun 2010 16:27
It's a distributed revision control system. It's managed to gain more popularity than similar systems like Bazaar and Mercurial. Basically, instead of having to set up a server to contain your subversion/perforce/CVS repo, your repo is on your local machine.

If you want to have a model similar to subversion where everyone must commit to a central location, you can set up a repo on a shared folder or a server, and then send your changes to it using a "push" command. Or alternatively, someone can be the maintainer of the central repo, disallow pushing, and only "pull" from sources they know they need to tag a release or update the master master branch (similar to trunk).

The nice thing about it is that it's really flexible with how you set up your workflow, and you get to have your own revisions on your local copy. And, if you're working on a solo project, you can just set up a single repo and it will work great. Everything just sits in a .git folder, similar to subversion, except there's no nested git folders to worry about, just that single .git. Branching is also really fast & easy. When you create a new branch, it does so fast, and when you switch branches, git reads the info in .git and changes everything in your working directory (you only have to maintain one working directory, thank god) to match the content of the bridge you've switched to. Changing back and forth between several different branches is ridiculously fast.

Here's a video of Linus Torvalds being really arrogant, but also presenting a lot of good info about git:
http://www.youtube.com/watch?v=4XpnKHJAok8

Here's the git site:
http://git-scm.com/

Check out this WIP flash game from the Global Game Jam!
JoelJ
21
Years of Service
User Offline
Joined: 8th Sep 2003
Location: UTAH
Posted: 24th Jun 2010 00:20
I've been doing iPhone and Mac development for both personal and school projects for a year now. I must agree ... Objective-C is ugly. But once you get used to the syntax it's merely a syntax change. Hardly something to turn it down for. You can adjust.

The thing I hate about Objective-C is the actual compiler. The errors and warnings it gives are completely worthless. It's so inconsistent.

I do love both the Cocoa and Objective-C Libraries. Very very well done. Memory management is a breeze in Obj-C (even without the garbage collector).

Xcode is a little frustrating, but the recently announced Xcode4 looks amazing. They're integrating it all into a one-window app. Which will be nice.

Your mother has been erased by a mod because it's larger than 600x120
Zeus
18
Years of Service
User Offline
Joined: 8th Jul 2006
Location: Atop Mount Olympus
Posted: 24th Jun 2010 07:10
Quote: "I'm yet to see any non-troll post from you

But sarcasm... from me? Surely you're joking? I don't know what you're talking about
"


*gives one loud bold HA*



I missed you Shadowtroid!
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 24th Jun 2010 14:40
Quote: "Memory management is a breeze in Obj-C (even without the garbage collector)."


Actually, that's probably one thing I do like about it. It's nice and fast, unlike garbage collection, but feels somewhat more secure than plain old new/delete. When I do C++ programming, I also tend to think about having ownership of objects, but I never really had the idea that concrete in my head, y'know? It was just a vague idea that Objective-C made quite clear. Only thing is that every object (in the Obj-C part of the language) has to be a pointer. I would have liked to make objects straight on the stack, because on the project I'm working on I'm using a large list of objects, and I'd like them to be contiguous in memory, simply because all the extra pointers do end up using up valuable space, and for caching efficiency.

"everyone forgets a semi-colon sometimes." - Phaelax
JoelJ
21
Years of Service
User Offline
Joined: 8th Sep 2003
Location: UTAH
Posted: 24th Jun 2010 23:26
Quote: "I would have liked to make objects straight on the stack, because on the project I'm working on I'm using a large list of objects, and I'd like them to be contiguous in memory, simply because all the extra pointers do end up using up valuable space, and for caching efficiency."

That's what arrays are for. ArrayLists use arrays as a backend. Shouldn't cause any problems. Putting them all on the stack would cause ... problems. Especially if you had a large list of objects.

Your mother has been erased by a mod because it's larger than 600x120
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 25th Jun 2010 00:09
Yeah but the array lists in Objective-C seem to store 'id' objects, which as far as I know are all reference types... Nevermind though, I'm using C++ for it, so I don't even care

"everyone forgets a semi-colon sometimes." - Phaelax
JoelJ
21
Years of Service
User Offline
Joined: 8th Sep 2003
Location: UTAH
Posted: 25th Jun 2010 02:57
is there really any overhead in using pointers? I'm sure there is a tiny bit. But even with a list a million, is it even noticeable?

Your mother has been erased by a mod because it's larger than 600x120
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 25th Jun 2010 04:10
It's not so much that (there is a slight overhead in having to dereference the pointer, but that's negligible unless you have to do it a lot - the size of the list doesn't matter here anyway). It more the fact that if you do it like this, the data is all spread around your RAM - the list simply holds addresses to it. This does cause problems because for every list item, you're taking up an extra 4 bytes of memory just for the pointer. Then there's cache misses - the cache stores data nearby the last access that you did, because it's nearby data that you're most likely to use. Using pointers, where data is spread all over, you get a lot of cache misses, so it's slower.

If they were value types, the list wouldn't be the number of items * 4 (the size of a pointer), but the number of items * size of the object - i.e., the object itself is inside the list. The amount of space you take up is 4 * the number of items less, and you get fewer cache misses, and there's no need to dereference the pointers. Not to mention that you don't need to manage the memory of every single object!

"everyone forgets a semi-colon sometimes." - Phaelax

Login to post a reply

Server time is: 2024-11-23 13:21:54
Your offset time is: 2024-11-23 13:21:54