Curse of pointers!



  • I do not intend to start flame-war, but it look like this is where it will eventually head toward.

    I have been coding in java for most of my life. I have seen that anything that can be done in C can be done in Java with more elegant code. Still there are people who keep on touting about C as if that is best thing to hit the industry.

    I have taken one class in C, which I quickly abandon in favor of java. I am certain that C is mother of all languages, but what I try to understand is why must all C programmers insist on using so much pointer code in their source code.

     [color=#C80046]C[/color][color=#C8003F] [/color][color=#C70038]i[/color][color=#C80031]s[/color][color=#C8002A] [/color][color=#C80023]a[/color][color=#C7001C] [/color][color=#C80015]c[/color][color=#C8000D]r[/color][color=#C80005]u[/color][color=#C80100]e[/color][color=#C80900]l[/color][color=#C81100] [/color][color=#C81800]l[/color][color=#C81F00]a[/color][color=#C82600]n[/color][color=#C82C00]g[/color][color=#C83300]u[/color][color=#C83900]a[/color][color=#C83F00]g[/color][color=#C84500]e[/color][color=#C84B00].[/color][color=#C85100] [/color][color=#C85600]T[/color][color=#C85C00]h[/color][color=#C76200]e[/color][color=#C86800]r[/color][color=#C86D00]e[/color][color=#C77300] [/color][color=#C87900]i[/color][color=#C87F00]s[/color][color=#C88500] [/color][color=#C88B00]s[/color][color=#C79100]o[/color][color=#C79700]m[/color][color=#C89E00]e[/color][color=#C7A400] [/color][color=#C8AB00]p[/color][color=#C8B200]o[/color][color=#C7B900]r[/color][color=#C8C100]t[/color][color=#C6C800]i[/color][color=#BEC700]o[/color][color=#B6C800]n[/color][color=#AFC800] [/color][color=#A8C800]o[/color][color=#A0C800]f[/color][color=#99C800] [/color][color=#93C800]t[/color][color=#8CC800]h[/color][color=#85C800]e[/color][color=#7EC800] [/color][color=#78C800]p[/color][color=#71C800]o[/color][color=#6AC700]p[/color][color=#63C800]u[/color][color=#5DC700]l[/color][color=#56C800]a[/color][color=#4FC800]t[/color][color=#48C800]i[/color][color=#41C800]o[/color][color=#39C800]n[/color][color=#32C700] [/color][color=#2AC800]t[/color][color=#22C800]h[/color][color=#1AC800]a[/color][color=#12C800]t[/color][color=#09C800] [/color][color=#00C700]l[/color][color=#00C809]o[/color][color=#00C811]v[/color][color=#00C81A]e[/color][color=#00C822]s[/color][color=#00C82A] [/color][color=#00C832]c[/color][color=#00C839]r[/color][color=#00C840]u[/color][color=#00C848]e[/color][color=#00C84F]l[/color][color=#00C856]t[/color][color=#00C85C]y[/color][color=#00C863] [/color][color=#00C86A]i[/color][color=#00C871]n[/color][color=#00C877]f[/color][color=#00C87E]l[/color][color=#00C885]i[/color][color=#00C88B]c[/color][color=#00C792]t[/color][color=#00C799]e[/color][color=#00C8A0]d[/color][color=#00C8A7] [/color][color=#00C8AF]o[/color][color=#00C8B6]n[/color][color=#00C8BE] [/color][color=#00C8C6]t[/color][color=#00C1C8]h[/color][color=#00BAC7]e[/color][color=#00B2C8]m[/color][color=#00ABC8].[/color][color=#00A4C8] [/color][color=#009EC8]I[/color][color=#0097C8] [/color][color=#0091C8]a[/color][color=#008BC7]m[/color][color=#0085C7] [/color][color=#007FC8]n[/color][color=#0079C7]o[/color][color=#0073C8]t[/color][color=#006EC7] [/color][color=#0068C7]o[/color][color=#0062C8]n[/color][color=#005CC8]e[/color][color=#0057C8] [/color][color=#0051C7]o[/color][color=#004BC8]f[/color][color=#0045C7] [/color][color=#003FC7]t[/color][color=#0039C8]h[/color][color=#0033C8]o[/color][color=#002CC8]s[/color][color=#0026C8]e[/color][color=#001FC7],[/color][color=#0018C8] [/color][color=#0011C8]s[/color][color=#000AC8]o[/color][color=#0002C8] [/color][color=#0500C7]I[/color][color=#0D00C8] [/color][color=#1500C7]d[/color][color=#1C00C7]e[/color][color=#2300C8]c[/color][color=#2A00C8]i[/color][color=#3100C8]d[/color][color=#3800C8]e[/color][color=#3F00C8] [/color][color=#4600C8]t[/color][color=#4C00C8]o[/color][color=#5300C8] [/color][color=#5A00C8]s[/color][color=#6000C8]t[/color][color=#6700C8]i[/color][color=#6E00C8]c[/color][color=#7500C8]k[/color][color=#7C00C8] [/color][color=#8300C8]t[/color][color=#8A00C8]o[/color][color=#9100C8] [/color][color=#9900C8]j[/color][color=#A100C8]a[/color][color=#A900C7]v[/color][color=#B100C7]a[/color][color=#BA00C8].[/color]




  • Java uses pointers all over the place. You just don't notice them as much. Whenever you pass an object to a method, you've just passed a pointer by value. This has subtle, but important, distinctions from passing by reference.

    Elegance is often in the eye of the beholder. Java is a fairly verbose language, not to mention the enforced object orientation. For many tasks, it's more convenient and efficient to use a language that is "closer to the metal," like C.

    You've got a significant runtime overhead with java on most platforms. C is very minimal.

    On most modern desktop or server platforms (i.e., not embedded, mobile, etc), the system is C-based, so it's usually easier to interface with the rest of the environment using C than with Java.



  • @boomzilla said:


    Java uses pointers all over the place. You just don't notice them as much. Whenever you pass an object to a method, you've just passed a pointer by value. This has subtle, but important, distinctions from passing by reference.

    Elegance is often in the eye of the beholder. Java is a fairly verbose language, not to mention the enforced object orientation. For many tasks, it's more convenient and efficient to use a language that is "closer to the metal," like C.

    You've got a significant runtime overhead with java on most platforms. C is very minimal.

    On most modern desktop or server platforms (i.e., not embedded, mobile, etc), the system is C-based, so it's usually easier to interface with the rest of the environment using C than with Java.

    The overhead I see is for DB layer. Maybe one reason is you're looking at it from system programming viewpiont. I am looking from business application programmer view point. I am fairly sure that java internally uses a lot of bells and whistles that are under the hood. As business programmer, I need to know how to calculate interest on loans, so I need to pay attention to client's business model. As a systems programmer, I need to know windows internals. Do you agree?



  • @Nagesh said:

    The overhead I see is for DB layer. Maybe one reason is you're looking at it from system programming viewpiont. I am looking from business application programmer view point. I am fairly sure that java internally uses a lot of bells and whistles that are under the hood. As business programmer, I need to know how to calculate interest on loans, so I need to pay attention to client's business model. As a systems programmer, I need to know windows internals. Do you agree?

    Yes, C is certainly more systems programming oriented. I was simply answering the question of why someone would choose C. Obviously, choice of language depends on what you're doing.

    Java can be convenient, since it takes care of some housekeeping that C forces you to do (e.g., garbage collection). And depending on the task, that large run time overhead can be beneficial, since there's generally a lot more that comes with Java than with the C runtime library. There's also the availability and suitability of external libraries to consider.

    Of course, personal preference, experience and knowledge play a big part. Some tasks can be reasonably done in either language.



  • <marginally offtopic>

    @boomzilla said:



    Java can be convenient, since it takes care of some housekeeping that C forces you to do (e.g., garbage collection).

     

    I just realized why I have a problem with the term "garbage collection."  My general approach to programming is to produce no garbage in the first place. Why would I need to collect it?

    </marginally offtopic>

    Ok, more seriously: I think the better comment is

    @boomzilla said:

    Of course, personal preference, experience and knowledge play a big part. Some tasks can be reasonably done in either language.

    When two tools can perform the same task, but the user is more adept with one tool than the other, that is the better choice.

    Personally, my mind "works" better with C than it does with Java, or even things like model-based programming.  I can still remember quite vividly the moment that I had my moment of epiphany and understood pointers. Yes, pointers can be dangerous and cumbersome, but that's a characteristic common to extremely powerful tools.



  • @too_many_usernames said:

    My general approach to programming is to produce no garbage in the first place.
     

    In what world is this remotely possible?



  • @dhromed said:

    @too_many_usernames said:

    My general approach to programming is to produce no garbage in the first place.
     

    In what world is this remotely possible?

    I never said it was a practical approach for any given situation.

    That said, there are numerous situations in which the concepts which gave rise to the concept of garbage protection are actually irrelevant. In certain embedded (or even desktop applications of a certain type), for instance, everything has static allocation so there is inherently no possibility of that type of failure.

    I would also argue that it's actually possible to write bug-free code that does employ dynamic allocation if you adhere to a philosophy of "create no garbage." Garbage here doesn't mean "allocated objects" but, rather, poorly designed architecture or implementation that makes bad assumptions about what a pointer represents. I think the main conceptual failure is that people treat pointers as "identity" of an object, rather than what they are, which is just an address in which an entity may or may not reside, and not even if that entity is the one you think it is.

    In general I'd say that problems with pointers are due to poor understanding of an application's overall architecture and incorrect assumptions about the validity of pointers at any particular time.

    I guess some of this rambling is mostly due to the fact that I work in the safety-critical embedded world, and that world conditions you pretty quickly to make no assumptions whatsoever about your data - you have to be very explicit (at least in design) about how all the data fits together. I guess maybe I think that GC is a bandaid that reduces the severity of certain types of design or implementation failure, and I'm the type of engineer that would rather fix the root cause than the symptom.



  • You can use what program language you prefer (so if you prefer Java, you should use Java), but I like to use C. I think C is very good, and pointers in C are very useful. Bitwise operations are also very useful, and so is the C preprocessor. And I like to make a book of the program, too.



  • @zzo38 said:

    And I like to make a book of the program, too.
    Um. What?



  • @boomzilla said:

    @zzo38 said:
    And I like to make a book of the program, too.
    Um. What?
    Did you know that Knuth also like to make a book of the program? I have the book called "TeX: The Program" and "METAFONT: The Program". However, if I publish a book I would also like to include the DVD in the back of the book, too.



  • @zzo38 said:

    @boomzilla said:
    @zzo38 said:
    And I like to make a book of the program, too.
    Um. What?
    Did you know that Knuth also like to make a book of the program? I have the book called "TeX: The Program" and "METAFONT: The Program". However, if I publish a book I would also like to include the DVD in the back of the book, too.

    I am trying hard to make sense of this post.



  • @Nagesh said:

    @zzo38 said:

    @boomzilla said:
    @zzo38 said:
    And I like to make a book of the program, too.
    Um. What?
    Did you know that Knuth also like to make a book of the program? I have the book called "TeX: The Program" and "METAFONT: The Program". However, if I publish a book I would also like to include the DVD in the back of the book, too.

    I am trying hard to make sense of this post.

    What part is difficult to you?



  •  may i introduce you to std::shared_ptr<Typename T>? :D

     

    exception safe reference counted smart pointers.  ref count hits zero, delete gets called.

     



  • @Nagesh said:

    I do not intend to start flame-war, but it look like this is where it will eventually head toward.

    I have been coding in java for most of my life. I have seen that anything that can be done in C can be done in Java with more elegant code. Still there are people who keep on touting about C as if that is best thing to hit the industry.

    I have taken one class in C, which I quickly abandon in favor of java. I am certain that C is mother of all languages, but what I try to understand is why must all C programmers insist on using so much pointer code in their source code.

     C is a cruel language. There is some portion of the population that loves cruelty inflicted on them. I am not one of those, so I decide to stick to java.

    Nagesh --
    You must understand that a large number of programers have an almost fanatical adherence to their language of choice as the best solution to every problem.

    Personally, I don't like Java.  I will not ridicule you for using it, as for the most part almost any programming language can be used (with sufficient effort) to do anything another language can do.
    But there are specific instances where one language or family of languages is superior to others.

    For systems programming, close to the metal, Assembly should normally give you the fastest implementation for example.
    You lose somewhat on maintainability and portability, but you're as close to bare metal as possible.

    C gives you a little more from a maintainability standpoint, as well as a few more built in housekeeping and general purpose items to make programming easier.

    For Business applications you can go with C#, VB, Java, etc.  You're most familiar with Java and could probably develop code that performs just as well as C# code from another developer with equivalent C# skills.  I work in VB.NET.  I have some real C# guru's in my group, as well as some Java novices.  My VB apps perform just as well as the C# apps (better more often than not, we have had "code-offs") and leave the Java guy's stuff so far behind it's not even funny.  Not because VB is better than C#, not because VB is better than Java, but because I know VB.

    And, for what it's worth, C is NOT the mother of all languages.  Basic predate's C.  Cobol, BASIC, LOGO, Lisp, Algol, SNOBAL, Pascal and Forth all predate C.  There are even rumours that C was intended as an inside joke at Bell Labs, created as a jab at CPL and or BCPL.

    So don't get too upset when someone says "you should be using what I use, because it's better".  It may be better for them, but that doesn't mean it's better for you.

    And, to put it all in perspective, the very first compiled programming language for digital computers was AutoCode, which bore strong resemblance to Dartmouth BASIC.
    So. . . . .



  • @Kazan said:

     may i introduce you to std::shared_ptr<Typename T>? :D

    exception safe reference counted smart pointers.  ref count hits zero, delete gets called.

    I think that is in C++, not C. (I do not use C++)


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.