Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Cromulent

macrumors 604
Original poster
Oct 2, 2006
6,810
1,100
The Land of Hope and Glory
Most of the books I have are written with C++ (the physics, AI, maths and graphics books are particularly bad) and I would like to port the code over to straight C so that I can use it in my programs.

I have a couple of problems though. What is the C equivalent to a namespace? I know it is an OOP construct but can I safely ignore it? Also how do I port something which uses operator overloading? And is there a built in Vector variable in C++?I see some code that uses it, or is it just using typedef to assign floats (or ints) to the name vector?

I'll probably have a few more questions, I have not had much of a chance to look.
 

garethlewis2

macrumors 6502
Dec 6, 2006
277
1
I'm slightly confused by your post.

Are you already proficient at C or are you just starting because you believe it is more hardcore than C++?

Anyway. There aren't in any sense of the word Namespaces in C. There are Namespaces, but you can't access them, they are used by the comiler. This is why you can have a function and a variable with the same name. To stop function clobbering you must make all functions in a compilable unit static. This way, they can only be seen in that file and therefore, only in that object file.

There is no such thing as operater overloading in C, and definately no such thing as function overloading.

There is no builtin Vector variable in C++, but there are standard libraries which define vector classes and structures. You will need to build these yourself. In most cases they will be simple structs, or even simpler, double arrays.

The code was probably written in C++ to make it easier to maintain. You can definately write this in C, I have done it in the past, but you end up doing a gigantic amount of work. Why don't you just use the C++ code?
 

Cromulent

macrumors 604
Original poster
Oct 2, 2006
6,810
1,100
The Land of Hope and Glory
I'm slightly confused by your post.

Sorry I should have been more clear.

Are you already proficient at C or are you just starting because you believe it is more hardcore than C++?

I wouldn't say I'm proficient in C but I'm certainly much better at it than I am at C++ hence the reason I want to port it.

Anyway. There aren't in any sense of the word Namespaces in C. There are Namespaces, but you can't access them, they are used by the comiler. This is why you can have a function and a variable with the same name. To stop function clobbering you must make all functions in a compilable unit static. This way, they can only be seen in that file and therefore, only in that object file.

I'm aware of the fact that there is no definition of a namespace within C code, I was just wondering if when porting C++ code to C is there anything I need to do special if the C++ code defines a namespace or can I just simply strip it out of the code?

There is no such thing as operater overloading in C, and definately no such thing as function overloading.

Oki dokie.

There is no builtin Vector variable in C++, but there are standard libraries which define vector classes and structures. You will need to build these yourself. In most cases they will be simple structs, or even simpler, double arrays.

I guess I can just write a simple library to handle this then.

The code was probably written in C++ to make it easier to maintain. You can definately write this in C, I have done it in the past, but you end up doing a gigantic amount of work. Why don't you just use the C++ code?

Because I don't know C++.

Thanks for your help :).
 

garethlewis2

macrumors 6502
Dec 6, 2006
277
1
You can strip out the namespaces, but then you have to sure that there aren't any functions that have the same name in different compilable units otherwise the linker is going to be ecstatic about sorting that out. If there is say a class called utilities in namespace math.game. and another utilities class with functions in file.game., and they both have functions with the same name, then you will need to go through all the source code where each is referenced and change the name, (refactor the code) to use the newer version of the function. That is going to be painful.
 

MrStevieP

macrumors newbie
Feb 27, 2008
22
0
Perhaps learn C++?

I have to agree with garethlewis2, this is going to be very painful and you'll spend alot of time grapalling with the compiler. No only this, the effort you'll need to put in to figuring out the nuances of the code will be big enough that it will probably be easier and quicker to just learn the basics of C++, which you'll need to do anyway for the porting process. You could then simply reuse the code you already have.

Maybe start with something like http://www.cplusplus.com/doc/tutorial/
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
Hi

You could get round the namespace issue by replacing :: with _. For example, say there are 2 (3d) vector classes in physics::vector and geometry::vector, then you could define 2 structs in C called physics_vector and geometry_vector. You'd still have to change any class methods to procedural functions though.

It's common practice for operator overloads to be defined as functions and not class methods (exceptions would be unitary operators). So they would be easy to translate.

Also, it sounds as though they are using the C++ standard library which defines a vector template. This has nothing to do with mathematical vectors. std::vector is a container, a little bit like a C array. Writing your own version would be tricky because it is a template and so can be instantiated to be a container for any type. You can have, for example, std::vector<int>, std::vector<std::string>, srtd::vector< std::vector<int> > and so on. If they are using std::vector, then they most probably are using a heap load of other standard library stuff too. 'vector' might just be the tip of the iceberg.

