c

Can this function be any safer ? Looking for tips and your thoughts !!!

Hi everyone, this is somewhat of an odd question. I wrote a C function. Its 'like' strchr / strrchr. It's supposed to look for a character in a c-string, but going backwards, and return a pointer to it. As c strings are not "null initiated", it also takes a third parameter 'count', indicating the number of chars it should look backwards...

C configuration API

When designing a C API for configuring a library/utility, I have a co-worker who prefers to lump all configuration parameters into 1 function call. For example: int set_diagnostic_email_config( char *to_address, bool include_timestamp, bool send_for_crashes, ...

Linux: How do I find out if a file has been updated by another process?

Hi, I am currently watching an XML file from log4j output. I have a custom viewer that displays the log-output in GUI. I need to watch this file as to when it gets updated so that the GUI can re-parse and update itself. In C# there is a FileWatcher concept so on Windows no-probs, but what options do i have using C on Linux. Is there a ...

Comparing collections of subsets up to permutation

I have an array a[i][j]. The elements are char, interpreted as subsets of the set {1,...,8} (the element k is in the subset if the k-th bit is 1). I do not think it is relevant, but every element has exactly 4 bits set. Every row a[1][j]..a[n][j] is a collection of subsets of {1,...,8}. I need to remove duplicate rows, where two rows ar...

Why do I get different results when I dereference a pointer after freeing it?

I've a question about the memory management in C (and GCC 4.3.3 under Debian GNU/Linux). According to the C Programming Language Book by K&R, (chap. 7.8.5), when I free a pointer and then dereference it, is an error. But I've some doubts since I've noted that sometimes, as in the source I've pasted below, the compiler (?) seems to work...

How can I search for simple if statements in C source code?

I'd like to do a search for simple if statements in a collection of C source files. These are statements of the form: if (condition) statement; Any amount of white space or other sequences (e.g. "} else ") might appear on the same line before the if. Comments may appear between the "if (condition)" and "statement;". I want to ex...

time.h - problem with clock()

I try to measure the clock cyles needed to execute a piece of code on the TMS32064x+ DSP that comes with the OMAP ZOOM 3430 MDK. I look at the "Programmer's Guide" of the DSP chip and it says that the DSP supports the clock() function. What I do is really simple, I just do start = clock(); for (i=0;i<100;i++){ /* do something here ...

How to profile each call to a function?

I want to profile my execution in a non-standard way. Using gprof, Valgrind, Oprofile... for a given function, I only get the mean of its execution time. What I would like is to obtain the standard deviation of this execution time. Example: void a() sleep ( rand() % 10 + 10 ) void b() sleep ( rand() % 14 + 2 ) main for (1 .. 10...

What is the Basic Structure of a Function in FORTRAN?

This is something that's I've wanted to know recently, mostly out of curiousity. I'm in the mood to learn some old coding styles, and FORTRAN seems like a good place to start. I guess I should help you guys out by providing a good starting point. So how would this C procedure be written in FORTRAN? int foo ( int x , int y ) { int...

Reverse a sentence in C??

Hi, I had just written a programme which reverses a sentence whatever the user gives. for eg:If the user gives the sentence as "How are you",my programm generates "uoy era woH". The programme which i had written is shown below.I just have a wild intution that there can be a smarter programe than the one which i had written.So valuab...

Some good Unicode tutorials in C?

Anyone knows of some good Unicode tutorials with examples in C? I have to create a console app (to be run in xterm), with Unicode support, and it has to be on C. :( ...

wchar_t vs wint_t

Hello. This is an ANSI C question. I have the following code. #include <stdio.h> #include <locale.h> #include <wchar.h> int main() { if (!setlocale(LC_CTYPE, "")) { printf( "Can't set the specified locale! " "Check LANG, LC_CTYPE, LC_ALL.\n"); return -1; } wint_t c; while((c=getwc(stdin))!=...

In C/C++ can anybody provide some thumb rules for writing small function using inline or macro?

Discussing with people, asking in interviews, or being asked in interviews, I do not know if I know exactly when to write a function as inline or write it as a macro. Apart from compile-time and run-time consideration, is there any suggestion from coding standard point of view. I think this is one of those question where it comes to the...

How to unescape html in C

I'm interested in unescaping text for example: '&#x5c;' -> '\' in C. Anyone knows of a good library? By html escape I mean, all the entity references. ...

Conditional operator differences between C and C++

I read somewhere that the ?: operator in C is slightly different in C++, that there's some source code that works differently in both languages. Unfortunately, I can't find the text anywhere. Does anyone know what this difference is? ...

C Library for compressing sequential positive integers

Hi, I have the very common problem of creating an index for an in-disk array of strings. In short, I need to store the position of each string in the in-disk representation. For example, a very naive solution would be an index array as follows: uint64 idx[] = { 0, 20, 500, 1024, ..., 103434 }; Which says that the first string is at po...

What’s the correct way to use printf to print a clock_t?

I'm currently using a explicit cast to unsigned long long and using %llu to print it, but since size_t has the %z specifier, why clock_t doesn't have one? There isn't even a macro for it. Maybe I can assume that on a x64 system (OS and CPU) size_t has 8 byte in length (and even in this case, they have provided %z), but what about clock_t...

Where can I learn how best to represent procedural code in a language-independent manner ready for transformations?

Hi, For my source-code transforming (Fortran and C) numerical automatic differentiation engine project PARADE, I need a language-independent representation of procedural program code. The IL (intermediate language) in the below diagram: (The whole tool will be developed solely using Haskell, so strip Java from the above diagram where...

C libraries for directory access

I know that standard C doesn't give me any ability to do anything with folders, but I would like a fairly portable and cross-platform way to access folders. At this time, all I need to do is make a folder, check if a folder exists, and possibly delete a folder. I can forsee needing to read files from a folder in the near future, but that...

Export a .sqllite file in C/C++ (on windows)

It is possible to move a .sqllite file somewhere while not corrupting it in C or C++? Somewhere could be another folder or something. If so could you give me some tips/pointers. ...