Thread: need help with extern pointers.

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Make a struct, stuff all the common arguments in there and pass it around. Unless there's a real need for global variables, I'd avoid it.
    There are things that need global variables, but this isn't it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Just bear in mind that if you are actually interested in performance within your ray-tracer, using local variables or parameters is likely to produce better code, since the compiler must assume that global variables may be affected by any function call you make (except for functions that the compiler decides to inline and thus can decide that this portion of code doesn't modify certain variables).

    It does sound like those definitions are suitable for storing in a struct that is passed as one const pointer to all functions (that need them).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you absolutely must use globals, then a few rules will help matters.

    1. Use a decent namespace, like prefixing all globals with "g_". If you use something terse like 'obj', then you're going to have surprises. One unfortunate project from my past managed to make a global variable called 'i'. Talk about chaos.

    2. Put them all in one place, so that you can instantly see the scope of the problem should you ever want to port the code somewhere else.

    3. Use gcc and compile with the -Wshadow option, to tell you about the case you highlighted in this thread, namely having a global and a parameter with the same name. But naming rules will also help.

    4. Keep the numbers down. Less than 10 is a lot easier to manage than say more than 100.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  2. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM
  3. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  4. extern symbols, what do these mean exactly?
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2002, 03:14 PM
  5. extern keyword and structures
    By GuitGentlyWeeps in forum C Programming
    Replies: 2
    Last Post: 01-30-2002, 07:02 AM