c

Error when mixing C and C++

I would like to create Win32 application with GUI in cpp-files and main logic in c-files. Things are ok until c-function, called within c++ code with CreateThread, for example open COM port. I got IDE hanging in C++ Builder and error in CodeBlocks+GCC. ...

Atomic delete for large amounts of files

I am trying to delete 10000+ files at once, atomically e.g. either all need to be deleted at once, or all need to stay in place. Of course, the obvious answer is to move all the files into a temporary directory, and delete it recursively on success, but that doubles the amount of I/O required. Compression doesn't work, because 1) I don...

Macro indicating I/O pins used

I'm writing firmware for a PIC32MX, using HiTech PICC32. One of the problems I want to avoid is that since most of the pins have multiple names (eg. AN0 = RB0 = CN2 = PGED1), I or someone else might accidentally use RB0 without realising that AN0 is already used. (This can actually be catastrophic, since incorrectly configuring an analog...

Cleaner pointer arithmetic syntax for manipulation with byte offsets

In the following lines of code, I need to adjust the pointer pm by an offset in bytes in one of its fields. Is there an better/easier way to do this, than incessantly casting back and forth from char * and PartitionMap * such that the pointer arithmetic still works out? PartitionMap *pm(reinterpret_cast<PartitionMap *>(partitionMaps)); ...

Creating a struct on the heap?

I've been instructed to write a model strdup by creating a String struct on the heap the holds a copy of the source. I think I have successfully coded the strdup, but I'm not sure if I've created a Struct on the heap... typedef struct String { int length; int capacity; unsigned check; char ptr[0]; } String; char* model...

Programmatic way to get variable name in C?

I am developing a tool to dump data from variables. I need to dump the variable name, and also the values. My solution: Store variable name as a string, and print the "variable name", followed by its value. Is there any programmatic way to know the variable name? ...

Open/close strategy for /proc pseudo-file

I have written a C utility for Linux that checks the contents of /proc/net/dev once every second., I open the file using fopen("/proc/net/dev", "r") and then fclose() when I'm done. Since I'm using a 'pseudo' file rather than a real one, does it matter if I open/close the file each time I read from it, or should I just open it when my ...

C-DLL from C++ source

I have a C-Wrapper for my C++ Framework. Since this should run on mac and windows I am using scons: env = Environment() env.Append(CPPPATH = ['./']) env.Append(LIBS = 'kernel32.lib') env.Append(LIBPATH = 'C:/Program Files/Microsoft SDKs/Windows/v6.0A/Lib') env.SharedLibrary(target='warpLib', source='warplib.cpp') Simple Versions of w...

Is EOF always negative?

Is EOF always negative? I'm thinking of writing a function that reads the next word in the input and returns the line number the word was found in or EOF if the end of the input has been reached. If EOF is not necessarily negative, the function would be incorrect. ...

Multithreading using C on PIC18

How does one create threads that run in parallel while programming PIC18, since there is no OS? ...

pointer-to-const conversion in C

The following code compiles without warning on GCC but gives a warning in Visual Studio 2005. const void * x = 0; char * const * p = x; x points to a constant object of unknown type, and p points to a constant pointer to char. Why should the assignment to p result in a warning? Again, this is C, not C++. Thanks. ...

C: Terminating the program by pressing a key

In C, I normally use the getch() function to wait for a key to be pressed and then end the program, but recently I read that since it's not a standard function, it's bad programming practice to use it. So instead of the following: int main() { dosomething(); getch(); //wait for the user to see the results and press a key to end...

Can I use a C style library built with VC6 directly in VC9 project?

We use an internal library(developed by some other team) built with VC6 compiler. This library mainly contains C Style APIs. We have a plan to migrate to Visual Studio 9 compiler. Should I request for the library to be built with VC9 compiler? A more generic question, On which points ( may be name mangling, optimization etc) a DLL buil...

C++ Win32 -- COM Method: equivalent C declaration

I've been told that every COM method callable from C++ code (take for instance IHTMLDocument2::write) has an equivalent C declaration, usable from C code... How do I find it? Thanks in advance! ...

ld: linker error

ld: foo.o: relocation R_X86_64_PC32 against undefined symbol `bar' can not be used when making a shared object; recompile with -fPIC I recompile with -fPIC and it still produces this error. ...

ISO C equivalent of braced-groups within expressions

How can I do the following in a compliant (ISO C99) way? #define MALLOC(type, length, message) ({ \ type * a_##__LINE__ = (type *)malloc((length) * sizeof(type)); \ assert(message && (a_##__LINE__ != NULL)); \ a_##__LINE__; ...

What is the size of an empty struct in C?

According to me, it is zero but there seems to be bit confusion here I have tested it with gcc compiler and it gives me zero as output. I know that in C++, size of an empty class is 1. Let me know if I am missing anything here. ...

strptime returning unexpected results

I'm still very new to C and trying to learn how to use strptime to see if it will work for part of a project, but I can't even get what seems like a very basic example working right... int main() { struct tm *t; t = (struct tm *) malloc(sizeof(struct tm)); memset(t, 0, sizeof(t)); if (strptime("12-2009", "%m-%Y", t) != ...

What is the difference between if(CONST==variable) or if(variable==CONST)?

Is there a difference in the order of the comparison operator? #define CONST_VALUE 5 int variable; ... if ( variable == CONST_VALUE ) // Method 1 ... OR if ( CONST_VALUE == variable ) // Method 2 ... Is this simply a matter of preference or is there a compelling reason for a particular comparison order? ...

How do I use the SQLite3 import command using the C API?

I have the following code: int rc; rc = sqlite3_exec(sqlite3_database, ".import mydata.csv mytable", callback, 0, &errMsg); After this gets run, errMsg contains this error message: near ".": syntax error I assume that it does not recognize the import command. However, this command works when running it from the sqlite3 program on t...