c

Does guarding a variable with a pthread mutex guarantee it's also not cached ?

Consider a simple (global in my case) variable: int i; Somewhere this variable is accessed pthread_mutex_lock(i_mutex); if(i == other value) { do_something(); } pthread_mutex_unlock(i_mutex); Another thread updates i while it holds i_mutex . Could the compiler cache the value of i so I don't get the recent value ? Must i be vola...

Getting 64-bit CPUID sample code to compile in VS2008

I'm trying to get some c & ASM sample code I found running in Visual Studio 2008. I don't think the problem is the difference between VS 2005-2008. This example is supposed to get the CPUID on 64-bit systems. (My attempts getting the ASM-only 32-bit examples to compile failed too) I can copy and paste this code into a new project, bu...

Get Mac idle time C or Python

How can i get system idle time (no keys pressed - mouse moved) in C or Python? EDIT: My program suspend a counter when idle time > 10 sec ...

C - program structure (avoiding global variables, includes, etc.)

I'm using C (not C++) and I'm unsure how to avoid using global variables. I have a pretty decent grasp on C, its syntax, and how to write a basic application, but I'm not sure of the proper way to structure the program. How do really big applications avoid the use of global variables? I'm pretty sure there will always need to be at lea...

where is the bug of this C code

I was told this is a buggy code, but I don't know why, some could explain it to me. why there would not be an array out of bound exception when I compile? int a[10]; int j; void main () { int i, j = 42; for (i = 0; i <=10; i++) a[i] = i; printf("%d", j); } ...

skyline algorithm

How do I find the vertices of the broken line that surrounds the silhouette in this image? A possible input for the example above is: WIDTH HEIGHT POSITION 3 9 17 5 9 9 12 4 8 3 11 3 10 7 1 2 3 19 So for this example the solution would be [(1, ...

Storing a uint64_t data type in a char type field present in a union in C

Hi I have the following union which is part of a larger struct and I want to store a uint64_t (64 bits size) data in this union. However i want to store it by accessing the id_data field since the others are not large enough for a complete uint64_t. But i dont know how to assign my uint64_t data into this id_data field. I know how to ...

Set the first byte to 0 or use memset to "reset" entire buffer

In C programming, whenever I try to perform first time cat, I need to TCHAR file_name[1024]; // Use memset or set the first byte to 0? file_name[0] = 0; _tcscat(file_name, TEMP_DIRECTORY_PATH); _tcscat(file_name, file); I see most programmers are using memset. But, for me, I just set the first byte to 0, to let _tcscat knows where to ...

Text file to string array in plain c?

I want to load a txt file into an array like file() does in php. I want to be able to access different lines like array[N] (which should contain the entire line N from the file), then I would need to remove each array element after using it to the array will decrease size until reaching 0 and the program will finish. I know how to read t...

Can I assign the same file pointer a second file?

function() { FILE *ptr; ptr = fileopen(file1.txt) fprint(ptr, some text) //print to file 1 if(second file needed) { ptr = fileopen(file2.txt) //open a second file, assign to same file pointer fprint(ptr, some text) //print to file 2 not working here? } } EDIT: Not printing to second file...However...

How to determine which object files are actually necessary for linking?

I had to modify some open source code to use in a C project. Instead of building a library from the modified code, I'd like to just compile and build an executable from my own source combined with the modified open source code. The goal is to have a stand-alone package that can be distributed. I can get this to work just fine using th...

wchar_t pointer manipulation - How to check whether it is ended with L".xls"

I am using C. I have a wchar_t pointer which pointing a a file path. I was wondering, how I can check, whether it is ended with L".xls"? Any function call I can use? ...

WH_GETMESSAGE global hook not working

I am trying to set a global GetMessage hook on all threads. This is my DLL: #include <windows.h> __declspec(dllexport) LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam) { MessageBeep(0); return CallNextHookEx(NULL, nCode, wParam, lParam); } As you can see, it's not much. I just want it to call MessageBeep when...

Why doesn't Direct3D have it's own vertex structure?

I've always wondered the reasoning behind why we must always define D3DVERTEX. Is it because Microsoft wants to allow the opportunity to put this in a class and overload operators, or is there another reason? Thanks ...

gcc gives error while using fmod()

Sample code for fmod #include <stdio.h> #include <math.h> int main(void) { double x = 0.14527, y = 3.14159; printf("fmod(x, y) = %.6lf\n", fmod(x, y)); return 0; } $ gcc main.c -o main I get /tmp/ccztJO01.o: In function `main': main.c:(.text+0x4d): undefined reference to `fmod' collect2: ld returned 1 ...

Communication between EXE and Global Hook DLL

I have an application loading a library containing a callback function for a global GetMessage hook. I get the procedure and pass it to SetWindowsHookEx to be set for all running threads. The problem is that I want the DLL function to, under a certain circumstance like a keypress, tell the original application to exit, not all applicati...

How to printf "unsigned long" in C?

I can never understand how to print unsigned long datatype in C. Suppose boo is an unsigned long, then I try: printf("%lu\n", unsigned_boo) printf("%du\n", unsigned_boo) printf("%ud\n", unsigned_boo) printf("%ll\n", unsigned_boo) printf("%ld\n", unsigned_boo) printf("%dl\n", unsigned_boo) And all of them print some kind of -12312312...

Convert char * to short and char

char * x="a"; how would i convert it to char y='a'; also if i have a short char * a="100" how can i convert it to short b=100 thanks ...

K&R Exercise 4-6 Problem thinking of a solution.

In k&r we have managed to create an RPN. The exercise now is to: Add commands for handling variables, (It's easy to provide twenty-six variables with single letter names.) Add a variable for the most recently printed value. So this is meant to act somewhat like the Python interpreter, where we can do: >>>5 >>>_ (where _ prints 5) >>>...

Question on void **

int size=2; char *a0; char **allargs; short *a1; void **args; allargs=(char **)malloc(size*sizeof(char *)); allargs[0]="a"; allargs[1]="100"; args=(void **)malloc(size*sizeof(void *)); a0 = malloc(sizeof(char *)); *a0=(*allargs[0]); args[0]=(void *)&a0; fprintf(stderr,"assigned %c %c\n",*a0,*((char *)args[0])); a1 = malloc(sizeof(short...