c

Hashmap in the making

How to go about creating a Hashmap in C from scratch ? What would be the parameters taken into consideration and what how would you test the hashmap as to how good it is ? As in what would be benchmark test cases which you require to run before you say that your hash map is complete. ...

Compilation

My source code doesn't get compiled when i specify the compiler as Borland C. It gets compiled when the compiler is selected as Microsoft Visual Express. Once it gets compiled under Microsoft Visual Express, only then,it gets compiled under Borland C. Why is this happening? ...

Bus error vs Segmentation fault

Difference between a bus error and a segmentation fault? Can it happen that a program gives a seg fault and stops for the first time and for the second time it may give a bus error and exit ? ...

popen() in C

I can run the following command xwd -root | xwdtopnm | pnmtojpeg > screen.jpg in a terminal under linux and it will produce a screenshot of my current screen. I try to do the following with the code: #include <stdio.h> #include <stdlib.h> int main() { FILE *fpipe; char *command="xwd -root | xwdtopnm | pnmtojpeg"; char line[2...

Why should I use malloc() when "char bigchar[ 1 << 31 - 1 ];" works just fine?

What's the advantage of using malloc (besides the NULL return on failure) over static arrays? The following program will eat up all my ram and start filling swap only if the loops are uncommented. It does not crash. ... #include unsigned int bigint[ 1 << 29 - 1 ]; unsigned char bigchar[ 1 << 31 - 1 ]; int main (int argc, char **argv)...

Replicating C struct padding in Java

Hi, According to here, the C compiler will pad out values when writing a structure to a binary file. As the example in the link says, when writing a struct like this: struct { char c; int i; } a; to a binary file, the compiler will usually leave an unnamed, unused hole between the char and int fields, to ensure that the int field i...

Hints and tools for finding unmatched braces / preprocessor directives

This is one of my most dreaded C/C++ compiler errors: file.cpp(3124) : fatal error C1004: unexpected end-of-file found file.cpp includes almost a hundred header files, which in turn include other header files. It's over 3000 lines. The code should be modularized and structured, the source files smaller. We should refactor it. As a ...

How to initialise a C program using file, environment and/or command line?

In C, how to configure the initial settings for some program, using key-value pairs, with any or all of: a plain text configuration file, environment variables, and options on the command line. Also, how to decide which method's values overide those from either of the other two. Then if values have been changed, to optionally update the ...

Why is it possible to define an variable with type of a superclass, but assign it an object of a subclass?

That's still not totally clear to me. Example: NSArray *arr = [[NSMutableArray alloc] init]; I mean... yeah, it works. BUT: Why can I tell the compiler "hey, my var is just an NSArray, you just need to reserve space so that this fits in there" and then, in reality, there comes a big, fat NSMutableArray into that place that has way more...

The right type for handles in C interfaces

I'm creating a C api that hides some functionality in a DLL file. Since everything is C++ on the inside most of the functions works against handles which maps directly to this pointers on the inside of the API. To get a some degree of type safety for those handles I define them like this: typedef struct MyType1* MyType1Handle; typedef...

What does floating point error -1.#J mean?

Recently, sometimes (rarely) when we export data from our application, the export log contains float values that look like "-1.#J". I haven't been able to reproduce it so I don't know what the float looks like in binary, or how Visual Studio displays it. I tried looking at the source code for printf, but didn't find anything (not 100% s...

Check whether user input is a valid string or not using C

Hi I am taking a string input from user. But how d I check whether user has entered a string or a number?? ...

How do function pointers in C work?

I had some experience lately with function pointers in C. So going on with the tradition of answering your own questions, I decided to make a small summary of the very basics, for those who need a quick dive-in to the subject. ...

Warning when using function pointers in C

Hi all! This is actually a non-critical question, but I get this warning most of the time I use function pointers and still couldn't figure out why on my own. Consider the following prototype: typedef void * Pointer; void tree_destroyLineage(Tree greatest_parent, void *dataDestructor(Pointer data)); And so far I can compile my thousan...

GCC __attribute__((aligned(x)) explanation

Hi, i have the following code: #include <stdio.h> int main(void) { float a[4] __attribute__((aligned(0x1000))) = {1.0, 2.0, 3.0, 4.0}; printf("%p %p %p %p\n", &a[0], &a[1], &a[2], &a[3]); } And i have the following output: 0x7fffbfcd2da0 0x7fffbfcd2da4 0x7fffbfcd2da8 0x7fffbfcd2dac Why the address of a[0] is not a...

Signal Stack

I did read that signals need to have a separate stack, why and how do you think it is implemented ? Are they dynamically allocated or statically allocated ? How is memory allocation done ? Is it the same for all signals ? ...

Is #define supposed to add spaces around macros?

I was looking at the program at http://www0.us.ioccc.org/1988/westley.c, mentioned in another SO answer - it's supposed to print the value of pi, about 3.142, but when I compile it and run it I get 0.250. It looks like when the GCC preprocessor (both 4.1.2 and 3.4.6 tested) runs on the code, it converts #define _ -F<00||--F-OO--; _-_-_...

How to get a list of open sockets in Linux using C?

Hello, Is there a way to get a list of all open sockets ( socket address or socket descriptor ) in Linux using C in user-space or kernel? Thank you ...

Using WinDDK test utilities with Visual Studio 2005

I'm currently trying to run the NdisProt driver given by WinDDK 2003. I've build and installed the driver successfully. It comes with a test utility named uiotest When I build the utility with make it runs correctly. When I create a blank win32 application solution with Visual Studio 2005 it cannot connect to the driver during CreateF...

XML to C struct and C struct to XML

I like to do my server side programming in C, but need to inter-operate with some XML. What I need to write is some function that, given a C structure, or nested structure, and another structure (or nested structures) that describes the elements in the C structure, spits it out as XML. And another function that reads the XML, verifies ...