c

Differences between struct in C and C++

I am trying to convert a C++ struct to C but keep getting "undeclared identifier"? Does C++ have a different syntax for referring to structs? struct KEY_STATE { bool kSHIFT; //if the shift key is pressed bool kCAPSLOCK; //if the caps lock key is pressed down bool kCTRL; //if the control key is pressed down bool kALT;...

Linux receive signal break for ttycontrol program

I would like to receive SIGINT if my process controlling /dev/ttyS2 receives BREAK on a serial port. I run this program from a shell. From what I discovered only "the terminal is the controlling terminal of a foreground process group, it will cause a SIGINT to be sent to this foreground process group" I tried make process making controll...

How do you get what kind of encoding your system uses in c/c++?

In linux terminal one would type locale charmap in order to see what kind of character-encoding your system uses, eg UTF-8. My question is how would you do this using c/c++. (I'm using linux) edit: I tried using nl_langinfo(CODESET) but I got ANSI_X3.4-1968 instead of UTF-8 (which is what I get when typing: locale charmap). Am I...

error: invalid conversion from 'void (*)(...)' to 'void (*)()'

Hi, having this problem on Mac with gcc 4.0.1 build 5370, XCode 2.5. The code snippet is: there is a declared function, the second parameter cause the problem: void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue) calling it like this: typedef void (*FuncPtr)(); FuncPtr func = some_function_pointer; ffi_call(nul...

When do you use third-party code?

How do you generally resolve a programming problem, for example, when you have to parse a ini file? If it is my task, I will : First check if there is already a weapon suitable for it in my arsenal. I mean check the libraries I am familiar with, like Glib, APR, or just standard C API. If I don't find anything suitable, I will check i...

Reading command line arguments after '<' in C

Let me start by saying this is associated with a homework assignment. However, this is a very small and relatively insignificant part of the assignment. The C program receives input via command line arguments but it needs to be in the form: $ ./program < input How, would I go about receiving that input as a string? Each time I try t...

Probing for filesystem block size

I'm going to first admit that this is for a class project, since it will be pretty obvious. We are supposed to do reads to probe for the block size of the filesystem. My problem is that the time taken to do this appears to be linearly increasing, with no steps like I would expect. I am timing the read like this: double startTime = ge...

How to prevent a function from being optimized

I am optimizing the entire code, yet I dont want a certain function from being optimized, say for debugging purposes. Is there a way to do it on gcc 3.4+ compiler? ...

Faster means of checking for an empty buffer in C?

By 'empty buffer,' I mean a buffer full of zeroes. I am searching for a faster method of accomplishing this: int is_empty(char * buf, int size) { int i; for(i = 0; i < size; i++) { if(buf[i] != 0) return 0; } return 1; } I realize I am searching micro optimization unnecessary except in extreme cases, but I know a faster method ...

asn.1 parser in C/Python

I am looking for a solution to parse asn.1 spec files and generate a decoder from those. Ideally I would like to work with Python modules, but if nothing is available I would use C/C++ libraries and interface them with Python with the plethora of solutions out there. In the past I have been using pyasn1 and building everything by hand ...

Simple C/C++ network I/O library

I have the following problem to solve. I want to make a number of requests to a number of "remote" servers (actually, a server farm we control). The connection is very simple. Send a line, and then read lines back. Because of the number of requests and the number of servers, I use pthreads, one for each request. The naive approach, ...

c string multiple replacements within a character string

Lets say I have a string: "(aaa and bbb or (aaa or aaa or bbb))" **for simplicity sake, this will always be the format of the string, always 3 a's followed by a space or ')' or 3b's followed by a space or ')'. what would be the best way to replace every occurence of 'aaa' with a '1' and everyoccurrence of 'bbb' with a '0' in C. endin...

strtok and memory leaks

hello, I wrote a simple url parser using strtok(). here's the code #include <stdio.h> #include <stdlib.h> typedef struct { char *protocol; char *host; int port; char *path; } aUrl; void parse_url(char *url, aUrl *ret) { printf("Parsing %s\n", url); char *tmp = (char *)_strdup(url); //char *protocol, *host...

Determining size of bit vectors for memory management given hard limit on memory.

After searching around a bit and consulting the Dinosaur Book, I've come to SO seeking wisdom. Note that this is somewhat homework-related, but actually isn't a homework problem. Also, this is using the C programming language. I'm working with a kernel that currently allocates memory in 4K chunks. In an attempt to cut down on wasted mem...

How to have a C program respond to a letter and spit out something else?

Hey guys, I can't seem to get this program to compile. I keep getting the error: 'Ammonia' undeclared 'Carbon_Monoxide' undeclared and so on. Am I using the right function with switch? /*This program will report the content of a compressed-gas cylinder based on the first letter of the cylinder's color.*/ #include <stdio.h> int mai...

stepping into MACRO in VC++

I am debugging a source code that has a lot of big #define'd MACRO routines. I am interesting in stepping into them, but I guess, VC++ does not allow step-in functionality ... so, I am converting them into functions, but this is becoming hard for me Is there a way to step into MACRO routines? especially in VC++? PS: I can port the w...

Returning C string from a function

Hi, I am trying to return a C String from a function but its not working. Here is my code. char myFunction() { return "My String"; } and then in main i am calling it like this. int main() { printf("%s",myFunction()); } I have also tried some other ways on myFunction but they are not working. E.g; char myFunction() { ...

Does unaligned memory access always cause bus errors?

According to this wiki page, bus error can be caused by unaligned memory access. The wiki page gives an example about how to trigger a bus error. In the example, we have to enable alignment checking to see the bus error. What if we disable such alignment checking? The program seems to work properly. I have a program access unaligned memo...

Java vs C output

This might seem simple but it's just stumbled me and my friends... lets take the following piece of code- in java //........ int a=10; a= a-- + a--; System.out.print("a="+a); //........ in c //........ int a=10; a= a-- + a--; printf("a= %d",a); //....... where in the former case you get output as 19 in C you get it as 18. the lo...

Why log(1000)/log(10) isn't the same as log10(1000)?

Today, I came across quite strange problem. I needed to calculate string length of a number, so I came up with this solution // say the number is 1000 (int)(log(1000)/log(10)) + 1 This is based on mathematical formula log10x = lognx/logn10 (explained here) But I found out, that in C, (int)(log(1000)/log(10)) + 1 is NOT equal to ...