OpenGL include directives on OS X

I am a bit confused why this code compiles. I leave out the "necessary" #include <OpenGL/gl.h> and still the program can compile. How is this possible when my program is calling functions from the GL library, without including them. int main(int argc, char** argv) { glClearColor(1.0,1.0,1.0,1.0); return 0; } I use this compilation command: gcc -framework GLUT -framework OpenGL test.c I was under the assumption that adding -framework just specifies to the linker where the

C Structs and arrays

I can't get the code below to compile (see errors). Advice on correction would be appreciated. #include <stdio.h> typedef struct { char *fldName; unsigned fldLen; } Field; typedef struct { char *fldPrompt; unsigned startRow; unsigned startCol; } Prompt; typedef struct { Field *fields[]; Prompt *prompts[]; unsigned numFlds; <<< invalid field declaration after empty field } Form; <<< in '(incomplete) struct (no name)'. Fiel

I'm trying to increment values in a C character array, and it keeps failing - help?

Here is my function's code: char * increment_b_string(char * b_string) { char * ret_string = (char *) malloc(7); ret_string = "001aaa"; if (*(b_string + 2) == '9') { *(ret_string +2) == '0'; if (*(b_string + 1) == '9') { *(ret_string +1) = '0'; *(ret_string) = *(b_string) + 1; } else { *(ret_string + 1) = *(b_string + 1) + 1; } } else { *(ret_string + 2) = *(b_string + 2) + 1; } r

My string padding function in C does not work?

I had tried to do it myself but failed (I am tempted to do it again for learning but just need it for an example program). Essentially I wish to represent a binary number but padded of course to the nearest byte with 0's so I found a function on another question here: char * string_pad(char * string, size_t padlen, char * pad) { size_t lenstring = strlen(string); size_t lenpad = strlen(pad); char * padded = (char*)malloc(lenstring + lenpad + 1); strncpy(padded, string, lenstrin

C free(temp) pointer frees head pointer in linked list-How to retain pointer to origin of list

