+ Reply to Thread
Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: Searching and Inserting: Arrays vs Linked Lists

  1. #11
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Searching and Inserting: Arrays vs Linked Lists

    His issue with macros is pretty simple: they can avoid type checking, which makes them far less safe. That said, he strongly encourages #define for things like include guards. C++ was designed to make macros almost completely unnecessary, which is my biggest problem with wxWidgets.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #12
    outsid3r's Avatar
    outsid3r is offline Programming God
    Join Date
    Jul 2008
    Posts
    621
    Rep Power
    19

    Re: Searching and Inserting: Arrays vs Linked Lists

    Well said, that's exactly where C++ aims.

  4. #13
    TkTech's Avatar
    TkTech is offline The Crazy One
    Join Date
    Jun 2006
    Location
    Canada
    Posts
    1,412
    Blog Entries
    1
    Rep Power
    31

    Re: Searching and Inserting: Arrays vs Linked Lists

    The name flew over my head in an incredibly daft way. Anyways, what do you say WingedPanther to him not only liking but recommending wxWidgets? And macro's will always, always be required. Why would you add overhead to a compiled program when it could be done in the preprocessor stages? Ex: Debug macros, varadic macros, ect..,

  5. #14
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Searching and Inserting: Arrays vs Linked Lists

    One of the things I like about Bjarne is that he doesn't speak in absolutes. I think he likes wxWidgets because it was one of the first cross-platform GUI toolkits, but there are many others out there now.

    I think macros have a place, but they should be used with caution.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #15
    outsid3r's Avatar
    outsid3r is offline Programming God
    Join Date
    Jul 2008
    Posts
    621
    Rep Power
    19

    Re: Searching and Inserting: Arrays vs Linked Lists

    In my opinion macros are really a great feature both in C and C++, but many things changed in C++ and we don't need to use macros excessively like in C. The main advantage that C++ can take from macros are definitions like natural or mathematical constants, like:

    [highlight=c++]
    #define PI = 3.141592653589793238f
    #define SPEED_OF_LIGHT = 299792458
    #define SECONDS_PER_DAY = 86400
    [/highlight]

    and not much more (besides standard doings like protection from re-inclusion, pragmas, conditional compilation for multi-platform code, debugging, etc.)

    Like you said, of course that macros are required and they are a really great feature of C and C++ but it's non-sense to use macros like we used in C to do things like:


    Generic code to return the max value:

    [highlight=C++]
    #define GET_MAX(a, b) (a > b ? a : b)
    [/highlight]

    This kind of things are sometimes used in C but should be really avoided in C++, we should use templates instead which are much more powerful, generic, specialized and safer to use.


    An example of a function specialized only for int, float and string:

    [highlight=C++]
    template<typename T>
    T get_max(const T& first, const T& second);


    template<>
    int get_max(const int& _F, const int& _S)
    {
    return _F > _S ? _F : _S;
    }


    template<>
    float get_max(const float& _F, const float& _S)
    {
    return _F > _S ? _F : _S;
    }


    // Returns the biggest string
    template<>
    string get_max(const string& _F, const string& _S)
    {
    return _F.size() > _S.size() ? _F : _S;
    }
    [/highlight]


    For variadic macros i don't see any use in C++ but in C it's ok, we don't need to rely on VA_ARGS in C++, i prefer to use a function that for receive a random number of params uses a list or other kind of generic dynamic array.
    Last edited by outsid3r; 09-20-2009 at 09:49 AM.

  7. #16
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Searching and Inserting: Arrays vs Linked Lists

    I would implement:
    Code:
    #define PI = 3.141592653589793238f
    #define SPEED_OF_LIGHT = 299792458
    #define SECONDS_PER_DAY = 86400
    as
    Code:
    const double PI = 3.141592653589793238f
    const int SPEED_OF_LIGHT = 299792458
    const int SECONDS_PER_DAY = 86400
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  8. #17
    outsid3r's Avatar
    outsid3r is offline Programming God
    Join Date
    Jul 2008
    Posts
    621
    Rep Power
    19

    Re: Searching and Inserting: Arrays vs Linked Lists

    There is nothing wrong with your representation, and i would even prefer it to define non natural or mathematical constants, but for really natural and mathematical constants i think that i may prefer defines, like you can see in some libraries like math3d.h for example.

    EDIT:

    I found very recently in a book named "effective c++" some information about the advantage of using const representations to define constants instead of using defines, i think that it's worth mentioning:


    "This Item might better be called "prefer the compiler to the preprocessor," because #define is often treated as if it's not part of the language per se. That's one of its problems. When you do something like this,

    #define ASPECT_RATIO 1.653

    the symbolic name ASPECT_RATIO may never be seen by compilers; it may be removed by the preprocessor before the source code ever gets to a compiler. As a result, the name ASPECT_RATIO may not get entered into the symbol table. This can be confusing if you get an error during compilation involving the use of the constant, because the error message may refer to 1.653, not ASPECT_RATIO. If ASPECT_RATIO was defined in a header file you didn't write, you'd then have no idea where that 1.653 came from, and you'd probably waste time tracking it down. This problem can also crop up in a symbolic debugger, because, again, the name you're programming with may not be in the symbol table.

    The solution to this sorry scenario is simple and succinct. Instead of using a preprocessor macro, define a constant:

    const double ASPECT_RATIO = 1.653;

    This approach works like a charm. There are two special cases worth mentioning, however.


    First, things can get a bit tricky when defining constant pointers. Because constant definitions are typically put in header files (where many different source files will include them), it's important that the pointer be declared const, usually in addition to what the pointer points to. To define a constant char*-based string in a header file, for example, you have to write const twice:

    const char * const authorName = "Scott Meyers";

    For a discussion of the meanings and uses of const, especially in conjunction with pointers, see Item 21.

    Second, it's often convenient to define class-specific constants, and that calls for a slightly different tack. To limit the scope of a constant to a class, you must make it a member, and to ensure there's at most one copy of the constant, you must make it a static member:

    class GamePlayer {
    private:
    static const int NUM_TURNS = 5; // constant declaration
    int scores[NUM_TURNS]; // use of constant
    ...
    };

    There's a minor wrinkle, however, which is that what you see above is a declaration for NUM_TURNS, not a definition. You must still define static class members in an implementation file:

    const int GamePlayer::NUM_TURNS; // mandatory definition;
    // goes in class impl. file

    There's no need to lose sleep worrying about this detail. If you forget the definition, your linker should remind you."

+ Reply to Thread
Page 2 of 2 FirstFirst 12

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Help with inserting into a linked list (C)
    By Mt570 in forum C and C++
    Replies: 6
    Last Post: 10-01-2009, 12:02 AM
  2. Linked Lists
    By chili5 in forum Java Tutorials
    Replies: 2
    Last Post: 09-01-2009, 02:10 PM
  3. Intermediate Linked Lists
    By WingedPanther in forum C Tutorials
    Replies: 5
    Last Post: 12-30-2008, 09:34 AM
  4. Linked Lists
    By Andrew.Anderson.2008 in forum C and C++
    Replies: 1
    Last Post: 06-27-2008, 09:39 AM
  5. questions about linked lists
    By jkurth in forum Java Help
    Replies: 0
    Last Post: 11-10-2007, 04:33 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts