c

processes vs threads (user vs kernel)

i understand the difference between a process and a thread. and i know the difference between a user thread and a kernel thread. my question is , how do you code any of them in c? all i know in c is how to create POSIX threads (but is this user threads or kernel threads)? can anyone put some c code samples for a process,user thread and a...

C error TLS error

My Error /usr/bin/ld: errno: TLS definition in /lib/libc.so.6 section .tbss mismatches non-TLS reference in ./../lib/lib.a(file_op.o) /lib/libc.so.6: could not read symbols: Bad value ...

How to create a generic C free function .

I have some C structures related to a 'list' data structure. They look like this. struct nmlist_element_s { void *data; struct nmlist_element_s *next; }; typedef struct nmlist_element_s nmlist_element; struct nmlist_s { void (*destructor)(void *data); int (*cmp)(const void *e1, const void *e2); unsigned int size; ...

Traceroute Theory

I am toying with trace route, my application send a ICMP echo request with a ttl of 0 every time i receive a time exceeded message i increment the ttl by one and resent the package, but what happens is I have 2 routers on my network i can trace the route through these router but third hop always ends up being one of the open dns servers ...

Interview question: Check if one string is a rotation of other string.

A friend of mine was asked the following question today at interview for the position of software developer: Given two string s1 and s2 how will you check if s1 is a rotated version of s2 ? Example: If s1 = "stackoverflow" then the following are some of its rotated versions: "tackoverflows" "ackoverflowst" "overflowstack" where as...

Help with bugs in a C code

This C code is giving me some unpredictable results. The program is meant to collect 6 nos and print out the max, position of the max no and the average. It's supposed to have only 3 functions - input, max_avr_pos and output for doing what the code is supposed to do but I am getting unpredictable results. Please what could be the problem...

memory alignment within gcc structs

I am porting an application to an ARM platform in C, the application also runs on an x86 processor, and must be backward compatible. I am now having some issues with variable alignment. I have read the gcc manual for __attribute__((aligned(4),packed)) I interpret what is being said as the start of the struct is aligned to the 4 byte bo...

Array pointer arithmetic question

Is there a way to figure out where in an array a pointer is? Lets say we have done this: int nNums[10] = {'11','51','23', ... }; // Some random sequence int* pInt = &nNums[4]; // Some index in the sequence. ... pInt++; // Assuming we have lost track of the index by this stage. ... Is there a way to deter...

What is "urgent data"?

The man page of epoll_ctl() says about EPOLLPRI: There is urgent data available for read(2) operations. How exactly is "urgent data" defined and who decides which data has priority? ...

Reading ASCII numbers using "D" instead of "E" for scientific notation using C

Hello, I have a list of numbers which looks like this: 1.234D+1 or 1.234D-02. I want to read the file using C. The function atof will merely ignore the D and translate only the mantissa. The function fscanf will not accept the format '%10.6e' because it expects an E instead of a D in the exponent. When I ran into this problem in Pytho...

EASY: How does one release memory correctly in the attached C array?

Hi, I'm just trying to work out why the following code is leaking memory and I have a funny feeling that i'm not releasing the array memory correctly. This is a C function in a wider objective-c app and I'm not native to C... i've tried just using free() on the array, but have a feeling this isn't the whole story... Could someone have a...

Displaying an inverted pyramid of asterisks

I help with my program. I am required to create a inverted pyramid of stars which rows depends on the number of stars the user keys, but I've done it so it does not give an inverted pyramid, it gives a regular pyramid. #include <stdio.h> #include <conio.h> void printchars(int no_star, char space); int getNo_of_rows(void); int main(void)...

Chained Hash Tables vs. Open-Addressed Hash Tables

Can somebody explain the main differences between (advantages / disadvantages) the two implementations? For a library, what implementation is recommended? ...

Why does setting this member in C fail?

I'm writing a Python wrapper for a C++ library, and I'm getting a really weird when trying to set a struct's field in C. If I have a struct like this: struct Thing { PyOBJECT_HEAD unsigned int val; }; And have two functions like this: static PyObject* Thing_GetBit(Thing* self, PyObject* args) { unsigned int mask; if...

Truncating double without rounding in C

Lets consider we have a double R = 99.999999; (which may be obtained by a result of some other computation),now the desired output is 99.99 I tried using printf("%.2lf",R); but it's rounding off the value.How to get the desired output ? (preferably using printf) ...

SQLite's test code to production code ratio

SQLite claim to have 679 times more test code than production one. http://www.sqlite.org/testing.html Does anyone knows how it is possible? Do they generate any test code automatically? What are the major parts of these "45678.3 KSLOC" of test code? ...

Reading data from text file in C

I have a text file which contains words separated by space. I want to take each word from the file and store it. So i have opened the file but am unsure how to assign the word to a char. FILE *fp; fp = fopen("file.txt", "r"); //then i want char one = the first word in the file char two = the second word in the file ...

STM32 I2C1 Start bit not set on SR1 register

Hi guys, I am trying to program the stm32 to talk to my i2c EEprom, but it seems like everytime I say: I2C_GenerateSTART(I2C1, ENABLE); while( !(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) ); the code hangs here I went through with the debugger and I found that the SR1 bit 0 flag (which is the start bit generated flag) is no...

Syntax highlighting in MS Word document

I want to insert some C/C++ source code into a Microsoft Word document. I have no problem in copying the code from source file and pasting into Word. I can do fixed-width. But, in Word, I see the code in black-and-white format; I loose the syntax highlighting as I get in the source code editor (for me, gvim). So, my question is, is there...

Segmentation fault

#include<stdio.h> #include<zlib.h> #include<unistd.h> #include<string.h> int main(int argc, char *argv[]) { char *path=NULL; size_t size; int index ; printf("\nArgument count is = %d", argc); printf ("\nThe 0th argument to the file is %s", argv[0]); path = getcwd(path, size); printf("\nThe current working directory...