Funny C Rules (And Low-Level Languages in general)

Discuss programming topics for the various GPL'd game engine sources.
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

Re: Funny C Rules (And Low-Level Languages in general)

Post by Spike »

stdcall dates back to when there were multiple languages and compilers for native code... microsoft used it in the hope that it would give people less reasons to hate windows, and its been plaguing c/c++ users ever since. :P
I think windows has some special magic so that message handlers can use either convention without exploding, because if people can muck it up, then they will do so, and have done so. This doesn't mean that you should do so.

stdcall is nice because if you passed the wrong number of arguments, your whole program explodes...
using cdecl means you probably won't notice for years.
stdcall's symbol decorations that add @4 or whatever depending on the number of arguments help when you have dodgy function protypes all over the place. personally I use fastcall as a default calling convention in debug builds so stuff like this is blatent, its like stdcall but with the added illusion of extra speed!

win64 has only one calling convention. it'll ignore any stdcall/cdecl/fastcall markup (it might still warn though, for compat with 32bit code if its later compiled for that instead).
and 64bit linux/unix predates that with a different one (except for wine, anyway).
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: Funny C Rules (And Low-Level Languages in general)

Post by Baker »

Interesting. So multiple calling conventions is a "Windows things".
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
frag.machine
Posts: 2126
Joined: Sat Nov 25, 2006 1:49 pm

Re: Funny C Rules (And Low-Level Languages in general)

Post by frag.machine »

Indeed.

http://blogs.msdn.com/b/oldnewthing/arc ... 48616.aspx

To be fair, according to one of the commenters in the link above, _fastcall was created by Borland to use with Delphi.
I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC :) (LordHavoc)
jitspoe
Posts: 217
Joined: Mon Jan 17, 2005 5:27 am

Re: Funny C Rules (And Low-Level Languages in general)

Post by jitspoe »

mankrip wrote:

Code: Select all

do
{
stuff (); // this works
} while (true);

Code: Select all

do
{
stuff (); // this doesn't, even though it comes before the conditional check
} while (false);
And I figured out that the need for ( ) in sizeof is because of pointers, like
i = sizeof(cvar_t *) - 1;
instead of
i = sizeof cvar_t * - 1;
do { ... } while (false); is used frequently in macros, so something is really weird if that's not working for you.
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: Funny C Rules (And Low-Level Languages in general)

Post by Baker »

If you are going to use #pragma message (I use it a lot), don't use the word "warning" in the text. #pragma message is awesome to remind you to revisit code because it prints when you compile.

#pragma message ("If we aren't reading from server every frame, this stuff will break ...") // A note I stuck in make myself aware of a weakness in client/server separation

The compiler may see the word "warning" and interpret it as being a compile warning:
Don't do: #pragma message ("This causes a warning")

yourbin.exe 0 error(s), 1 warning(s)
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
mankrip
Posts: 924
Joined: Fri Jul 04, 2008 3:02 am

Re: Funny C Rules (And Low-Level Languages in general)

Post by mankrip »

malloc must always be typecasted, no matter the data type you're attributing it to. Otherwise there may be memory alignment problems.
Source: https://isocpp.org/wiki/faq/c

And freeing the same pointer multiple times can result in all kinds of memory corruption:
It might, depending on the phase of the moon, corrupt your heap, crash your program, make arbitrary and bizarre changes to objects that are already out there on the heap, etc. Unfortunately these symptoms can appear and disappear randomly.
Source: https://isocpp.org/wiki/faq/freestore-mgmt

I must check for both situations in my code. The first one may be happening a few times.
Ph'nglui mglw'nafh mankrip Hell's end wgah'nagl fhtagn.
==-=-=-=-=-=-=-=-=-=-==
Dev blog / Twitter / YouTube
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: Funny C Rules (And Low-Level Languages in general)

Post by Baker »

mankrip wrote:malloc must always be typecasted, no matter the data type you're attributing it to. Otherwise there may be memory alignment problems.
Source: https://isocpp.org/wiki/faq/c
I'm pretty sure that's for usage of malloc within C++, not C.

http://stackoverflow.com/questions/4993 ... -in-malloc

While I'm no expert in C++ and hope to keep it that way, keep in mind that in C you do almost everything yourself.

C++ adds an extra layer of memory management and has its own conventions of handling objects and such, so it needs some clarity about what is going on for the memory management.
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
revelator
Posts: 2621
Joined: Thu Jan 24, 2008 12:04 pm
Location: inside tha debugger

Re: Funny C Rules (And Low-Level Languages in general)

Post by revelator »

