c

NSLog the method name with objective-C in iPhone

Currently, we are defining ourselves an extended log mechanism to print out the class name and the source line number of the log. #define NCLog(s, ...) NSLog(@"<%@:%d> %@", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], \ __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__]) For example, when I call NCLog(@"Hel...

To find the substring in a given text.. C programm..

char *substring(char *text, int position, int length) { int i, j=0; char *temp ; for(i=position-1; i<position+length-1; i++) { temp[j++] = text[i]; } temp[j] = '\0'; return temp; } Hi What is the error in the following code.. I am trying to run this on Fedora Machine.. And its giving me a run-time error "Seg...

When will this condition evaluate to False?

Under what circumstances will the "False" part of the following code be executed? x = 20; y = -30; if (x > y) { // True part } else { // False part } NB: Language is C, compiler is gcc (although some other compilers may also do the same thing). ...

C99 strict aliasing rules in C++ (GCC)

As far as I understand, GCC supports all of its C99 features in C++. But how is C99 strict aliasing handled in C++ code? I know that casting with C casts between unrelated types is not strict-aliasing-safe and may generate incorrect code, but what about C++? Since strict aliasing is not part of C++ standard (is that correct?), GCC must ...

Hi, i want to implement a small routing table for my learning? I know it is implemented using radix/patricia tree in routers?

Hi, i want to implement a small routing table for my learning? I know it is implemented using radix/patricia tree in routers? Can someone give me an idea on how to go about implementing the same? The major issue i feel is storing IP ADDRESS. For example : 10.1.1.0 network next hop 20.1.1.1 10.1.0.0 network next hop 40.1.1...

semicolon in C++?

Here is the question Is "missing semicolon" error really required? why not treat it as a warning? Why I am asking this stupid question? When I compile this code int f = 1 int h=2; the compiler intelligently tells me that where I am missing it. but to me its like - "If you know it, just treat it as if its there and go ahead. (Later ...

Clone existing structs with different alignment in Visual C++

Is there a way to clone an existing struct with different member alignment in Visual C++? Here is the background: I use an 3rd-party library, which defines several structs. To fill up the structs, I pass the address of the struct instances to some functions. Unfortunately, the functions only returns unaligned buffer, so that data of so...

How to lock file in Windows?

How to lock file in Windows so that this file can be opened/read/wrote only by one process? I found out that file can be locked with CreateFile by giving 0 to dwShareMode flag. It works but only the returned handle can be used to work with file. But I want to be able to lock the file to other processes and at the same time to create mul...

Why doesn't GCC produce a warning when assigning a signed literal to an unsigned type?

Several questions on this website reveal pitfalls when mixing signed and unsigned types and most compilers seem to do a good job about generating warnings of this type. However, GCC doesn't seem to care when assigning a signed constant to an unsigned type! Consider the following program: /* foo.c */ #include <stdio.h> int main(void) {...

How to force input from tty

I'd like to write a program which reads user input only from tty, instead of redirected stdin pipes, like passwd and ssh do. Is there any approach? Many thanks ...

How to make an C vector or array from an NSArray?

Is that possible? I've seen no method that would generate a plain old C vector or array. I have just NSNumber objects in my array which I need as C vector or array. ...

A/UX cc compiler errors on trivial code: "declared argument argc is missing"

On a quite ancient UNIX (Apple A/UX 3.0.1 for 680x0 processors) using the built-in c compiler (cc), this issue arrises. Here is the code I'm trying to compile: #include <stdlib.h> #include <stdio.h> int main() int argc; char **argv; { if (argc > 1) puts(argv[1]); return (EXIT_SUCCESS); } And here is the o...

whether rand_r is real thread safe?

Well, rand_r function is supposed to be a thread safe function. However, by its implementation, I cannot believe it could make itself not change by other threads. Suppose that two threads will invoke rand_r in the same time with the same variable seed. So read-write race will occur. The code rand_r implemented by glibc is listed below. A...

Simple socket server for Linux

Hi all, I want TCP/IP based socket server application or code for Linux, which performs a very simple operation: reads xml string from one of the connected socket clients and forwards it to all socket clients which are connected to it. I have such client server application developed in cocoa, but according to my requirements now I need...

Optimizing a shared buffer in a producer/consumer multithreaded environment

I have some project where I have a single producer thread which writes events into a buffer, and an additional single consumer thread which takes events from the buffer. My goal is to optimize this thing for a single dual-core machine to achieve maximum throughput. Currently, I am using some simple lock-free ring buffer (lock-free is po...

C: picking a random item based on probabilities

There's a similar question, I know, but it confused me, so I thought it easier to ask in my way. So I have an array of values, positive and negative. The higher they are, the more probability they have of being chosen. I'm having trouble actually figuring out how to assign the probabilities and then randomly choose one. I'm guessing the...

Kernel module for /proc

How to write a kernel module that creates a directory in /proc named mymod and a file in it name is mymodfile. This file should accept a number ranged from 1 to 3 when written into it and return the following messages when read based on the number already written into it: • 1: Current system time (in microseconds precision) • 2: System ...

What is the point of the logical operators in C?

I was just wondering if there is an XOR logical operator in C (something like && for AND but for XOR). I know I can split an XOR into ANDs, NOTs and ORs but a simple XOR would be much better. Then it occurred to me that if I use the normal XOR bitwise operator between two conditions, it might just work. And for my tests it did. Consider...

Using many mutex locks

I have a large tree structure on which several threads are working at the same time. Ideally, I would like to have an individual mutex lock for each cell. I looked at the definition of pthread_mutex_t in bits/pthreadtypes.h and it is fairly short, so the memory usage should not be an issue in my case. However, is there any performanc...

How to find the size of integer array...

How to find the size of an integer array in C. Any method available without traversing the whole array once, to find out the size of the array.. Thankss.. ...