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) ?
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
Quote:
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).
Re: Moving code from C to C++
Quote:
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)