I have a question, if I wish to delete a node from linked list and I do this : Assume: Head points to first node. deleteFirstNode(struct node * head) { struct node* temp=head;//this line temp->next=head->next->next; temp->data=head->next->data; free(head); head=temp; } 1) will this delete first node? 2) If so, when I free head , will it not free temp pointer too? because both temp and head point to same location in this line (see comment in "this line

How to call WM_SETREDRAW?

When I send WM_SETREDRAW to disable redraw for a window, how do I "restore" the previous state when I'm done? What's the proper way to send this message?

How to send message to a server through socket in 'C' when server is started after the client?

I have made a client (in C) which is trying to connect to a server (in VB 6.0) till the connection is established. If the server is ready t listen and then we run the client program trying to connect and send a message, it works fine and also the response is received on the client side. But when we start the client program before server, it tries to connect after a short interval to the server until it gets connected (in an infinite loop). When we switch on the server, the client connects succes

executing programs from command prompt

I am having trouble with executing my executable file by executing my program from the location of the program. I am using eclipse, and when I paste the location of my executable file from debug in eclipse, onto the command prompt my program works perfectly. However when I try to execute my program from the location of the debug, my program is altered, any help please?

Circular-/Ring-Buffer with blocking read and non-blocking write?

I am searching for a ringbuffer-implementation in C in userspace, so I can use it in my library. Because I need a ringbuffer with non-blocked write (=overwrite oldest data) blocked read if empty I searched a while and remembered I have used wait_event_interruptible & wake_up_interruptible to do something like this in kernel-mode. But what is used in user-space so I maybe can search for a ringbuffer in combination with that method? I don't want to re-invent the wheel - there are many ri

C error in reading file in

i am trying to data (given below ) of a text file but i keep getting the error too few arguments 1/1/1990/0/0 774.359070 1/1/1990/0/10 772.973206 1/1/1990/0/20 739.628784 1/1/1990/0/30 680.130066 1/1/1990/0/40 618.887695 my code is #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE* input; FILE* output; input = fopen(argv[1],"r"); output = fopen(argv[2],"w"); float date; float value; while(fscanf(input,"%f/%f/%f/%f/%f/%

gstreamer 1.0 seeking within an mpegts not working as expected

I have a bunch of mpegts video files obtained from a Silicondust HDHomerun tuner. Seeking was really slow in gstreamer 0.10 (see my previous post). Now seeking is greatly improved (in 1.0.6), but there are still problems. Here is some stdout: $ ./fullscreen02 No accelerated IMDCT transform found No accelerated IMDCT transform found pos_ns: 1700833333, seek_ns: 11700833333 position: 0:00:11.700833333 (fullscreen02:30989): GStreamer-CRITICAL **: gst_segment_do_seek: assertion `segment->for

Are there any practical differences between constructing a string via strdup() and malloc()?

Suppose I want to write a function that will produce a string, and while I can set an upper limit on the size of the string I don’t know in advance exactly how much space the string will take up. I can think of two ways to arrange this: char *ParametersAsString_1(int temperature, float pressure) { char buffer1[128]; snprintf(buffer1, 128, "temperature: %d; pressure: %g", temperature, pressure); return strdup(buffer1); } char *ParametersAsString_2(int temperature, float pres

parent send command line arguments to child

I am writing a program that creates a pipe, forks, then the parent sends the command line arguments to the child one char at a time. The child is supposed to count them, and then the parent reaps the child and prints out how many arguments there were. Here is what I have so far: #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <string.h> int main(int argc, char **argv) { pid_t pid; int

C Invalid write and signal 11 error valgrind

I'm trying to implement a program that operates on a list of generic value nodes (pointers to void). The program works as follows: Read each line of an input file that translates each line, interprets and performs a function. Performs functions such as addition, subtraction, normalization, distance, increment, decrement ... Running with the values ​​of the nodes that are almost floats vectors. To compile use: g+ +-g-Wall-Wextra-Werror-pedantic main.cpp-o metbasicos.cpp metintermedios.cpp metav

LD_PRELOAD free/malloc/realloc/calloc free doesn't work

I'm recoding malloc/calloc/realloc/free functions. For now, I finished malloc and for tests, I just write "free" function like : void free(void* ptr) {} And I test my functions with different software, works with ls/firefox/man ... But, with software like GIMP I have : *** Error in `gimp': free(): invalid next size (normal): 0x0000000001d75e00 *** ======= Backtrace: ========= /lib/x86_64-linux-gnu/[0x7f280d77e996] /lib/x86_64-linux-gnu/[0x7f280d78061

C, Multi-Dimensional Arrays Exercise

My understanding of a 2d array is wrong. So I'll approach this question another way. Say I have the following variables. int student_id[10], course_id[5]; int student_course[10][2]; There can only be 10 students. There can only be 5 courses. A student can only take 2 courses. /*prompt user for student id*/ /* say that the value for: student_id[0]=123 */ /*prompt user for course id*/ /* say that the value for: course_id[0]=101 course_id[1]=102 course_id[2]=103 course_id[3]=104 course_id[

Segmentation fault, parsing, C

I am writing a c file that takes in information from a .csv file, parse it, then delete whatever ive parsed. The problem that i am having is that, after it runs through one line of the .csv file i get a segmentation fault. I heard i can use GDB to help with this, but I don't know how to use that. This is my .c file void parser(int argc, char ** argv) { FILE * songList; char * theString; char * theToken; char songs[ROOM_STRING_LENGTH]; char artist[ROOM_STRING_LENGTH]; char title[ROOM_STRING_LENG

What is wrong with my itoa conversion?

Background Im trying to determine character codes for some national diactric letters. Problem Is something is wrong with my code? char a = "a"; // "a" ascii code is 97 char buffer[8]; itoa(buffer,(int)a, 10); print(buffer); // but it prints "252" instead of "97"

finding memory access violation for a code

I am noob on valgrind. GCC compiler compiler gave me go flag but an online compiler gave me hard time where I need to submit this code. Please help me finding memory access violation for below code #include<stdio.h> #include<string.h> #include<stdlib.h> #include<string.h> #include<ctype.h> #include"railfence.h" #define RAIL 2 char* getCipher(char plainText[]) { int strln; int i; int j; int k = 0; char *result; char **arr; strln = strle

How to interrupt pselect?

Here there's my code, I'm not able to interrupt the main loop using signals. When I launch the SIGUSR1 signal handler is called but I don't unblock pselect. #include <errno.h> #include <signal.h> #include <stdio.h> int end = 1; void handler(int sig) { end = 1; } int main() { sigset_t blockset; struct sigaction sa; int res; /* Block the signal */ sigemptyset(&blockset); sigaddset(&blockset, SIGUSR1); sigprocmask(SIG_BLOCK, &blocks

Is signal function safe enough to just ignore or restore signal handler?

I have read many articles which suggest using sigaction to replace of signal for many reasons. But after reading some open source code (Such as Redis, Lua), I find the signal still exists. If I only use signal to ignore or restore default handler, such as: signal(SIGPIPE, SIG_IGN); or: signal(SIGPIPE, SIG_DFL); Is signal safe enough?

Seg fault while checking argv for an ampersand

I have a function that checks whether argv contains ampersand at the end (shell purposes) and it gives a seg fault for seemingly no reason.. Thanks in advance length(char **argv) calculates length of argv. int ampersandCheck(char **argv){ int n = length(argv); int i = 0; while(argv[n][i] != '\0'){ if(argv[n][i] == '&' && argv[n][i+1] == '\0') return 1; i++; } return 0; }

C How does this reverseBytes method work?

I was looking at this function online and am wondering how it works: /* * reverseBytes - reverse bytes * Example: reverseBytes(0x12345678) = 0x78563412 * Legal ops: ! ~ & ^ | + << >> */ int reverseBytes(int x) { int newbyte0 = (x >> 24) & 0xff; int newbyte1 = (x >> 8) & 0xff00; int newbyte2 = (x << 8) & 0xff0000; int newbyte3 = x << 24; return newbyte0 | newbyte1 | newbyte

Printing Unix directory content as normal text file

I'm very curious how unix system works, now I'm interested in directories which are collection of files, especially collection of two items like a name of file and index(number) to table of i-nodes for that file and I would like to know if there is any hack to print directory as normal text file to see directory content... any ideas?

C MPI reverse probe

Is there a way to check if some processes are waiting on MPI_Recv? I have a root proc, and some slave processes. Slave psedo-code: while (1) { do_some_stuff; // calls MPI_Test and clear unused buffers MPI_Recv(buf, ...); do_something_with_buf; MPI_Isend(buf2, ...); // possibly many sends depending on what was in buf } If all slave processes hang on MPI_Recv, then job is done and I need to brake the loop. Now I need some way to notify slave processes that job is done. Is ther

Using pointer to pointer to make a dynamic matrix of integers

I'm trying to make a dynamic matrix that is based on an integer pointer to pointer, this variable is allocated dynamically, but I'm having a little trouble using it, I don't know if I am using it wrong or something, but the outcome is not as expected. Is it something wrong that I'm doing or am I not thinking this through? void pkn(int n, int k) { int chec = checagemInicial(n,k); if(chec == 1) { return; } int mat[n]; int **resultado = NULL, **newMem; int posicoes = 0

How to assign value for element in array at struct pointer in C

I have struct like this: struct temper_t { unsigned char rom[8]; struct temper_t *next; }; In this main code, I want assign value for rom[8], how do i can do that: new_node = (struct temper_t *) malloc( sizeof(struct temper_t)); new_node->next = NULL; int m; unsigned char rom_value[8]; //Luu thong tin vao node moi for (m = 0; m < 8; m++) {

Array of struct objects - Thread safety

I have a global array of struct objects that is accessed by multiple threads concurrently. However there is a guarantee that the individual data members of the struct object(at a particular array index) accessed by two different threads at a time will always be different. My question is whether I need to have a mutex lock on this global array for its access or not. My struct looks like this: struct data{ string a; int b; uint16_t c; }; EDIT: I have a multithreaded UDP server prog

C Why does %u %d not work on UNIX/LINUX for printing addresses

I know that %d is for int, and %u for unsigned int and %p for the address. But my question is we can print the address using %u,%d on a Windows machine but the same cannot be done on Linux. For example: #include<stdio.h> int main() { int x=15; int *p; p=&x; printf("%d \n",x); printf("%d \n",&x); printf("%u \n",&x); printf("%p \n",p); printf("%x \n",p); printf("%u \n",&p); return 0; } OUTPUT: 15 2358812 2358812 000000000023FE1C

Cause gets() to exit on SIGINT in C

I am writing a simple C program which reads one line of text at a time from the keyboard (in a loop) using the gets() function. The loop must exit immediately if the user presses CTRL-C. I know how to write a sig handler which can set a flag to exit the loop...but how do I make the gets() function return immediately when this signal arrives?

Makefile on GCC for C

I'm using MinGW and trying to create a justify executable using the Makefile but it gives me the following error: Directory of C:\Users\PATTY\Desktop\loops 08/19/2016 01:34 PM <DIR> . 08/19/2016 01:34 PM <DIR> .. 08/18/2016 10:41 PM 59,628 a.exe 08/18/2016 11:58 PM 261 demo.c 08/18/2016 11:59 PM 59,541 demo.exe 08/18/2016 07:01 PM 605 justify.c 08/18/2016 07:01 PM 1,122 line.c 08/18/2016

Convert String to Type in C language

Prototype: size_t offsetof(type, member); I know first parameter is type, what if I have just the name in string not the type. I want to get the offsetof a member with just string literals I want help from the community, how to achieve this. ex: #include <stdio.h> #include <stddef.h> typedef struct example_ { void *member1; void *member2; } example; unsigned int offset_gen(char *ds, char *member) { return (offsetof(ds, member)); } void main() { printf ("\n %d

Can you have a factorial macro in C?

I'm am searching for an example of factorial macro. I am not finding anything that works. Is there a specific reason there isn't one? Is this because of C's implementation? So, to attempt to make the question clearer....... I have not been able to find a factorial macro in the programming language of C. I understand that you cannot have recursion in a macro, but there is a way to have an iterative factorial function. Why isn't it possible in C, to have a factorial macro? For reference here is

C - dup2 stdin make dup2 stdout and stderr stop printing

I'm having a strange problem. I'm currently creating two programs, one will execute on background and the other one will read its output and also allow to make inputs to the other one running on background. The problem is, when I put the line dup2(in, STDIN_FILENO) the program executing on background stops printing to the files. The Reader: #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <sys/types.h

Extracting and Displaying data from text file in C

I am trying to write a C program that takes in two arguments, either [-url | -phone | -email] and a text file that the user will download from a website. After the user inputs the flag and the name of the text file, the program is supposed to extract and display the contents based on the regular expression I have developed. For example, for URL the regex is /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/ I am having a hard time figuring out how I can implement such a task.

AVR C : 8 Bit Counter using button

I have circuit setup, that turns the led on every time a button is pressed and it counts up from 0 to 255, so in binary 0000 0000 to 1111 1111. My switch is configured to PB2 which is D9 on the board. I facing a problem, as you can see from my coud I am using 8 pins, 6 from D register and 2 from B register. When I increment PORTB and at some point it become 0b00000100, then its the same value as then input pin which is pb2 so in my infinite loop even though I am not pressing the button for PINB

Do anyone konow why I get SIGSEGV, segmentation fault by execution 0x90

I get a segmentation fault error by executing 0x90 why cold this happen ?? this is my C code : #include<stdio.h> #include<string.h> #include<unistd.h> #include<stdlib.h> #include<stdint.h> char * buffer; int64_t * startPtr; int64_t * endPtr; int64_t * exploitAddress; int test() { printf("test\n"); return 0; } void main(int argc, char ** argv) { char buffer[512]; startPtr = (int64_t *) &test; printf("funcPtr : \n %p\n", startPtr); p

Lighttpd - Error while trying to run C application through CGI

Greetings fellow programmers, I'm a bit new here, so please be gentle. Now the problem, I'm currently working on a dynamic web application running on WAGO PLC (750-8202) with embedded linux. Lighttpd is installed on the PLC itself. The goal is to monitor/edit I/O of the PLC from the web app. After a lot of reasearch I found out I need to use CGI to communicate with my C applications that can return the information from the PLC that I need. I tried some simple hello world codes, but always wit

Setting pointer to NULL after free in quirky data structures (C)

I have the following following somewhat convoluted data structures: a binary tree made up of nodes, and a linked list of items, each item storing tree nodes. If a Node is created, then it will certainly be used in a Tree, in a List, or both. Here are the data structures: typedef struct Node Node; typedef struct Item Item; struct Node { int key; Node *left; Node *right; }; typedef struct { Node *root; } Tree; struct Item { Node *value; Item *next; }; typedef struct {

Can't find the proper output in this code

I was asked to write a program about a traveling man. The direction of the program: Suppose a man (say, A) stands at (0, 0) and waits for the user to give him the direction and distance to go. The user may enter N E W S for north, east, west, south, and any value for the distance. When the user enters 0 as direction, stop and print out the location where the man stopped My Code: #include <stdio.h> int main() { float x=0,y=0; char dir; float mile; while(1){ printf("Enter inp

C How to print lines from stdin?

I'm redirecting stdin to be output produced from the command ls -l when run on the command line, and I want to print out its results. So say after I run ls -l I get the following output: total 519 -rw------- 1 jeff dev 1274 22 Jun 14:52 prinprog -rwx---xr-- 1 jeff dev 2410 6 Apr 12:10 temp.txt -rwxr----x 1 jeff dev 8128 1 Feb 2013 yyz -rw-r--r-- 1 jeff dev 98 15 Feb 2013 yyz.c Now after compiling my C program I do ls -l | ./testing and I have effectively changed the

Correct way to write a menu in standard C

I would like to write a menu like: 1. Foo 2. Bar 0. Quit > This menu should work in both piped or terminal mode: $ ./menu 1. Foo 2. Bar 0. Quit >1 oops no ! I wanted to do 2 Error: try again > And the piped mode: $ printf 1 42 | ./menu You chose Foo and entered 42 So I am looking for a way to empty the buffer when someone enter things like 1 something. Otherwise I will go to the option foo and submit something to scanf. Is there a way to empty the current buffer? To play

How to add the user space headers in device driver programming?

I am trying to perform some GPIO operations through a lodable kernel module. for which, I am trying to access the file "/sys/class/leds/led1/brightness" using open() and write() system-calls, therefore I have included the following header files. #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> #include <stdio.h> #include <string.h> but while cross compiling the project, I got the following warning

deleting only non-existing node which is right next to last node in linked list terminates the program

When I delete any node present in a linked list my code works perfectly. let's say my linked list has 10 nodes, if I want to delete 12th, 13th, 14th... nodes my program gives me the expected message. But if I want to delete 11th node(which is adjacent to the last node), my program terminates with exit code -1073741819 (0xC0000005) int delete() { int position, count = 1; printf( "\nwrite your position" ); scanf( "%d", &position ); struct node *p, *q; p = head; if (

Viewing the exact memory range that a linker 'uses' for a executable binary

I am doing a bit of OSdev, and I've been trying to implement memory management in my kernel. I have started off with a physical memory manager (this is a 32 bit OS). The idea is to keep a table of bits where we allocate a bit per 4K physical memory block. If the bit is '1', the block is in use and if '0', it isn't. I thought that this table should come after the kernel. So here is my kernel code (minimal): #include<stdint.h> #define PMMAP 0x1000 //This contains information from int 15h/

C I am not being able to generate the subsets of an array basing on giving Sum

I am using a recursive function to find the subsets from a character array basing on a given sum: str="1234567890" n=2 (size of subset), m=9 (the given sum). Solution: {9,0},{0,9},{8,1},{1,8},{7,2},{2,7},,,,,{5,4},{4,5}. My code is following. #include<stdio.h> #include<string.h> #include<ctype.h> #include<stdlib.h> void genrate(char*,int,int,int); int main(){ char str[20]="1234567890"; genrate(str,2,9,10); } void genrate(char *str,int n,int m,int k){ int i

Difftime giving a larger than expected value

When I print the result of difftime, I get the result: -4334284568731237.000000. Why is this? //time1 and time2 are both of type time_t time_t temp; char val[] = "06:02"; struct tm tm; strptime(val, "%H:%M", &tm); temp = mktime(&tm); items[0].time1 = temp; char val2[] = "06:43"; struct tm tm; strptime(val2, "%H:%M", &tm); temp = mktime(&tm); items[0].time2 = temp; printf("%f\n", difftime(items[0].time1, items[0].time2));

C different behavior of sscanf i386 vs x86_64 parsing quoted string

I have found strange behavior of sscanf parsing double quoted string on i386 machine. My code: char string[] = "\"1597821828\" \"MODIFY\" \"/fullpath/test2\""; printf("line : %s", string); int total; char store_date[12] = "", store_etat_watch[24] = "", store_path[1024] = ""; printf("stores init:%s,%d , %s,%d , %s,%d \n", store_date, strlen(store_date), store_etat_watch, strlen(store_etat_watch

how to create MPI custom type properly

I have structure(single element of complex matrix): typedef struct s_complex_number { int real; int img; } ComplexNumber; This is how i described Complex matrix as custom MPI datatype #define SIZE_COL 10 MPI_Datatype matrix; MPI_Datatype types[2] = {MPI_INT, MPI_INT}; MPI_Datatype row; MPI_Datatype complexNumber; MPI_Aint disp[2]; ComplexNumber ***recvData; ComplexNumber ***sendData; ComplexNumber example; int blockLength[] = {1, 1}; disp[0] = (uintptr_t)&e

  1    2   3   4   5   6  ... 下一页 最后一页 共 1227 页