views:

645

answers:

7

Hi,

Many of the standard c library (fwrite, memset, malloc) functions have direct equivalents in the windows API (WriteFile, FillMemory/ ZeroMemory, GlobalAlloc).

Apart from portability issues, what should be used, the CLIB or windows API functions?

Will the C functions call the winapi functions or is it the other way around?

thanks for the help

+11  A: 

There's nothing magical about the C library. It's just a standardized API for accessing common services from the OS. That means it's implemented on top of the OS, using the API's provided by the OS.

Use whichever makes sense in your situation. The C library is portable, Win32 isn't. On the other hand, Win32 is often more flexible, and exposes more functionality.

jalf
+3  A: 

The functions aren't really equivalent with the exception of some simple things like ZeroMemory.

GlobalAlloc for example gives you memory, but it was used for shared memory transfer under win16 as well. Parts of this functionality still exist.

WriteFile will not only write to files but to (among others) named pipes as well. Something fwrite or write can't directly do.

I'd say use c library functions if possible and use the windows functions only if you need the extra functionality or if you get a performance improvement.

This will make porting to other platforms easier later on.

Nils Pipenbrinck
+1  A: 

C functions will call the system API, the Standard Runtime C Library (or CRT) is an API used to standardize among systems.

Internally, each system designs its own API directly using system calls or drivers. If you have a commercial version of Visual C++, it used to provide the CRT source code, this is interesting reading.

Vincent Robert
+2  A: 

Will the C functions call the winapi functions or is it the other way around?

The C functions (which are implemented in a user-mode library) call the WINAPI functions (which are implemented in the O/S kernel).

ChrisW
Many Winapi functions are implemented in the OS DLLs, which ones cause system calls is actually implementation defined and undocumented.
Blaisorblade
Some of the calls (e.g. file I/O, where the file system driver is in the kernel) obviously must end up in the O/S kernel; whereas others (e.g. simply zeroing a range of user-mode memory) needn't.
ChrisW
+1  A: 

A few additional points on some examples:

FillMemory, ZeroMemory

Neither these nor the C functions are system calls, so either one might be implemented on top of the other, or they could even have different implementations, coming from a common source or not.

GlobalAlloc

Since malloc() is built on top of operating system primitives exposed by its API, it would be interesting to know if malloc() and direct usage of such allocators coexist happily without problems. I might imagine of some reasons why malloc might silently assume that the heap it accesses is contiguous, even if I would call that a design bug, even if it were documented, unless the additional cost for the safety were non insignificant.

Blaisorblade
+2  A: 

If you're going to port your application across multiple platforms I would say that you should create your own set of wrappers, even if you use the standard C functions. That will give you the best flexibility when switching platforms as your code will be insulated from the underlying system in a nicer way.

Of course that means if you're only going to program for Windows platforms then just use the Windows functions, and don't use the standard C library functions of the same type.

In the end you just need to stay consistent with your choice and you'll be fine.

Daemin
+2  A: 

It's probably more information than you're looking for (and maybe not exactly what you asked) but Catch22.net has an article entitled "Techniques for reducing Executable size" that may help point out the differences in Win32 api calls and c runtime calls.

Slapout
thanks for the link. it touches many interesting topics, including relevant information on this topic.
vilaca