c

to build persistent and non-blocking web server

i'm supposed to make persistent HTTP web server which is non-blocking. so far i have used the select() to accept multiple request from different clients. can anybody tell me how should i code to make web server persistent?? ...

Type casting, c language problem.

Hello! I'm not able to understand some typecasting syntaxes. For eg. float f=7.0; short s=*(short *)&f; What's happening here short s=*(short *)&f? Looks like we're casting something as a pointer to a short and then initializing s to value stored in the address pointed to by something. Now, this something looks like the address of va...

Embedded C code review

Hi, I am required to write a function that uses a look up table for ADC values for temperature sensor analog input, and it finds out the temperature given an ADC value by "interpolating" - linear approximation. I have created a function and written some test cases for it, I want to know if there is something which you guys can suggest to...

comparing int with size_t

If i have a int and a size_t variable,can i compare them like: int i=1; size_t y=2; if(i==y) do something.. or i have to type-cast one of them? ...

What does mmap do?

mmap(NULL, n, PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); hi... i am trying to figure out the meaning of above code...? ...

chdir() not affecting environment variable PWD

When I use chdir() to change the current working directory, why doesn't the getenv("PWD") give the present working directory? Do I need to setenv("PWD",newDir,1) also? void intChangeDir(char *newDir) { if( chdir(newDir)==0 ) { printf("Directory changed. The present working directory is \"%s\" \"%s\"\n",gete...

array index and address return same value.

#include<stdio.h> int main(void) { int a[3] = {1,2,3}; printf("\n\t %u %u %u \t\n",a,&a,&a+1); return 0; } Now i don't get why a and &a return the same value, what is the reasoning and the practical application behind it? Also what is the type of &a and could i also do &(&a) ? ...

array is of same type and linked list is of different type

In an interview when I ask the recent graduate students that what is the difference between array and linked list, the first answer normally is "In array you have same data types and in the linked list you can have different data types." When I told them to explain they will say that they have just read it somewhere or that they don't kn...

select() doesn't wait

Hi, I've to read program log file and to do that I wanted to use select() and read() #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/select.h> #include <fcntl.h> #include <stdio.h> int main() { int fd = -1; fd_set fds; struct timeval tv; int rc; char buffer[4096]; char * log_path = "/...

Trouble with dup2

After incorporating Ben Voigt's answer into the code, it appears to work Original question: I'm trying to use dup2 to: pass the output of "ls -al" as input to "grep foo", whose output becomes input for "grep bar", which finally outputs to stdout. The final output is (blank), the file "in" is (blank) & the file "out" has the outp...

2d array confusion

Why is the memory of a 2d array accessed by two parameters and not just by one (ignoring pointers).Why is it that the memory diagram is in terms of rows and columns and not straight (horizontal),Further more why is it said that 2d array is a array of arrays but i dont get that . ...

c questions regarding memory

O.S stores in which memory area ...

How to put a GSList inside a GHashTable ?

I would like to put a GSList inside a GHashTable, here is how I managed all of this: #include <glib.h> #include <glib/gprintf.h> typedef struct Foo_ { GHashTable * bar; } Foo; Foo * create() { Foo * foo = g_malloc(sizeof(Foo)); foo->bar = g_hash_table_new(NULL, NULL); return foo; } void add_element(Foo * foo, gchar * key, gpo...

Using dup2 for piping

How do I use dup2 to perform the following command? ls -al | grep alpha | more ...

Can't handle Floating Point Exception (FPE) for the second time.

I have written a program that handles signal for Floating point exception and I am using Ubuntu 10.4. Here's my source code : #include <stdio.h> #include <stdlib.h> #include <signal.h> #include <setjmp.h> sigjmp_buf mark; void GeneralHandler(int signo) { switch(signo) { case SIGFPE: printf("\nERROR : Inva...

Does any operating system implement buffering for malloc() ?

A lot of c/malloc()'s in a for/while/do can consume a lot of time so I am curious if any operating system buffers memory for fast mallocs. I have been pondering if I could speed up malloc's by writing a "greedy" wrapper for malloc. E.g. when I ask for 1MB of memory the initial allocator would allocate 10MB and on the 2nd, 3rd, 4th etc.....

Writing a D (D2) binding for existing C libraries

I'd really like to get more into D, but the lack of good library support is really hindering me. Therefore I'd like to create some D bindings for existing C libraries I'd like to use. I've never done any binding, but it doesn't look too difficult either. I'm planning to do this for D2 (not specifically D1, but if it could be for both, e...

Warning while resizing integer array.

In my program I am trying to resize array using malloc function. #include <stdio.h> int main(void) { int list[5],i; int* ptr = &list; for(i = 0; i < 5; i++) list[i] = i; for(i = 0; i < 5; i++) printf("%d\n", list[i]); printf("----------------------------------------\n"); ptr = malloc(10); ...

problem with generating rand number between 0 and 1 using C language

Hi all I am trying to generate some numbers with the rand() function like below: int main() { int i=0 for(i=0;i<20;i++) { printf("%f\n",rand_md()); } return 0; } float rand_md() { return (float)rand()/(RAND_MAX+1.0); } But when I run gcc rand.c, I get the error like below: rand.c: In function ‘main’: ra...

Regex Expressions in C programming ?

Hello all, how can i use Regex Expressions in C programming? for example if i want to find a line in a file DAEMONS=(sysklogd network sshd !netfs !crond) then print each daemon in separate line like this sysklogd network sshd !netfs !crond here what i did so far #include <stdio.h> #include <stdlib.h> #include <string.h> #...