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

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

Linking SDL in a C program

I have recently become interested in using SDL after having learned some basics of C. I have installed SDL_image and SDL_mixer. They are located in /usr/local/include/SDL2. I realize that you must link against the header files however I am not sure h...
more »

2017-03-19 01: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

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

No error at all, why?

I'm testing this code ,but why no error at all? #include <stdio.h> int main() { int a = 1025; int *p; p = &a; // now I declare a char variable , char *p0; p0 = (char*) p; // type casting printf("", sizeof(ch...
more »

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

Cannot change default action for SIGINT

In C, I want to catch the SIGINT signal and print out a message like "SIGINT received" by using sigaction and passing a new handler to it via sa.sa_sigaction = handler; I don't want to terminate the program. If I run my program through the shell ...
more »

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

"Dynamic Inheritance" in C

I wrote the following code and it works, but I would like to know if a can be sure that it works all the time on all x86 machines. #include <stdio.h> #include <stdlib.h> typedef struct Base { int a; float b; } Base; typedef struct ...
more »

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

code crashes when accessing a predefined array

I'm having trouble understanding why my code crashes when accessing the array. When converting the function param to int[3][3] instead of int** I get no problems, but I can't understand why, since the int** I'm given as a argument is a valid pointer ...
more »

2017-03-15 08:03 (2) Answers

What is the difference?

Is there any difference between these two declarations: char ch; char ch[2] Actually the thing because of which this question arose in my mind is the ending with '\0' i.e. null character so one of the 2 characters of the array ch[2] is null chara...
more »

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