c

Is this a safe way to share read-only memory with child processes?

I want to allocate and initialise a fairly large chunk of contiguous memory (~1GB), then mark it as read-only and fork multiple (say several dozen) child processes which will use it, without making their own copies of the memory (the machine won't have enough memory for this). Am I right in thinking that if I malloc the memory as usual,...

C, Unix Domain Sockets, Ancillary data, and GCC; Using CMSG_DATA macro

How can I do this: *(int *)CMSG_DATA(hdr) = fd2pass; Without GCC raising this: error: dereferencing type-punned pointer will break strict-aliasing rules In a way compatible with these options: -Wall -Werror -pedantic ...

Portable user defined character class division in C89 by a lookup table, would you do this?

static const int class[UCHAR_MAX] = { [(unsigned char)'a'] = LOWER, /*macro value classifying the characters*/ [(unsigned char)'b'] = LOWER, . . . } This is just an idea. Is it a bad one? ...

Swapping objects using pointers

I'm trying to swap objects for a homework problem that uses void pointers to swap objects. The declaration of my function has to be: void swap(void *a, void *b, size_t size); I'm not looking for the exact code how to do it so I can figure it out by myself, but I'm not sure if I understand it correctly. I found that one problem is by...

#if 0 as a define

I need a way to define a FLAGS_IF macro (or equivalent) such that FLAGS_IF(expression) <block_of_code> FLAGS_ENDIF when compiling in debug (e.g. with a specific compiler switch) compiles to if (MyFunction(expression)) { <block_of_code> } whereas in release does not result in any instruction, just as it was like this #if 0 ...

Where can I get started with Unicode-friendly programming in C?

So, I’m working on a plain-C (ANSI 9899:1999) project, and am trying to figure out where to get started re: Unicode, UTF-8, and all that jazz. Specifically, it’s a language interpreter project, and I have two primary places where I’ll need to handle Unicode: reading in source files (the language ostensibly supports Unicode identifiers a...

Using C to remove certain characters then put rest in array

How can I take a string (in this case it'll be loaded from a file) then remove certain characters and store them in an array. Ex: f.e.d.r.t.g.f remove "." to get f e d r t g f in an array where I can manipulate each individually ...

Emulate floating point string conversion behaviour of Linux on Windows

I've encountered an annoying problem in outputting a floating point number. When I format 11.545 with a precision of 2 decimal points on Windows it outputs "11.55", as I would expect. However, when I do the same on Linux the output is "11.54"! I originally encountered the problem in Python, but further investigation showed that the diff...

anomalous behaviour of scanf

hello, wondering all about C, can you demystify this I am using turbo C I have this code scanf(“%d , %d”,&a,&b); printf(“%d,%d”,a,b); scanf(”%c”,&c); printf(“%d,%d”,a,b); then scanf for doesnt scan value of c output is : 1,2 if I use this scanf(“%d , %d”,&a,&b); printf(“%d,%d”,a,b); scanf(”%c ”,&c);//note a blank after %c prin...

C programming pointer arrays read file

The line *array[cnt] = thing causes a seg fault and I don't know why. Any ideas to fix this? long *Load_File(char *Filename, int *Size) { FILE *fp; if((fp = fopen(Filename,"r")) == NULL) { printf("Cannot open file.\n"); exit(-1); } fscanf(fp,"%d",Size); int cnt = 0; int items = *Size; l...

high performance udp server. blocking or non-blocking? c

hi, i've been doing a lot of reading on blocking vs non-blocking sockets for udp, but am having a hard time understanding the merits of one over the other. the overwhelming majority of comments on the internet seem to indicate that non-blocking is better, but aren't very specific as to what scenarios they would be better in and i've foun...

PyEval_CallObject failing in loop occasionally

Hi. I am struggling a bit with the Python C API. I am calling a python method to do some game AI at about 60hz. It works most of the time but every second or so the call to PyEval_CallObject results in a NULL return value. If I correctly detect the error and continue looping, all is well for the next second or so, whereupon the error o...

Converting C to PHP?

Is there any tool available that takes C code as input and outputs valid PHP files? I'd like to use the SAC API but all the implementations currently available are in C, Java, Ruby and Perl. I wonder if the C implementation can be converted to PHP easily. ...

Reassemble float from bytes inline

I'm working with HiTech PICC32 on the PIC32MX series of microprocessors, but I think this question is general enough for anyone knowledgable in C. (This is almost equivalent to C90, with sizeof(int) = sizeof(long) = sizeof(float) = 4.) Let's say I read a 4-byte word of data that represents a float. I can quickly convert it to its actual...

fptd 0.17 and directory access

I'm working on FTP demon 1.7 I have following questions. How can I restrict a user to one particular directory? Lets say a user logs in as "admin", he should be having access to only "/tmp" directory, nothing else. I'm going through ftpd's code and trying to modify as per my needs. Any good reference/documents to get hang of current co...

can scanf be terminated on pressing some specific key other than enter

Hello, I have a situation here i am taking input from user using scanf can I terminate the scanf as soon as user presses the # key please enlighten me on this ...

What's the fastest vector/matrix math library in C for iPhone game?

As the title, I'm finding vector/matrix library in C optimized for iPhone/iPod processors. Or just generally fast. ---(edit)--- I'm sorry for unclear question. I'm looking for fast lib for commercial games for iPhone/iPod. So GPL lib cannot be used. However, I'll stop finding fastest lib, it maybe meaningless. ...

Compiling a C library so it can be used in an iPhone static library

I've never done this before, so I'm not sure where to even start. I have a few projects where I want to use the liblio library, both on the iPhone and OS X. I've put the installation instructions in a gist. Here are my questions, and I'll try and edit these questions as I figure them out. Do I want to use make install to compile these...

How to avoid using a button in an MFC application?

Hi! This is my first my MFC application, and unfortunately i don't understand, how it works. I found a simple MFC application, which gets the file list of a given path. I modifyed this code for my needs, but now i have one problem. What my application should do is the following. It reads two drive letters from a file. Then gets the file...

Reusing socket descriptor on connection failure

In my client code (Linux), I am following these steps to connect to a socket: Creating a socket sockDesc = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) Connecting it (retry for 'x' time in case of failure) connect(sockDesc, (sockaddr *) &destAddr, sizeof(destAddr)) (After filling the destAddr fields) Using the socket for se...