c

global counter in c is not working as expected

I have a bit of queue code that I was working on. I was trying to use a global int to keep track of the queue's size. #define MAX 100 int size=0; int gEnqueue=gDequeue=0; int enqueue() { gEnqueue++; if( size == MAX ) return QUEUE_FULL; /* snip the actual queue handling */ size++; return 0; } int dequeue()...

c++: what is the optimal way to convert a double to a string?

What is the most optimal way to achieve the same as this? void foo(double floatValue, char* stringResult) { sprintf(stringResult, "%f", floatValue); } ...

C: type conversion when passing an argument on a function call

From The C Programming Language 2nd Edition: Since an argument of a function call is an expression, type conversions also take place when arguments are passed to function. In absence of a function prototype, char and short become int, and float becomes double. By reading the text, I am getting an impression that unless you explicit...

Is there a reason that C99 doesn't support function overloading?

Apparently (at least according to gcc -std=c99) C99 doesn't support function overloading. The reason for not supporting some new feature in C is usually backward compatibility, but in this case I can't think of a single case in which function overloading would break backward compatibility. What is the reasoning behind not including thi...

How can I Fix the float value in output in C language

I have output like this: 1569.3669 15968.3699 41.3878 587.5401 but I want the output like this: 01569.3669 15968.3699 00041.3878 00587.5401 How can I do this in the C language? ...

interleaving bits with a mask

inputs: arbitrary bitset, e.g. bit positions 012345 arbitrary bit mask, e.g. (x=1) xx0x0x output: xx0x1x2345 That is, I want the first bit of the bitset to be placed in the first 0 of the mask. Likewise, the second bit is placed in the second 0 of the mask. example: mask = 1001001 bits = 1101 result = 1111011 I know that this ...

Using ALSA's function snd_pcm_writei can I free the sample buffer right away?

Using ALSA to play audio, after calling snd__pcm__writei, can I free the sound sample buffer right away or do I need to wait until the sound is finished playing before I can free the sample buffer? For example: unsigned short *buffer; buffer = malloc(size of sample to play); ...load data into buffer... snd_pcm_writei (playback_handle,...

Getting rounding problem while converting char[8] to double

I am trying to convert a char array to double, but I got some sort of rounding at the last precision of the the decimal. Please see the code below. #include <stdio.h> #define INT32MAX 017777777777 #define LIND 0 #define MIND 1 typedef int Int32; typedef unsigned int UInt32; typedef short int Int16; union _talign { doub...

Bizarre bug in C

So I have a C program. And I don't think I can post any code snippets due to complexity issues. But I'll outline my error, because it's weird, and see if anyone can give any insights. I set a pointer to NULL. If, in the same function where I set the pointer to NULL, I printf() the pointer (with "%p"), I get 0x0, and when I print that s...

i can't figure out the bug in my program for calculating permutations

#include<stdio.h> #include<conio.h> main() { int i,j,k,x,y,n=4,a[]={1,2,3,4}; //n is the length of the array for(i=0;i<n;i++) { for(k=0;k<(n-2);k++) { for(j=(n-1-k);j>=1;j--) { y=a[j]; a[j]=a[j-1]; a[j-1]=y; for(x=0;x<n;x++) { printf("%d",a[x]); } printf("\t"); }...

Using Gcc on Win32 and linking to msvcrt.dll

I know microsoft recommends against linking to the msvcrt.dll, so please spare me from that warning. They do it all the time in their software (like WinDbg) and they won't introduce breaking changes since all VC6 apps link against msvcrt.dll. Linking against msvcrt.dll has several benefits. Small executable, easy deployment: msvcrt is t...

GCC: Empty program == 23202 bytes?

test.c: int main() { return 0; } I haven't used any flags (I am a newb to gcc) , just the command: gcc test.c I have used the latest TDM build of GCC on win32. The resulting executable is almost 23KB, way too big for an empty program. How can I reduce the size of the executable? ...

Globally override malloc in visual c++

Hi! I'm trying to figure out a way to globally override malloc and related functions in visual c++ (2005). My setup is a dll with statically linked runtime library that consists of both my own c++ code, external c++ and c code. What I want to accomplish is to allow a user of the dll to set their own implementations of the memory allocat...

Debugginng a daemon that terminates unexpectedly

I am writing a daemon in c on linux. It traps signals SIGHUP, SIGTERM, SIGINT, and SIGQUIT, logs them using syslog and quits. If it receives SIGSEGV it core dumps. When these occur everything happens as expected but once in a while it quits...does not exit cleanly, does not log the signal, and does not leave a core dump. I am stumped and...

Big Number Subtraction in C

I just finished my exam in an introductory C course about 20 minutes ago. The first question on the exam caught me somewhat off guard, and involved finding the difference two large numbers. The goal was to take two structures (N1 and N2) by value, and store the difference in a structure passed by reference (N3). We were allowed to assum...

GCC's assembly output of an empty program on x86, win32

I write empty programs to annoy the hell out of stackoverflow coders, NOT. I am just exploring the gnu toolchain. Now the following might be too deep for me, but to continuie the empty program saga I have started to examine the output of the C compiler, the stuff GNU as consumes. gcc version 4.4.0 (TDM-1 mingw32) test.c: int main() ...

Send file over serial port with Linux and C

Hi all, i'm developing an application that reads data from a serial port and sends it over a TCP connection, and vice versa. Unfortunately, while reading data from serial port, it never stops. It does not detect EOF mark, nor EOL or some other special character. So, how could i detect an end of file (or "end of connection") over serial ...

Need help with Windows Journal Record Hook

I want to build a software test automation software and I'm playing around with Windows Hooks for that. So I built the following C code. Can anyone tell me how to correct it ? #include "windows.h" // the call back function LRESULT CALLBACK JournalRecordProc(int code, WPARAM wParam, LPARAM lParam) { HHOOK hhk = 0; if (code > ...

C memory management beginner question

OK, I have this piece of code: typedef struct faux_crit { char dna[DNALEN+1]; //#define'd to 16 int x, y; int age; int p; int dir; } crit; crit *makeguy(int x, int y) { crit *guy; guy = (crit *) malloc(sizeof(crit)); strcpy(guy->dna, makedna()); guy->x = x; guy->y = y; guy->age = guy->p = guy->dir = 0; return gu...

Comprehesive information on serial ports and programming?

What are some comprehesive sources on serial programming? Ideally they would cover things like: history of devices current and future uses how serial devices work protocols and, of course, how to program, preferably in C/C++ ...