c

Perl: Blocking signal NOT delayed as it should be -> Test code provided.

In a Perl script I'm writing I'm having a problem where I block the INT and QUIT signals, execute a process within backticks, and then unblock the INT and QUIT signals. I want to prevent an INT or a QUIT from reaching the child process and killing it. The script successfully blocks and unblocks at the right points in the code, however...

C can someone tell me what is going on here?

I can not figure out what the heck is happening here. What I expect is that the output should say that there is only 1 element in keys, it's saying there are 7 when I have allocated only the 0 position with 120 bytes. void add2(char **b, char *i) { if (!i) { b[0] = (char*) malloc(120); sprintf(b[0], "%s", "hithere");...

Cannot read binary video files in GNU/Linux

Hi all. I'm stuck with an apparently harmless piece of code. I'm trying to read a whole flv video file into a uint8_t array, but by no reason only the 10 first bytes are read. contents = malloc(size + 1); if (read(fd, contents, size) < 0) { free(contents); log_message(WARNING, __func__, EMSG_READFILE); return (NULL); } I'...

What Is the difference between how the char datatype is stored or represented in 32bit vs 64bit in C?

What Is the difference between how the char datatype is stored or represented in 32bit vs 64bit in C? ...

How do I get the GCC __attribute__ ((constructor)) to work under OSX?

extern void MyInitFunc(void) __attribute__ ((constructor)); extern void MyTermFunc(void) __attribute__ ((destructor)); void MyInitFunc(void) { printf("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"); } void MyTermFunc(void) { } I put this in a .c file which is present in the main application (not a library or framework). It doesn't get ...

generating a random number within range 0 to n where n can be > RAND_MAX

How can I generate a random number within range 0 to n where n can be > RAND_MAX in c,c++? Thanks. ...

Client is having trouble connecting to server using sockets?

My server is up and running (connecting through telnet worked so I know its functional) but my client cannot seem to establish a connection. I have a feeling it has something to do with the way I'm filling out the sockaddr_in serverAddr struct. Can anyone please help? Thank you. int clientSocket; char hostname[256]; struct sockaddr_i...

Exporting shared library symbols in a crossplatform way?

Is there a cross platform way to selectively export certain functions and structs from a C project which builds a shared library? I want to do in a way that does not require a specific build system (the visibility should be defined in the code, eg as a macro), and in a way which both GCC and MSVC can understand. Thank you. ...

CS senior project ideas involving Unix system programming

I know there are a number of questions about senior project ideas but I am specifically looking for a project that involves Unix system programming in C or (preferably) C++. I have the book which I used for one quarter but haven't had a chance to use since. I want to find a project that will give me as much experience with Unix system ...

sin, cos, tan and rounding error

I'm doing some trigonometry calculations in C/C++ and am running into problems with rounding errors. For example, on my Linux system: #include <stdio.h> #include <math.h> int main(int argc, char *argv[]) { printf("%e\n", sin(M_PI)); return 0; } This program gives the following output: 1.224647e-16 when the correct answer ...

multi dimensional char array?

What I am trying to do is create an 2-d array of character strings The following seg faults instantly, what is wrong? void add2(char***b, char *i) { if (!i) { b[0][0] = (char*) malloc(120); sprintf(b[0][0], "%s", "hithere"); b[0][1] = (char*) malloc(120); sprintf(b[0][1], "%s", "bithere"); } else { ...

Anyone has a working viterbi decoder source code with descriptions?

Hi, I am having trouble trying to understand some viterbi decoder source codes. I understand the concept and everything but I just don't get some of the calculations that appears in viterbi decoding as well as how convolution code works in the source code. Many thanks for any answers. ...

Parse html using C

I've been a big fan of the site and frankly this is the first time I ever came across a problem that Stackoverflow didn't have the answer to. I need to grab some content from an html(xhtml valid) page. I grab the page using curl and store it in memory. I played with the idea of using regex with the PCRE library, but simply I couldn't fi...

checking a string for correct characters in c

Is there any easier way to do the following in c? unsigned short check_str(char *str) { while (*str) { if (!(*str == ' ' || *str == '(' || *str == ')' || *str == '1' || *str == '2' || *str == 'a' || *str == 'x' || *str == 'b')) return 0; str++; } return 1; } ...

Split large file without copy?

Question: Are there Windows API calls (perhaps NTFS only) which allows one to split a very large file into many others without actually copying any data (in other words, specify the logical breakpoints between joined files, with file names and sizes)? Examples: SetFileValidData, NtSetInformationFile Scenario: I need to programatically...

printf just before a delay doesn't work in C

Hi! Does anyone know why if i put a printf just before a delay it waits until the delay is finished before it prints de message? Code1 with sleep(): int main (void) { printf ("hi world"); system("sleep 3"); } Code2 with a self implemented delay: void delay(float sec) { time_t start; time_t current; time(&sta...

Idiomatic way to do list/dict in Cython?

My problem: I've found that processing large data sets with raw C++ using the STL map and vector can often be considerably faster (and with lower memory footprint) than using Cython. I figure that part of this speed penalty is due to using Python lists and dicts, and that there might be some tricks to use less encumbered data structur...

Using a variable to represent a function in C

This has the functionality I want (and it works) #include <stdio.h> //includes other libraries needed int foo(); int main() { while(true) { while(foo()==1) { //do something } //does other unrelated things } } int foo() { // Returns if a switch is on or off when the function was called...

What C preprocessor conditional should I use for OS X specific code?

What C preprocessor conditional should I use for OS X specific code? I need to include a specific library if I am compiling for OS X or a different header if I am compiling for Linux. I know there is __APPLE__ but I don't know if that is a current conditional for OS X 10.x. ...

C - If/Else and Pointers Returning Wrong String

We have a function longest, which returns the longest substring that consists of letters. Example: longest("112****hel 5454lllllo454") would return: lllllo However, when I run the program it seems to return lllllo454. Here is the function: char *longest(char *s){ char *pMax = NULL; int nMax = 0; char *p = NULL; int n...