c

Barriers for thread syncing

I'm creating n threads & then starting then execution after a barrier breakdown. In global data space: int bkdown = 0; In main(): pthread_barrier_init(&bar,NULL,n); for(i=0;i<n;i++) { pthread_create(&threadIdArray[i],NULL,runner,NULL); if(i==n-2)printf("breakdown imminent!\n"); if(i==n-1)printf("breakdown already occurred!\n"); } ...

Why an Operating System(OS) is called as hardware dependent/platform dependent

Why we are saying that the OS is purely hardware dependent (other than hardware peripherals like RAM/USB etc)? The word hardware independence means, the OS should run on any platform with out any underlying hardware abstraction layer like ARM/x86/xtensa/starcore etc etc. Can you please give me the exact hardware dependencies in a simple...

What is the correct type for this parameter?

This one is for all you ALSA guys. I need a sanity check here. I am using the the alsa-lib api to play sounds and the function that I am using to write the data to the driver is snd_pcm_sframes_t snd_pcm_writei (snd_pcm_t* pcm, const void* buffer, snd_pcm_uframes_t siz...

Where does the word "pragma" come from?

So I know what pragma is, and what it's used for, but what is the meaning of the word itself? I've used it many times in code, but I never really knew what the word actually means or stands for. ...

preprocessor functions evaluated at compile time in C

I want to write preprocessor functions/arrays that are evaluated at compile time. For example, if I define #define MYARR[] {5,4,3,2,1,0} then, the code int x = R[0]; should be presented as int x = 5; to the compiler. (of course only literals can be used in the index). This is important if code size/memory is critical and we dont...

What are all the ways to allocate memory in C and how do they differ?

I'm aware of the following: malloc calloc realloc What are the differences between these? Why does malloc seem to be used almost exclusively? Are there behavioral differences between compilers? ...

C: fscanf and character/string size

Hello. I am parsing a text (css) file using fscanf. The basic goal is simple; I want to pull out anything that matches this pattern: @import "some/file/somewhere.css"; So I'm using fscanf, telling it to read and discard everything up to a '@' character and then store everything until it reaches a ';' character. Here's the function that...

Searching for a sub 'string' in another 'string'

How would I write an efficient algorithm to search for a subset array of integers in another array in C? For example: unsigned a[] = {42, 72, 61, 1023, 84, 42, 42, 193, 302, 72}; unsigned long al = 10; unsigned b[] = {61, 1023, 84}; unsigned long bl = 3; I've tried a brute-force approach, by looping through a and then looping through ...

Division by power of 2 with round toward zero

I need to compute a number (a/(2**b) using only bitwise operators such as ! & ^ ~ and shifts. I was given the following hint but I'm new to C and I dont know what the code means: int bias = x>0 ? 0 : ((1<<n)-1); Can anyone explain it to me? I thought a>>b would work but I dont think it works for negative numbers. ...

Just how many printf and scanf variants are there?

There are so many different versions of printf and scanf in C that it brings out a chuckle in me. Let's start: printf: original implementation; uses format then the values as arguments fprintf: the same, but takes a FILE pointer before format sprintf: takes a char pointer before format snprintf: same as above, but limits size written f...

what would be a good alternative to ncurses that will work under emacs shell?

I'm an avid Emacs user and love shell-mode that in my opinion brings the best of two worlds: Emacs buffers and terminal emulators. For me shell-mode's biggest down fall is that ncurses based applications wouldn't render correctly. Having the need to design & code a little monitoring app in C that reads and write to standard input & out...

Visual C++ handling C files as C

Microsoft's IDE handles C files fine, except that it syntax highlights them as C++ and (possibly related?) it shows them under the Source Files list with the C++ icon (even though Windows Explorer correctly uses the C icon). Is that just the way it does things, or does it indicate I'm missing a setting or somesuch someplace? edit: I sp...

gcc, include file dependencies

hello Is there option (or some utility) to generate graph of dependencies for given file? I am trying to track down who brings an include file into compilation unit. ...

C for(;;) - What does it mean

I've seen for(;;) and for ( ; *s != '\0'; s++) Why is it blank like that. Thanks. ...

Testing C/C++ source code

Possible Duplicates: Suggestion for UnitTest tools for C++ Choosing a C++ unit testing tool/framework Unit Testing C Code This is something I've been wondering now that we have to use C/C++ as base language for some of our university projects : In Java there's JUnit, In PHP there's PHPUnit etc. How are unit testing don...

C linked list search function

Edit: Stupid error. I found it. Thanks for the help though. Sorry i can't keep my code up ...

how to pass a package to libnet(1.1)?

I hava captured some packages by libpcap and built ip header. The package is a const u_char type and named payload, so how to pass the payload to libnet , then I can forward the payload to another ip ? which libnet api can work for this ? ...

libdtrace buffered output

I'm trying to use dtrace through libdtrace (on Snow Leopard; 10.6.4). I want to catch the printed output of my dtrace script within my own program. One way to do so would be to have the output go to a temporary file and read it from there. However, libdtrace supports a callback function to catch the output directly which I'd prefer. I a...

Odd function pointer warning in C

ueach is a function that loops through a Unicode string, and running the callback on each character by passing a single-character string to it. string ueach(string s, void *function(string)) { unsigned long i; for (i = 0; i < s.length; i++) function(uchar(s, i)); } If I have a callback testing: void testing(string c) ...

convert string into signed int

I want to convert a string into a signed int. Following is the requirement. I have stored hex value as a string in buffer. Now I want to convert that value into signed int. buf = "fb869e" Convert this into signed int. So o/p should be -293218. but when I'm trying to convert using strtol I'm getting 16483998. So what I should I do? ...