Const in C

Started by
12 comments, last by outRider 19 years ago
After having worked in C++ for a few years, I've moved back to C for my latest project. I've been developing the project for the past 4 months and I've got a solid code base (~6000 lines atm). Now that my code is getting more complex in certain areas, I'm finding const qualification to be extremely exasperating. In C++, I would often use constant references to pass objects that I didn't want a function to change. I've been trying to do a similar thing in C, passing with constant pointers to structures if I don't want the function to change them. However, if the structure contains pointer to other structures, the data within those subsidiary structures remains mutable. So the data ends up being only partially constant. If I want fully constant structures, I have to make special versions of the parent structures that qualify the children as constant. I'm never going to do this because the structure would become unusable in non-constant situations, so I've had to live with partial constants that document the code but don't actually ensure consistency. Unfortunately, this has led to a landslide of problems. With tangled dependency chains between structures, one member variable that should be constant but isn't leads to other structures becoming non-const to remain compatible. The structures are no longer interchangable like they ought to be. I can't simply slap a const qualifier on a structure instance and expect it to be entirely immutable. I'm guessing my C++-based methods are just plain incompatible with C, in which case I have a couple options. I can rewrite the engine in object-oriented C++ (I'm not going to do this) or I can learn how to use const in a C friendly fashion. So here's my question: how do experienced C programmers deal with const? Do they use it to the same degree as C++ programmers do? Is it used for implicit documentation and function contracts like it is in C++? Have I just gone overboard with const? Finally, is it valid to cast from const to non-const in C (I get warnings but not errors, so I'm curious as to whether its simply illogical but guaranteed to succeed anyway)? Its funny to think that all my problems could be solved if I simply ignored the const keyword.
Advertisement
Out of curiosity, why did you go to C from C++?
C++ is so packed with features that when I tried to remain true to its paradigm, it really bogged me down and I ended up not finishing anything. C seemed conceptually simpler, so I made the switch. It was also after reading "The Timeless Way of Building" which seemed to solidify my thinking that C might be a more "timeless" language than C++ (which is probably more personal feeling than hard truth). Another reason might be that C has a deeper and stronger history.
i hope i'm not completely unhelpful when asking this but: are you using C89 or C99?
i think there is a difference between the two even with const.

Beginner in Game Development?  Read here. And read here.

 

Yeah C99. I guess it might be a good idea to check out some old C versions for some context on the const qualifier.
Const is viral. there is nothing you can do about it if your using legacy or third party code. If its your own code then you can design better:)

Atleast in C++ you can use overloading to provide const and non const versions.

As far as not using C++ because it has too many features, thats akin to saying your going back to a ford focus because the porche has too many features. Its just a sillly thing to say. Nothing forces you to use the "advanced" C++ features. In fact many places don't use stl, templates or exceptions in C++ stritcly beacuse they make the code harder for new hires to understand. Note, that's not a view I really share:)

Cheers
Chris
CheersChris
Why not write C-style C++?

Then you get the C++-style const you're used to. There's noone forcing you to use classes and inheritance in C++...

Quote:Finally, is it valid to cast from const to non-const in C (I get warnings but not errors, so I'm curious as to whether its simply illogical but guaranteed to succeed anyway)?


I expect it's valid wherever a const_cast would be in C++.
this also might be a pain but pulling a page from choillda's book. maybe you could make a regular struct and a const version of your struct and use a #define to differeniate between the two.

it's a sucky idea but works..... i think.

Beginner in Game Development?  Read here. And read here.

 

chollida1: I see your point and I don't disagree entirely. But C++ caters to a certain type of programming. If you don't write code in that manner, you're really using the wrong language. For instance, C provides designated initializers while C++ doesn't. I'm guessing thats because C++ isn't designed around structs; they're only there to remain compatible with C. C also has a different casting method than C++. You can do implicit void pointer casts in C while AFAIK you can't in C++. So its not that C++ has more features, it just that it has different features. (Well okay it has more, but only because it needs more).

me22: I've never done a const_cast in C++, so I don't know if its safe there either. I guess I ought to just read through the standard.

Alpha_ProgDes: Yeah I was considering something like that, but it just seems kind of unmaintainable. Its an interesting idea though.

Thanks for everyone's input - its appreciated.
Quote:Original post by dcosborn
chollida1: I see your point and I don't disagree entirely. But C++ caters to a certain type of programming. If you don't write code in that manner, you're really using the wrong language. For instance, C provides designated initializers while C++ doesn't. I'm guessing thats because C++ isn't designed around structs; they're only there to remain compatible with C. C also has a different casting method than C++. You can do implicit void pointer casts in C while AFAIK you can't in C++. So its not that C++ has more features, it just that it has different features. (Well okay it has more, but only because it needs more).
I have misused C++ like that on a number of occasions, and didn't have a problem with it.

Can anyone here honestly say that they haven't ever slipped in a C-sytle cast, or other C construct into their C++ program?

You're only fighting with yourself. Just do it. You're gonna be no less happy by the sounds of it. You don't have to anything other than change your file types to cpp, and stop denying yourself the pleasure of C++'s extra features.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement