## Avoid Overflow when Calculating π by Evaluating a Series Using 16-bit Arithmetic?

Take a look at related QA: Baking-Pi Challenge – Understanding & Improving Its using Wiki: Bailey–Borwein–Plouffe_formula which is more suited for integer arithmetics. The real challenge however would be: How do I convert a very long binary number to decimal?. As you probably want to print the number in dec base … Also if you … Read more

Categories c

## Intel SSE: Why does _mm_extract_ps return int instead of float?

None of the answers appear to actually answer the question, why does it return int. The reason is, the extractps instruction actually copies a component of the vector to a general register. It does seem pretty silly for it to return an int but that’s what’s actually happening – the raw floating point value ends … Read more

## How C strings are allocated in memory?

When you write const char *ptr = “blah blah”; then the following happens: the compiler generates a constant string (of type char []) with the contents “blah blah” and stores it somewhere in the data segment of the executable (it basically has a similar storage duration to that of variables declared using the static keyword). … Read more

Categories c Tags

## C pointer notation compared to array notation: When passing to function

When you declare a function parameter as an array, the compiler automatically ignores the array size (if any) and converts it to a pointer. That is, this declaration: int foo(char p[123]); is 100% equivalent to: int foo(char *p); In fact, this isn’t about notation but about the actual type: typedef char array_t[42]; int foo(array_t p); … Read more

## Is scanf(“%d%d”, &x, &x) well defined?

The short answer is: Yes, it is defined: scanf will attempt to convert a sequence of bytes from stdin as an integer written in base 10 with optional initial spaces and an optional sign. If successful, the number will be stored into x. scanf will then perform these steps a second time. The return value … Read more

## Infinite recursion in C

Whenever you call a function, the arguments are pushed on the stack, which means that data on the stack segment is “allocated”. When the function is called, the return adress is also pushed on the stack, by the CPU, so it knows where to return to. In your example case this means, that no arguments … Read more

## How to check heap size for a process on Linux

Heap and memory management is a facility provided by your C library (likely glibc). It maintains the heap and returns chunks of memory to you every time you do a malloc(). It doesn’t know heap size limit: every time you request more memory than what is available on the heap, it just goes and asks … Read more

## C color text in terminal applications in windows

Since you want a C and Windows specific solution, I’d recommend using the SetConsoleTextAttribute() function in the Win32 API. You’ll need to grab a handle to the console, and then pass it with the appropriate attributes. As a simple example: /* Change console text color, then restore it back to normal. */ #include <stdio.h> #include … Read more

Categories c

## Where do malloc() and free() store allocated sizes and addresses?

There are lots of ways in which malloc/free can store the size of the memory area. For example, it might be stored just before the area returned by malloc. Or it might be stored in a lookup table elsewhere. Or it might be stored implicitly: some areas might be reserved for specific sizes of allocations. … Read more

## how to set close-on-exec by default

No and no. You simply need to be careful and set close-on-exec on all file descriptors you care about. Setting it is easy, though: #include <fcntl.h> fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); #include <unistd.h> /* please don’t do this */ for (i = getdtablesize(); i –> 3;) { if ((flags = fcntl(i, F_GETFD)) != -1) … Read more