I would agree with the other posters though, you'd could end up putting an awful lot of effort into this project. Why not use it to learn C++. If you're proficient in C, then the step up to C++ isn't too bad, and more importantly, it would be a positive step – you'd be learning so much in the process.

b e n
 

mward333

macrumors 6502a
Jan 24, 2004
574
33
Another vote to learn C++

I have to agree that learning C++ is definitely worthwhile.

In fact, there are several good books available for people who know C but want to expand their knowledge to C++. People have a wide variety of opinions about such things, so I won't get into that discussion here (about which book is the best for you).

Whenever I am in a dilemma like yours (should I port the code? or, work a little harder to learn a slightly new language?), I'm usually willing to learn the new language if I think I might use it again.

In your case, if you already know C, then you can learn some of the basics of C++ very, very easily, and just keep adding to your C++ skill and knowledge as you go by. For the most part (take this with a grain of salt), the C code that you already know and love will run on a C++ compiler anyway, so don't be afraid to take a step upwards to C++. You'll be happy that you did.

I use C++ as a workhorse. It is my bread and butter.
 

Cromulent

macrumors 604
Original poster
Oct 2, 2006
6,810
1,100
The Land of Hope and Glory
Well I guess you guys are right. Learning C++ is obviously benificial in the long run, I was just hoping that I could put off the quagmire of OO programming for a little while.

Still, I'm not sure how C++ fits into my future plans.
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
Look at this analogy, you know German, and have a book you want to read that is only in Dutch.

You want to translate the book to German, rather than just learning Dutch which will mean you don't make any errors ;).
 

mward333

macrumors 6502a
Jan 24, 2004
574
33
Look at this analogy, you know German, and have a book you want to read that is only in Dutch.

You want to translate the book to German, rather than just learning Dutch which will mean you don't make any errors ;).

Here's a different analogy for you, which will make you feel better.

You know the language of a country (let's say USA) that speaks English, but you notice that there is a new portion of the country (let's say New York) where they do speak English, and they also have some special English words that aren't available in the rest of the country.

In this case, you already know C, and your C code should work (99.9999 percent of the time) without any changes in C++. So just run your C code through the C++ compiler, and begin to learn the extra things in C++ as you go.

You already have C mastered, so moving to C++ will not be so difficult for you (as compared to a beginner). Most people view C as a subset of C++ (which is, again, 99.9999 percent correct).

Good luck. We're always here to help if you have questions.
 

cubist

macrumors 68020
Jul 4, 2002
2,075
0
Muncie, Indiana
And now for a differing viewpoint.

C++ was originally implemented as a translator, which read C++ source and output C source. Therefore any C++ program can be mechanically translated to a C program; there is no inherent functional superiority of the language.

C++ is a notoriously complicated and ambiguous language. C++ programs are famous for horrible memory leaks, and are more difficult to debug.

If you look at the generated assembly language, C++ compilers produce very complex output code, with lots of indirection. This means that optimization is not as good as for C (altho, if performance is your main concern, you'd be coding in assembly in the first place, or possibly Fortran).

Operator and function name overloading seem elegant but, in practice, lead to code which is difficult to read and maintain.

Always picture yourself as the person who has to sight-read code which has not been touched in five years, ten years, fifteen years. C++ is the Pascal of the 90s. COBOL is still here; Pascal is gone. My expectation is, twenty years from now, C will still be here; C++ will be gone.

The most telling thing is the name: C++. That post-increment means increment the language, but use the original language.
 

garethlewis2

macrumors 6502
Dec 6, 2006
277
1
I don't want to sound offensive, but did you just happen to step through a time portal from 1992 into 2008?

You could not take C++ code today and translate it into C using a translator tool. You could do that with the very first version. But we're talking 15-20 years ago. Namespaces didn't exist, exceptions didn't exist.

I would love to see what happens in 20 years. I should still be around, and I guarantee that C++ won't be going anywhere. There are too many applications written in it. And Microsoft have a very big stake in it.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
Hi Cubist
Yes, it is possible to write C++ code that is terrible, difficult to read and horrendous to debug. But it isn't mandatory to do so. In fact only a language that is as rich and deep as C++ could claim such abilities.

COBOL?!!?

b e n
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.