pthread mutex not unlocking on wait

I'm using c posix threads, and i've encountered a problem I don't quite understand and would like some fresh eyes to see what the problem could be. In summary, I'm creating 3 threads which check some program state, signal that they are ready, and th...
more »

2017-03-19 18:03 (1) Answers

fcntl F_GETLK always returns F_UNLCK

I am trying to understand POSIX file-region locks in C. The program below is really simple, sets the lock to F_WRLCK and then gets locks. There is no errors during opening/setting lock. Unfortunatelly it's always returning F_UNLCK. Where is the mista...
more »

2017-03-19 10:03 (1) Answers

Very simple cryptography function in C

I'm trying to create a very simple cryptography function that takes a character array and returns another character array with its value increased by one. Precise explanation: The input is a line of text inserted by the user. I expect that the array...
more »

2017-03-19 08:03 (4) Answers

Why Can I Trust Memory Allocation?

I'm currently taking an introductory CS course that uses C. My textbook seems to imply that a pointer variable still holds the address for memory previously allocated to it, even after free() was called on it -- assume I previously used malloc(). Doe...
more »

2017-03-19 08:03 (5) Answers

Import new functions from file in C at runtime

Is there any way to import a file of functions at runtime in C? Meaning that from user input (or maybe as compiled programs are added to a directory), the main function could call functions in these using trampolines? For example, a directory would ...
more »

2017-03-19 08:03 (0) Answers

Sorting a list of dates by chronological order?

My program takes a list of dates in the format of (month day year). It then sorts it in chronological order of most recent events.It should sort the years from 90 to 99, and then from 00 to 12. For example it should sort this list. January 1 01 ...
more »

2017-03-19 02:03 (2) Answers

Qsort and Comparators weird behaviour. C

So, I am using qsort in my C program from C library. It works as expected so I decided to play around with comparators. Comparator 1 (I use this): int compare (const void * a, const void * b) { if (*(double*)a > *(double*)b) return 1; else ...
more »

2017-03-18 23:03 (1) Answers

How to read multiple digit number from a string

I am trying to pass a string S as input. Here the string S can contain multiple integer values followed by an alphabet. The program must expand the alphabets based on the previous integer value. Consider the Input: 4a5h For which the Output: aaaahhh...
more »

2017-03-18 18:03 (3) Answers

c - fopen opening directories?

I am using fopen to determine whether I'm opening a file or a directory like this: FILE *f; f = fopen(path, "r"); if (f != NULL){ // found file } else { // found a directory } And path is currently a path pointing to a directory, not a file...
more »

2017-03-18 16:03 (2) Answers

pthread_cleanup_push and O2 CFLAGS

I have some warning when compiling a piece of code using pthread_cleanup_push/pop with -O2 CFLAGS. Just by removing the O2 cflags in the Makefile make it compile without issue. Is it forbidden to use gcc optimization with these pthread macros ? I wa...
more »

2017-03-18 15:03 (1) Answers

Why do we need prototype in function?

I'm studying C and my book is explaining how to "prototype a function" so we can use it before we define it. The point is that I can't imagine a situation in which is needed to use a function before have defined it, why can't we just define it at th...
more »

2017-03-17 23:03 (2) Answers

Make the program stop after user enters a value

I am building a quiz project on C. My question is, can I use a command or use something to make the user decide if he wants to keep answering questions or not. For example here is a part of my code. And i would like to make user able to press "0" and...
more »

2017-03-17 23:03 (2) Answers

Using qsort and structs to sort a list

I'm a beginner at C and using structs.I'm trying to create a program which sorts a list of dates. The user first inputs the number of dates, then the dates themselves, being the month, day and the year. Then using qsort I want to sort it chronologica...
more »

2017-03-17 23:03 (1) Answers

isdigit() Input testing for valid phone number

I'd like to create an algorithm that checks to see if the user has inputted a valid phone number (digits only). The program should keep prompting the user to enter a phone number until it is strictly digits. I've come up with the following. Program ...
more »

2017-03-17 18:03 (3) Answers

Constant time random number within range

I have a random number generator that runs in constant time. The prototype for this function is as follows: uint8_t rand(); What I want to be able to do is create a function that randomly returns an uint8_t such that the output is between 0 and m...
more »

2017-03-17 18:03 (3) Answers

why use usleep and not sleep

I was reading a code of application and something caught my attention. The code was : usleep(6*1000*1000). I understand that they use this format for readability issues. I think that both sleep and usleep use the nanosleep function, so my question ...
more »

2017-03-17 16:03 (2) Answers

Fortran to pass C struct between two C functions

I have a Fortran application that is required to call two C routines. One to load a file and one to run a calculation on the file about 200 times. I understand that a C DLL cannot 'save' the loaded struct in a static variable to be reused in the calc...
more »

2017-03-17 15:03 (1) Answers

C shared library problems

I am trying to create a shared library called -lrfc7539 with the structure below: rfc7539.o: rfc7539.c chacha20poly1305.o $(CC) $(CFLAGS) -c -o $@ $< chacha20poly1305.o: chacha20poly1305.c chacha20.o poly1305.o $(CC) $(CFLAGS) -c -o $@ $...
more »

2017-03-17 14:03 (2) Answers

C preprocessor replacement not working

#include <stdio.h> #define VAR cc int main(void) { int ccc = 9; printf("hell loo %d", VARc); return 0; } My understanding of this code means that anywhere the preprocessor finds VAR, it will replace it with cc, hence the printf w...
more »

2017-03-17 11:03 (2) Answers