c

C Foreach or Similar

Almost all languages have a foreach loop (function) or something similar. I wonder if C has one? Can you post some example code? ...

Any good tutorials to help me to create header files for randomizing in C, C++

I want to create a header file for C, C++ which is specific to help in randomization. ...

Weird bit-shift behavior

I'm in the process of porting some ANSI C++ code to C#... and this is killing me right now. Both tests have value = 6844268. Test code: value >> 12 value & 0x00000FFF C++ returns 18273 and 29497, whereas C# returns 1670 and 3948. I've tried every possible combination of types in C# (int, uint, long, ulong, Int64...), but no go :( O...

Casting a char to an unsigned short: what happens behind the scenes?

Given this field: char lookup_ext[8192] = {0}; // Gets filled later And this statement: unsigned short *slt = (unsigned short*) lookup_ext; What happens behind the scenes? lookup_ext[1669] returns 67 = 0100 0011 (C), lookup_ext[1670] returns 78 = 0100 1110 (N) and lookup_ext[1671] returns 68 = 0100 0100 (D); yet slt[1670] returns ...

Randomize a string in C

I'm trying to generate random permutations of an 80-character fixed string in C. Much to my dismay, the system I'm working on lacks strfry(). What's the best way for me to generate a random permutation of this string? Since this will be looped over approx. 100,000 times, performance is an issue. ...

How do I convert a string in C++ or C to an integer array?

How do I convert a string into an array of integers? Can I use sstream, because atoi doesn't work?! ...

Memory Efficient Programming

What are some best practice for "Memory Efficient C programming". Mostly for embedded/mobile device what should be the guidelines for having low memory consumptions ? I guess there should be separate guideline for a) code memory b) data memory ...

Do you prefer "if (var)" or "if (var != 0)"?

I've been programming in C-derived languages for a couple of decades now. Somewhere along the line, I decided that I no longer wanted to write: if (var) // in C if ($var) # in Perl when what I meant was: if (var != 0) if (defined $var and $var ne '') I think part of it is that I have a strongly-typed brain and in my mind, "if...

Download Web Page

How do i Download Web Page using C (not with libcurl), OS: Win32 Duplicate of Fetch Web Page in C [closed] and How to fetch HTML in C/C++. ...

which one to use linked list or static arrays?

hi all I have a structure in C which resembles that of a database table record. Now when I query the table using select, I do not know how many records I will get. I want to store all the returned records from the select query in a array of my structure data type. Which method is best? Method 1: find array size and allocate first ge...

On Linux SCHED_FIFO and SCHED_RR

I'm writing a a very small daemon that must remain responsive even when a system is under severe stress. I'm looking at the differences between SCHED_FIFO and SCHED_RR in regards to scheduling, as well as trying to determine a sensible priority. Which scheduler would be appropriate for a small but critical monitoring daemon, what priori...

Confused about accessing struct members via a pointer

I'm new to C, and am confused by results I'm getting when referencing a member of a struct via a pointer. See the following code for an example. What's happening when I reference tst->number the first time? What fundamental thing am I missing here? #include <stdio.h> #include <stdlib.h> typedef struct { int number...

Casting related invalid lvalue in unary &

I'm getting the invalid lvalue error using gcc 3.4.6. The line that invokes the error has a function that receives a pointer to SetElement (which is typedef void*). I'm trying to achieve this using casting from another type (also typedef void*) the following way: &((SetElement)my_var). gcc complains about this. Any idea what can be done?...

Number of points on elliptic curve

If you have an elliptic curve in the form of: y^2 = x^3 + a*x + b (mod p) Is there a good program to calculate the number of points on this curve? I have read about Schoof's and Schoof-Elkies-Atkin (SEA) algorithm, but I'm looking for open source implementations. Does anyone know a good program that can do this? Also if a is 1 and b...

Kind of self-modifying program in C

Is it possible to write a C function that does the following? Allocate a bunch of memory in the heap Writes machine code in it Executes those machines instructions Of course, I would have to restore the state of the stack to what it was prior to the execution of those machine instructions manually, but I want to know if this is feasi...

Creating a web server in pure C

Im doing a little project on my university which now involves in creating a Webserver only using C. I know a little about the HTTP 1.1 and i've created a webserver in C# before. However, I'd like to see a nice little tutorial on how you either begin socket programming and using multi threads in C or a complete "How to create a webserver...

Multithreaded paranoia

This is a complex question, please consider carefully before answering. Consider this situation. Two threads (a reader and a writer) access a single global int. Is this safe? Normally, I would respond without thought, yes! However, it seems to me that Herb Sutter doesn't think so. In his articles on effective concurrency he discuss...

Accessing COM interface from C or C++ in Windows environment

I'm relatively new to the Component Object Model specification - I have a simple question: How can I access a COM interface from a C or C++ application For instance, accessing Microsoft Excel COM interface to perform basic operations, without user intervention. Kind regards ...

Using POSIX message queues instead of TCP sockets - how to establish "connection"?

I have client and server programs which now communicate via TCP. I'm trying out using POSIX message queues instead (in cases where the client and server are on the same machine, of course). My hope is that it will improve performance (specifically via reduced latency). I've worked out most of it, but am not sure about one thing: how t...

Accessing an application's COM interface using C++ or C

In response to question, how can I (or find more information to) automate certain functionality without user intervention, from a C++ (or C) using: ATL Or Automation code directly in C/C++ Regards ...