c

Easiest way to get file's contents in C

What is the simplest way (least error-prone, least lines of code, however you want to interpret it) to open a file in C and read its contents into a string (char*, char[], whatever)? ...

Cross platform format string for variables of type size_t?

On a cross platform c/c++ project (Win32, Linux, OSX), I need to use the *printf functions to print some variables of type size_t. In some environments size_t's are 8 bytes and on others they are 4. On glibc I have %zd, and on Win32 I can use %Id. Is there an elegant way to handle this? ...

Learning game programming

Quick question: I've been in the web-dev world for several years now, I've wrapped my head around Java, Python, PHP, C# and Ruby- and currently make my living as a C#.NET programmer and I want to learn game development for real, no "build your own 2d sprite game over 14 days"-bs, the real deal. Where do I start? learning C? learning C++?...

C/C++ Web Server Library?

I'm looking for a well-written, flexible library written in C or C++ (I'm writing my apps in C++) that can be used to embed an relatively simple HTTP server into my applications. Ultimately I will use this for application monitoring and control. There are a number of great client-side libraries (e.g. libwww, neon, curl) but I'm struggl...

What's the algorithm behind sleep() ?

Now there's something I always wondered: how is sleep() implemented ? If it is all about using an API from the OS, then how is the API made ? Does it all boil down to using special machine-code on the CPU ? Does that CPU need a special co-processor or other gizmo without which you can't have sleep() ? The best known incarnation of sle...

When is it ok to use a global variable in C?

Apparently there's a lot of variety in opinions out there, ranging from, "Never! Always encapsulate (even if it's with a mere macro!)" to "It's no big deal - use them when it's more convenient than not." So. Specific, concrete reasons (preferably with an example) Why global variables are dangerous When global variables should be use...

How to Build a simple HTTP server in C

I need to build a simple HTTP server in C. Any guidance? Links? Samples? Thanks! ...

Unit testing error conditions - EINTR

In short, how do you unit test an error condition such as EINTR on a system call. One particular example I'm working on, which could be a case all by itself, is whether it's necessary to call fclose again when it returns EOF with (errno==EINTR). The behavior depends on the implementation of fclose: // Given an open FILE *fp while (fclo...

What's a good C memory allocator for embedded systems?

I have an single threaded, embedded application that allocates and deallocates lots and lots of small blocks (32-64b). The perfect scenario for a cache based allocator. And although I could TRY to write one it'll likely be a waste of time, and not as well tested and tuned as some solution that's already been on the front lines. So what ...

Unit Testing Frameworks for C

I've seen a few questions specific to C++, but I'm really curious about C. I'm trying to add a standard unit test framework into our build environment. My primary goals are to encourage our developers to write unit tests, and to standardize those test so others can run them. Ideally I'd like to run the unit tests as part of our nightly b...

Initializing a large structure in C

In our code we used to have something like this: *(controller->bigstruct) = ( struct bigstruct ){ 0 }; This used to work great, and then we upgraded versions of GCC and suddenly started seeing stack overflows. Looking at the assembly, the old GCC code (2.x) was basically doing this: memset(controller->bigstruct, 0, sizeof(struct b...

const static

const static int foo = 42; I saw this in some code here at StackOverflow and I could'nt figure out what it does. Then I saw some confused answeres at other forums. My best guess is that it's used in C to hide the constant foo from other modules. Is this correct? If so, why would anyone use it in a C++ context where you can just make ...

Book recommendations: C for PHP programmers

I've been developing with PHP for a couple of years now, and have a strong grounding in it. But I'd like to learn more about programming languages in general, and want to be introduced to more low-level stuff. I've decided to learn C, and wanted to get your book recommendations. PHP has been pretty simple for me so far (including the OO...

Isn't saying "C/C++" wrong?

I've seen a lot of questions around that use improperly the expression "C/C++". The reasons in my opinion are: Newbie C and C++ programmers probably don't understand the difference between the two languages. People don't really care about it since they want a generic, quick and "dirty" answer While C/C++ could sometimes be interpret...

What is the return value of feof() on a closed file?

for a FILE* stream, if I read as much data as possible, feof(stream) returns me non-zero. Then, If I fclose stream, it feof(stream) will continue to returns me a non-zero value? Is it GUARANTEED? ...

C++: Asterisks and Pointers

I've recently decided that I just have to finally learn C/C++, and there is one thing I do not really understand about pointers or more precisely, their definition. How about these examples: int* test; int *test; int * test; int* test,test2; int *test,test2; int * test,test2; Now, to my understanding, the first 3 cases are all doin...

Learn C first before learning Objective-C

Being an aspiring Apple developer, I want to get the opinions of the community if it is better to learn C first before moving into Objective-C and ultimately the Cocoa Framework? My gut says learn C, which will give me a good foundation. ...

What is the best encryption library in C/C++?

What is the best encryption library in C/C++ In terms of: entropy quality ease of use readability portability performance What's your favorite and why do you like it? ...

Can you allocate a very large single chunk of memory ( > 4GB ) in c or c++?

With very large amounts of ram these days I was wondering, it is possible to allocate a single chunk of memory that is larger than 4GB? Or would I need to allocate a bunch of smaller chunks and handle switching between them? Why??? I'm working on processing some openstreetmap xml data and these files are huge. I'm currently streaming th...

Your preferred C/C++ header policy for big projects?

When working on a big C/C++ project, do you have some specific rules regarding the #include within source or header files? For instance, we can imagine to follow one of these two excessive rules: #include are forbidden in .h files; it is up to each .c file to include all the headers it needs Each .h file should include all its dependa...