c

Why artificially limit your code to C?

This is prompted by a an answer I gave to a current question which asks about a generics library for C - the questioner specifically states that they do not want to use C++. My question to him and others who insist on using C is why do they do so when: C++ provides the specific features they are asking about Their C compiler is almost ...

What is the point of adding a return statement at end of a void C/C++ routine?

I see functions/methods with a void return in the signature that have a return statement at the end of the function. What is the reason for this, and does this apply to other languages? For all I know, I can use return if I want to exit anywhere else but the end of the function. A C example: void funtion(void) { int x = 1 + 2; ...

Developing kernels and testing them in virtual machines

I like programming challenges, and writing a kernel seems a programming challenge. Unfortunately, kernels are particularly hard to test because they are basically the core of operating systems and so they can't be easily ran on top of an operating system. However, I know about applications called Virtual Machines that can emulate compu...

long integer as array index in C gives segmentation fault

Hello to all, The following C Code gives a segmentation fault: #include <stdio.h> #include <stdint.h> int main(){ uint32_t *a; uint32_t idx=1233245613; a[idx]=1233; return 0; } How can I use uint32_t as index of an array in C? Or how can I use array like structure which can get uint32_t and 12 digit n...

What are some tricks I can use with macros?

In our legacy code, as well as our modern code, we use macros to perform nifty solutions like code generations, etc. And we make use of both the # and ## operators. I am curious how other developers use macros to do cool things, if they use them at all. ...

How can I use fprintf and write to a pipe?

I created a pipe and I used dup2() to overwrite streams 1 & 2 (stdout & stderr) into those pipes. Now I wish to use fprintf to write to stream 1 or 2, but my program doesn't seem to be receiving anything on the other side of the pipe. I've tried using printf(), but I'm not sure if this writes to stdout or stream 1 by default. If it writ...

stack & realloc question C++

int main() { char myString = NULL; realloc(&myString, 5); strncpy((char *)&myString, "test", 5); } Seems to work Fine but, i'm still slightly confused about stack vs heap, is this allowed? Does myString need to be freed manually or will it be released when it goes out of scope? Edit: Thanks for the responses, so i presume t...

Checking whether the file can be opened in portable C

I'd like to perform a quick check whether or not a file can be opened. It should be written in portable C, or at least to work on Win32 and POSIX systems. #ifdefs are acceptable. I'm trying to avoid this: int openable(const char*filename) { FILE *f = fopen(filename,"r"); if (!f) return 0; /* openable */ fclose(f); ...

Game loop that won't stop waiting for user input

I've started fiddling with C to improve my programming skills, and decided to try and implement a Tetris game. Nothing too fancy, it'll run on the console. I never implemented a game that keeps running despite user input, and didn't figure out I'd have to deal with this problem until I started thinking about the game algorithm. Googl...

Concurrency: Are Python extensions written in C/C++ affected by the Global Interpreter Lock?

One of Python's strongest points is the ease of writing C and C++ extensions to speed up processor intensive parts of the code. Can these extensions avoid the Global Interpreter Lock or are they also restricted by the GIL? If not, then this "ease of extension" is even more of a killer feature than I previously realized. I suspect the ans...

Bimodal distribution in C or Python

What's the easiest way to generate random values according to a bimodal distribution in C or Python? I could implement something like the Ziggurat algorithm or a Box-Muller transform, but if there's a ready-to-use library, or a simpler algorithm I don't know about, that'd be better. ...

Hacking samba - how to get a directory from struct fd_handle

Hi, i'm hacking some samba internals, and I want to log, what's written in read_file and write_file, exactly I want to get file name, directory, and how much bytes are written. in struct files_struct, there is defined file name (char* fsp_name), and I can count number of written bytes, but in files_struct there is no field with director...

How do I write file modification dates programmatically in POSIX?

Hello. I would like to touch my files from C code to modify their access date. This does not seem to work: struct stat fileSt; lstat(path, &fileSt); fileSt.st_mtime = time(NULL); Thank you for help. ...

Realloc()/Resize an object C++

when they are represented in memory are objects C++ objects the same as C structs. With C I could do something like this: struct myObj { int myInt; char myVarChar; }; int main() { myObj * testObj = (myObj *) malloc(sizeof(int)+5); testObj->myInt = 3; strcpy((char*)&testObj->myVarChar, "test"); ...

Setting Accept-Language in COM-embedded IE

I have an IE instance embedded in my C application using COM. With IWebBrowser2.Navigate I can pass headers to be sent along with the request, except apparently "Accept-Language". It seems the language settings from IE itself always override the value I pass in for that header. Is there any way around this? ...

Know of any small projects implementing an HTTP service using libevent?

My C is a bit rusty. I have some ideas I'd like to try and realize with libevent. Do you know any codebase I can use as a reference? ...

Vala (C#-like language) compiles to C?

I'm a C# developer who stumbled across a new programming language for linux called vala. It has almost exactly the same syntax as C#, which is awesome. I never really was a big fan of Mono. This allows programmers to write GTK+ apps in a C# style language. My question is: Does vala get compiled into C? ...

What is the worst real-world macros/pre-processor abuse you've ever come across?

What is the worst real-world macros/pre-processor abuse you've ever come across (please no contrived IOCCC answers *haha*)? Please add a short snippet or story if it is really entertaining. The goal is to teach something instead of always telling people "never use macros". p.s.: I've used macros before... but usually I get rid of the...

Has anyone ever had a use for the __COUNTER__ pre-processor macro?

The title pretty much says it all. The __COUNTER__ symbol is provided by VC++ and GCC, and gives an increasing non-negative integral value each time it is used. I'm interested to learn whether anyone's ever used it, and whether it's something that would be worth standardising? ...

Equivalence of <limits> and <climits>

Is this guaranteed to be always true: std::numeric_limits<int>::max() == INT_MAX What does C++ standard say about it? I could not find any reference in the standard that would explicitly state this, but I keep reading that those should be equivalent. What about C99 types that are not in C++98 standard for compilers that implement bot...