while malloc and free are usable in C++ i guess new and delete would be a better choice :)
allthough if you need something like _aligned_malloc/_aligned_free you would probably have to write it yourself.
atleast gcc does not accept malloc without typecasts in C, looks like void * is not treated as a valid pointer so gcc barfs.
Productivity is a state of mind.
mankrip
Posts: 924
Joined: Fri Jul 04, 2008 3:02 am

Re: Funny C Rules (And Low-Level Languages in general)

Post by mankrip »

Anyway, since my last post I made all malloc typecasts explicit already.

I do plan on moving on to C++ in the future, so that may help.
Ph'nglui mglw'nafh mankrip Hell's end wgah'nagl fhtagn.
==-=-=-=-=-=-=-=-=-=-==
Dev blog / Twitter / YouTube
mh
Posts: 2292
Joined: Sat Jan 12, 2008 1:38 am

Re: Funny C Rules (And Low-Level Languages in general)

Post by mh »

I think the C-like subset of C++ is just a flat-out better language than C. It's worthwhile just renaming your .c files to .cpp and getting a clean compile. You'll be surprised at the amount of nastiness that lurks behind valid C code. Having features like stronger type-safety and explicit forward declarations will just make you a better programmer (your code will be cleaner too!); you don't have to descend to the ninth circle of template insanity in order to write C++ code.

There are some other C++ features such as function overloading and optional parameters/default parameter values that make code nicer too. A lot of C fans who dismiss C++ seem to do so based on some of the more extreme examples of C++ code imaginable, and apparently without even having much knowledge of the language, assuming that you must write classes for everything and use templates wherever possible (I'm thinking of something like Linus Torvalds' rant about C++ here, where he really showed how ignorant he is of what he was talking about more than anything else; unfortunately people will read that crap and believe it). None of that is true; the C-like subset is quite lean and simple.

As a bonus you can now link to and use both C and C++ libraries without any contortions.

As for compile times: just use precompiled headers. Even with C you can get the compile time for something like FitzQuake down from 10+ seconds to under 1 second.
We had the power, we had the space, we had a sense of time and place
We knew the words, we knew the score, we knew what we were fighting for
mankrip
Posts: 924
Joined: Fri Jul 04, 2008 3:02 am

Re: Funny C Rules (And Low-Level Languages in general)

Post by mankrip »

Getting a bit off-topic here, what confuses me the most in C++ are namespaces. Classes and such are fine, I've learned about them during the Java classes in college.

And a Java feature that I love is to be able to access a public variable of an object directly from the return of a statement that returns that object. My Java skills are rusted, but an example would be somewhat like
{
(guy = new player (teleport.getOrigin()) ).setName ("Quake Guy Jr.");
}
instead of this:
{
guy = new player (teleport.getOrigin());
guy.setName ("Quake Guy Jr.");
}
I don't remember specific examples of code where I used this, but it simplified my code design a lot by allowing me to use nameless objects.
Ph'nglui mglw'nafh mankrip Hell's end wgah'nagl fhtagn.
==-=-=-=-=-=-=-=-=-=-==
Dev blog / Twitter / YouTube
revelator
Posts: 2621
Joined: Thu Jan 24, 2008 12:04 pm
Location: inside tha debugger

Re: Funny C Rules (And Low-Level Languages in general)

Post by revelator »

namespaces are a bit like C structs grouping together a bunch of functions :)
a simple way to explain it when in C you do something like thispointer->thisfunction to access say typedef struct thispointer { char *thisfunction; }
a namespace works much the same way but it looks a little different see -> thispointer::thisfunction. One big difference is that you can do namespace thispointer { char *thisfunction = wtf; }
while a C struct does not allow that as far as i know.
Productivity is a state of mind.
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: Funny C Rules (And Low-Level Languages in general)

Post by Baker »

Here's a fun one.

float bob = 494;

What is type of !!bob ?

Is it still a float? Or did it turn into an int? I'm guessing it's an int because it is a logical operation result like ( == )
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
revelator
Posts: 2621
Joined: Thu Jan 24, 2008 12:04 pm
Location: inside tha debugger

Re: Funny C Rules (And Low-Level Languages in general)

Post by revelator »

could probably be a float if bob = 494.0f; hehe but i get your meaning :)
actually 494.0f would be a double so whoops :P
Productivity is a state of mind.
ericw
Posts: 92
Joined: Sat Jan 18, 2014 2:11 am

Re: Funny C Rules (And Low-Level Languages in general)

Post by ericw »

The C99 draft standard pdf says: (6.5.3.3 Unary arithmetic operators)
The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0. The result has type int. The expression !E is equivalent to (0==E).
So the type of !!bob is int.
Post Reply