c

C optimization breaks algorithm

I am programming an algorithm that contains 4 nested for loops. The problem is at at each level a pointer is updated. The innermost loop only uses 1 of the pointers. The algorithm does a complicated count. When I include a debugging statement that logs the combination of the indexes and the results of the count I get the correct answ...

How to calculate dawn / dusk times

Hi, I'm currently using this code to calculate the sunrise / sunset times. (To be more precise, I'm looking for civil dawn / civil dusk times which are defined as the time when the sun is between 0° and -6° altitude). As a next step, I'd like to compute the dawn beginning and dusk ending times. I believe the calculations must be very si...

Is there an equivalent to BCEL (Java .class manipulation) but in C or C++

Is there a C or C++ compatible library for reading and writing Java class files? I.e. something like Apache BCEL. ...

Replace for loop with formula

I have this loop that runs in O(end - start) and I would like to replace it with something O(1). If "width" wouldn't be decreasing, it would be pretty simple. for (int i = start; i <= end; i++, width--) if (i % 3 > 0) // 1 or 2, but not 0 z += width; start, end and width have positive values ...

Starting work on a Pre-existing Project

So this is more of a generic question. I seem to keep finding myself being put on larger and larger projects. Recently I have been assigned to a very large project written in C and VHDL. The goal is for me to become familiar with the code and eventually take the lead on the project. This is by far the largest project I have been assi...

Method of programmatically getting CPU time for thread, in C, that works on both OpenSolaris and Linux

I have a small daemon that I'm writing in C and I need a way to get the current CPU time on a thread. Linux apparently supplies a number of ways to go about doing this, clock_gettime(CLOCK_THREAD_CPUTIME_ID, ...), pthread_getcpuclockid(), getrusage(RUSAGE_THREAD, ...) but none of these seem to be supported under OpenSolaris 2009.06. Is ...

How to access a structure member in a function that get it as void* type?

I want to have a function that accepts different type of structures as argument. So, since I don't have a specific type, I have to use void*. Now question is: when I pass a structure to this function, how can I access a known member of this structure inside the function? Specifically, I know that all structures have str1 as a member and...

how to create a string literal as a function argument via string concatenation in c

I need to pass a string literal to a function myfunction("arg1" DEF_CHAR "arg1"); now part of that constructed string literal needs to be a function return stmp = createString(); myfunction("arg1" stmp "arg2"); //oh that doesn't work either is there any way to do this in one line? myfunction("arg1" createString() "arg2"); //what i...

Struct member [ array vs pointer ]

What is the benefit of declaring a C structure member as in array of size 1 instead of a pointer : struct { a_struct_t a_member[1]; ... }b_struct; Thanks in advance ...

static, define, and const in C

Hi, I've read that static variables are used inside function when one doesn't want the variable value to change/initialize each time the function is called. But what about defining a variable static in the main program before "main" e.g. #include <stdio.h> static double m = 30000; int main(void) { value = m * 2 + 3; } Here the varia...

What is the problem with this code? How to solve it? (fork)

What is the problem with this code? How to solve it? Parent processes goto in if or child process? first code produce zombie process or second code or both or non ? #include <signal.h> #include <sys/wait.h> main() { for (;;) { if (!fork()) { exit(0); } sleep(1); } } what about this code : #include <signal.h> #...

Is "for(;;)" faster than "while (TRUE)"? If not, why do people use it?

for (;;) { //Something to be done repeatedly } I have seen this sort of thing used a lot, but I think it is rather strange... Wouldn't it be much clearer to say while (TRUE), or something along those lines? I'm guessing that (as is the reason for many-a-programmer to resort to cryptic code) this is a tiny margin faster? Why, and ...

Learning to read GCC assembler output

I'm considering picking up some very rudimentary understanding of assembly. My current goal is simple: VERY BASIC understanding of GCC assembler output when compiling C/C++ with the -S switch for x86/x86-64. Just enough to do simple things such as looking at a single function and verifying whether GCC optimizes away things I expect to d...

Can I use a binary literal in C or C++?

I need to work with a binary number. I tried writing: const x = 00010000 ; But it didn't work. I know that I can use an hexadecimal number that has the same value as 00010000 but I want to know if there is a type in C++ for binary numbers & if there isn't, is there another solution for my problem? ...

Operator Overloading in C

In C++, I can change the operator on a specific class by doing something like this: MyClass::operator==/*Or some other operator such as =, >, etc.*/(Const MyClass rhs) { /* Do Stuff*/; } But with there being no classes (built in by default) in C. So, how could I do operator overloading for just general functions? For example, if...

dynamic memory allocation problem

I am working on a program that requires me to make use of 4 matrices sized [1000][1000]. I have created them using malloc(), but when I try running the program it just crashes and the memory usage shoots up to 2.5 GB. Please suggest any solution as soon as possible. I would be grateful.. ...

Problems with this stack implementation

where is the mistake? My code here: typedef struct _box { char *dados; struct _box * proximo; } Box; typedef struct _pilha { Box * topo; }Stack; void Push(Stack *p, char * algo) { Box *caixa; if (!p) { exit(1); } caixa = (Box *) calloc(1, sizeof(Box)); caix...

Variable Addition in C Language

Hi all, main() { char a,b,c; a=6; b=-5; c = a+b ; printf ("%d %d %d \n" , a, b,c ) ; } Here , what actually happens 00000110 10000101 -------- 10000111 -------- So values c will -11 , then how come it is 1. ...

Rerversing AND Bitwise.

Hey all, Here's the following algorithm: int encryption(int a, int b) { short int c, c2; uint8_t d; c = a ^ b; c2 = c; d = 0; while(c) { c &= c - 1; d++; } return d; } How can I find which variable a and b I should send in that function to decide of the output value of d? In other w...

Double split in C

OK. For example I have this line in my txt file: 1|1,12;7,19;6,4;8,19;2,2 As you can see, it has 2 parts, separated by |. I have no problems getting both parts, and separating second part 1,12;7,19;6,4;8,19;2,2 using ; separator. BUT I do have problems with separating further by , to get first and second number of each set. This is my ...