Thread: Moving code from C to C++

  1. #1
    Elixia
    Guest

    Moving code from C to C++

    I'm slowly moving from C to C++, and doing this by slowly moving my projects into C++ format. In my current C file I have the following:

    struct tag_embdata
    {
    struct tag_mod_name *Prev;
    struct tag_mod_name *Next;
    int id;
    char *Name;
    } *emb_head = NULL, *emb_tail = NULL;


    I scan through this linked list regularly searching for various IDs and converting Names to IDs. This list is allocated using malloc, and each Name entry (which is an ASCII null terminated string) is also allocated using malloc.

    Now the question - is there a better way of doing this in C++ ? Some people have told me to use iterators, but I don't know what these are.

    Secondly, can anyone tell me the advantages and disadvantages of using "new" over "malloc" for creating simple pointers to char (as in the Name element above) ?

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148

    Re: Moving code from C to C++

    >> Some people have told me to use iterators, but I don't know what these are.
    As Bjarne Stroustrup elaborates
    An iterator is a pure abstraction.That is,anything that behaves like an iterator is an iterator...For example the built -in type int* is an iterator for an int[].
    So nothing to worry about.

    Most of the time you can use an iterator like a pointer (inc,dec,substract two iterators,and so on).

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619

    Re: Moving code from C to C++

    Originally posted by Elixia
    I'm slowly moving from C to C++, and doing this by slowly moving my projects into C++ format. In my current C file I have the following:

    struct tag_embdata
    {
    struct tag_mod_name *Prev;
    struct tag_mod_name *Next;
    int id;
    char *Name;
    } *emb_head = NULL, *emb_tail = NULL;


    I scan through this linked list regularly searching for various IDs and converting Names to IDs. This list is allocated using malloc, and each Name entry (which is an ASCII null terminated string) is also allocated using malloc.

    Now the question - is there a better way of doing this in C++ ? Some people have told me to use iterators, but I don't know what these are.

    Secondly, can anyone tell me the advantages and disadvantages of using "new" over "malloc" for creating simple pointers to char (as in the Name element above) ?
    OK, here's some quick thigs:

    1) All structs in C++ are really classes. Every struct/class has a constructor and destructor. If you allocate space with malloc, you create a place for the struct but you DON'T call the constructor. On a struct with no functions, this is OK (but undesirable), on something with functions, it could be disasterous.

    2) C++ has a built in linked list, called std::list. Get a good book on the C++ standard library if you need one (I'd recommend "The C++ standard Library, A tutorial and Reference" by Nicolai Josuttis). That's the fastest way to start using the STL effectively.

    Overall, when coming from C, you tend to have a few bad habits:

    * Poor encapsulation/poor OO in general. Study the principles of object-oriented programming (search Google, tons of OO design references are widely available). Sometimes people use too little OO, sometimes too MUCH (there are some people who tend to overuse inheritance).

    * Tendency to create own structures like binary search trees, linked lists, etc. The STL does this kind of thing for you, and their code is very efficient and safe.

    * Overuse of arrays and pointers. You should prefer std::vector and smart pointers like boost:shared_ptr (get Boost for that)

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    While Cat is right about prefering STL containers, and algorithms (although there are sometimes good reasons for using your own), I'd have to say your idea of converting old projects to C++ is a good way to learn.

    I'd advise actually rewriting them though. That way you'll be able to start with the 'right' design to begin with.

    For example, this is one way you might want to think about implementing the linked list (a lot different from a C implementation):

    Code:
    template<typename T>
    class linked_list
    {
    public:
        typedef T* iterator; // Your iterator.
    
        // Constructor, accessors, etc.
    private:
        struct node
        {
            node *next_, *prev_;
            T* data_;
        };
    
        node *begin_, *end_;
    };
    Which reminds me of something else. While OOP is one of the great features of C++, so are templates, for many reasons. Once your comfortable with the basics of them, you might want to look into template metaprogramming - using templates as compile-time programs, and such.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Moving Lapack++ dependent code to windows
    By confuted in forum C++ Programming
    Replies: 1
    Last Post: 10-16-2007, 10:47 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM