c

C/C++: How to make a variadic macro (variable number of arguments)

I want to write a macro in C that accepts any number of parameters, not a specific number e.g. #define macro( X ) something_complicated( whatever( X ) ) where X is any number of parameters I need this because whatever is overloaded and can be called with 2 or 4 parameters. I tried defining the macro twice, but the second definiti...

I've heard i++ isn't thread safe, is ++i thread-safe?

I've heard that i++ isn't a thread-safe statement since in assembly it reduces down to storing the original value as a temp somewhere, incrementing it, and then replacing it, which could be interrupted by a context switch. However, I'm wondering about ++i. As far as I can tell, this would reduce to a single assembly instruction, such as...

Error in C Program

Hi, while(((long)(1000*ratio*((long)clock()-(long)t0))%100)/1000)<Data_Read_Rate); The above line is generating the following error: "Syntax Error before < token". Why is this error coming up? I use MINGW32 for development(GCC compiler). Thanks... ...

using shared memory with php and c?

Can you use shared memory to communicate between php scripts and c program in windows? The c program runs all the time and uses memory mapped files ie: handle1 = CreateFileMapping( (HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE, 0, sizeof(byte)*BUFFER_SIZE, "my_foo" ); hView = (LPINT) MapViewOfFile(handle1, FILE_MAP_ALL_ACCESS, 0, 0, 0); ...

Getting the number of packets in a pcap capture file?

I need a program which prints the number of packets in a capture file which uses the pcap format. This number does not seem available in the pcap header (probably because it is written before the capture starts) and it does not seem there is a "footer" in the file, with this information. So, I believe the only algorithm is to loop over ...

Declaring structures within functions in C

I have a structure that only one function must access. The function converts tokens like "k, K, kb, KB, m, M, mb, MB, ..." into an actual unit. The purpose of this is to simplify a configuration file. So, suppose we have: static uint32_t real_unit(const char *str) { struct u2type { char key[3]; uint32_t val; } const...

How to use 2 C libs that export the same function names

Duplicate of the following question: C function conflict Hi, in my current project I have to use some kind of interface lib. The function names are given by this interface, what this functions do is developers choice. As far as I can tell a project shall use this functions and when it comes to compiling you choose the lib and with it ...

A Guide for Creating your own Library for Cocoa(touch) development

I'm currently using a lot of the same subclassed objects with custom methods. It would be more convenient to create my own library which I can use for several projects. The goal is to have my own classes being available in the same way classes like UIView, CGRect etc are, including convenient methods like CGRectMake, both with classes ...

How can one implement a Compact Directed Acyclic Word Graph (CDAWG) in C?

How would one implement this data structure in C. It is a structure similar to the DAWG, that is more efficient than the trie which only compresses prefixes. The CDAWG is a data structure twice as space efficient as the DAWG. ...

Using EXTERNAL mechanism with Cyrus SASL

Does the Cyrus SASL api not support the EXTERNAL mechanism? I'm trying to use it as a client, but it returns SASL_NOMECH when asked. % cat cyrus_sal_ex.c /* cyrus_sasl_ex.c: Example of using the Cyrus SASL api */ #include <stdio.h> /* for printf() */ #include <sasl/sasl.h> /* for sasl_client_*(), SASL_*, sasl_*_t */ static char...

Fixed address variable in C

For embedded applications, it is often necessary to access fixed memory locations for peripheral registers. The standard way I have found to do this is something like the following: // access register 'foo_reg', which is located at address 0x100 #define foo_reg *(int *)0x100 foo_reg = 1; // write to foo_reg int x = foo_reg; // r...

What does the 9th commandment mean?

In The Ten Commandments for C Programmers, what is your interpretation of the 9th commandment? The 9th commandment: Thy external identifiers shall be unique in the first six characters, though this harsh discipline be irksome and the years of its necessity stretch before thee seemingly without end, lest thou tear thy hair out and go...

Is this extern harmless

main.h extern int array[100]; main.c #include "test.h" int array[100] = {0}; int main(void) { /* do_stuff_with_array */ } In the main.c module, the array is defined, and declared. Does the act of also having the extern statement included in the module, cause any problems? I have always visualized the extern statement as...

Creative ideas for display large amount of text on web page

I have a 2 column table in a database 1,000 rows long(All integer data). The display will allow for the user to delete a certain range of data from the table. What I am looking for is a creative way to display all the data so the user can get to different parts of it really fast. Maybe displaying different chunks at once, represent wit...

Normalize file path with WinAPI

Given two file path strings with potentially different casing and slashes ('\' vs '/'), is there a quick way (that does not involve writing my own function) to normalize both paths to the same form, or at least to test them for equivalence? I'm restricted to WinAPI and standard C++. All files are local. ...

File transfer through sockets, final size with less bytes

I'm trying to receive some file through sockets in C. But the server sends me 64-byte packets for a 1000000 byte file for example and I get approximately 999902 bytes on the destination file. while ((n = read(sd, buffer_in, BUFSIZE ))) // BUFSIZE = 64 { if(n<0) { printf("Fail.\n"); fclose(archivo); return ...

Platform-dependent issue in run-length encoding of bmp files using C

I've written a program which opens a bmp file and treats it as a character file and performs run length encoding on it. It produces a valid compressed encoding file, which I read again to perform the decoding. When i'd made the application i was using Fedora and it ran perfectly fine. Now i'm running it on ubuntu and it refuses to work....

How to identify a 64 Bit build on Linux using the preprocessor?

Hi. I am about to port a Windows 32 Bit application to 64 Bit, but might decide to port the whole thing to Linux later. The code contains sections which are dependent on the amount of memory available to the application (which depends on whether I'm creating a 32 or 64 Bit build), while the ability to compile a 32 Bit version of the co...

About C/C++ stack allocation

While studying C++ (and C) I had some particular doubts regarding the working of stack allocation, that I can't find a solution to: Does stack allocation call malloc/free functions implicitly? If not; how does it assure there is no conflict between stack allocation and heap allocation? If yes; does stack allocation in C++ implicitly ca...

C# vs C - Big performance difference

I'm finding massive performance differences between similar code in C anc C#. The C code is: #include <stdio.h> #include <time.h> #include <math.h> main() { int i; double root; clock_t start = clock(); for (i = 0 ; i <= 100000000; i++){ root = sqrt(i); } printf("Time elapsed: %f\n", ((double)clock() - sta...