c

Pointer to literal value

Suppose I have a constant defined in a header file #define THIS_CONST 'A' I want to write this constant to a stream. I do something like: char c = THIS_CONST; write(fd, &c, sizeof(c)) However, what I would like to do for brevity and clarity is: write(fd, &THIS_CONST, sizeof(char)); // error //...

C: Looping without using looping statements or recursion

I want to write a C function that will print 1 to N one per each line on the stdout where N is a int parameter to the function. The function should not use while, for, do-while loops, goto statement, recursion, and switch statement. Is it possible? I want to find an answer to this as this a challenge question ...

Can I do a copy-on-write memcpy in Linux?

I have some code where I frequently copy a large block of memory, often after making only very small changes to it. I have implemented a system which tracks the changes, but I thought it might be nice, if possible to tell the OS to do a 'copy-on-write' of the memory, and let it deal with only making a copy of those parts which change. H...

I need raw read data from and write data into drive..help

I need raw read data from and write data into drive..not via existed filesystem like fat32 or something....i just wanna raw write read...... I was told in Windows i can use CreateFile WriteFile and ReadFile APIs to access data in drive directly... but I dont know in Linux whether there r similar functions.... dont tell me to use hardwar...

Specifically, what's dangerous about casting the result of malloc?

Now before people start marking this a dup, I've read all the following, none of which provide the answer I'm looking for: C FAQ: What's wrong with casting malloc's return value? SO: Should I explicitly cast malloc()’s return value? SO: Needless pointer-casts in C SO: Do I cast the result of malloc? Both the C FAQ and many answers to...

Do Implict Function Declarations in C Actually Generate Object Code?

In the course of this discussion about casting the return value of malloc many people have claimed that the implicit declaration of malloc would cause the return value to be converted to int then reconverted back to T* possibly resulting in truncation of the pointer in situations where: sizeof(int) < sizeof(void*) This would imply tha...

How to [portably] get DBL_EPSILON in C/C++

I am using gcc 3.4 on linux (AS 3) and trying to figure out to get DBL_EPSILON number, or at least a decent approximation. How to get it programmatically? How can I do that? ...

Handling huge numbers C, Java, Informix

Hi all, We are in a situation to handle numbers with a maximum of 15 digits. We need to parse this value from a text file, through C, store it in Informix table. There is another Java component that reads these values, does mathematical operations and computes a result. I have been doing some research on this and found that the int8 da...

MSMQ and queue errors

On the system our company develops we rely on Microsoft MQ to do IPC. So I've got some questions about how it works: 1 - If I've opened a queue, and while sending some data the result is a MQ_ERROR. What is the status of the queue after that? It remains open, or it will be closed? 2 - The same question, but for errors when setting the...

What are other modern, free analogs of Squeak and Esterel?

A long time ago, Rob Pike and Luca Cardelli wrote a paper called "Squeak: a language for communicating with mice". It was based on Hoare's communicating sequential processes, but it was compiled into single-threaded C code - no threads or scheduler at runtime. However, I can't find a compiler for Squeak, and Rob Pike went on to write new...

Initialize window code Mac OS X

I'm currently reading "the red book" for learning OpenGL properly, and in one of the first examples, the author writes a line that says "InitializeAWindowPlease();" as a place holder for the code that will make a window to draw the OpenGL content in. Since I'm using Xcode for my programing, I know that I "get" a window to work with aut...

Synchronized Producer & Consumer with Circular buffer

I've got a producer and a consumer. The producer writes fixed size items on a given shared memory area, and the consumer retrieves them. The producer can be noticeably slower or faster than the consumer, randomly. What we want is that If the producer is running faster than the consumer, when it fills the circular buffer, it keeps wri...

thread level memory consumption of process

How do I get per thread based memory consumption of a process in Linux? I understand that we can use /proc/pid/task/tid/statm, but thats not helping my case. All the threads show same value and its same as PID's statm. We can do valgrind but I am not looking for any invalid read/write or leaks. Valgrind will not tell me any thread leve...

GDB structure output

I haven't worked with gdb for a long time and this feels like a basic question. I am trying to observe a structure as it changes but rather than break at a specific point and print it out I'd rather let the application run as normal and give me a snapshot of the structure at a specific point. Think a breakpoint that performs an action ...

What does this line of code mean?

Hi, I am wondering what this line of code mean? b = (gen_rand_uniform()>0.5)?1:0; The gren_rand_uniform() is a function to generate random 0 and 1 numbers. However I don't get the meaning for >0.5 and 1:0. I know this is supposed to be a basic question, please bear with me. Thanks! ...

Optimal way to free() a malloc'ed 2D array in C

Supposing I have a 2 dimensional array which was created with something like this, char **foo = (char **) malloc(height * sizeof(char *)); for(i = 0; i <= height; i++) foo[i] = (char *) malloc (width * sizeof(char *)); First of all, Is this even the right way to create an array like this?. The catch here is, 'height' and 'width' ...

ASCII strings and endianness

An intern who works with me showed me an exam he had taken in computer science about endianness issues. There was a question that showed an ASCII string "My-Pizza", and the student had to show how that string would be represented in memory on a little endian computer. Of course, this sounds like a trick question because ASCII strings a...

Why does this division result in zero?

I was writing this code in C when I encountered the following problem. #include <stdio.h> int main() { int i=2; int j=3; int k,l; float a,b; k=i/j*j; l=j/i*i; a=i/j*j; b=j/i*i; printf("%d %d %f %f\n",k,l,a,b); return 0; } Can anyone tell me why the code is returning zero for the first and third variables ...

How to make gcc or ld report undefined symbols but not fail?

If you compile a shared library with GCC and pass the "-z defs" flag (which I think just gets passed blindly on to ld) then you get a nice report of what symbols are not defined, and ld fails (no .so file is created). On the other hand, if you don't specify "-z defs" or explicitly specify "-z nodefs" (the default), then a .so will be pro...

Arrange numbers in ascending order using if and swap statement

How can I arrange numbers in ascending order using if and swap statements? Could someone give me some direction? ...