Page 4 of 5

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

Posted: Sat Apr 04, 2015 4:36 am
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).

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

Posted: Sat Apr 04, 2015 12:56 pm
by Baker
Interesting. So multiple calling conventions is a "Windows things".

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

Posted: Sat Apr 04, 2015 10:18 pm
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.

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

Posted: Sun Apr 05, 2015 8:06 am
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.

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

Posted: Wed Apr 08, 2015 1:06 am
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)

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

Posted: Mon Apr 20, 2015 11:54 am
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.

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

Posted: Mon Apr 20, 2015 5:27 pm
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.

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

Posted: Tue Apr 21, 2015 9:42 am
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.

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

Posted: Thu Apr 23, 2015 4:32 pm
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.

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

Posted: Thu Apr 23, 2015 10:47 pm
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.

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

Posted: Thu Apr 23, 2015 11:45 pm
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.

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

Posted: Fri Apr 24, 2015 2:18 am
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.

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

Posted: Wed May 13, 2015 9:16 pm
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 ( == )

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

Posted: Sat May 16, 2015 11:43 pm
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

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

Posted: Sun May 17, 2015 1:41 am
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.