c

Why C or C++ does not allow passing array by values to function

C and C++ allows passing of structure and objects by value to function, although prevents passing arrays by values, why? ...

Pthread conditional signal - not working as expected

I am working on a project and trying to use pthread_cond_wait() and pthread_cond_signal() to synchronize two threads. My code looks something like this: pthread_mutex_t lock_it = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t write_it = PTHREAD_COND_INITIALIZER; int main(int argc, char**argv) { pthread_t t_send_segments, t_re...

A Simple, 2d cross-platform graphics library for c or c++?

As in title, i need a 2d graphics library that is cross-platform, and provides simple functions, like in Basic; essentially, i only need to paint a pixel a certain color-I do not need hardware acceleration, or any kind of 3d support. I've found a couple ones, but they're not cross-platform. Anyone knows a solution for me? ...

how to find a loop in the file system?

how to find a loop in the file system in Linux? i am indexing all files for making search fast(O(1))... i am using c programming language to implement by using the library functions in dir.h.... I can scan through entire file system but it goes in a loop if there is loop in the filesystem(example loop mount)... how to find the loop in fi...

C dynamic string length

There are different ways of creating dynamic strings in C (with length that constantly changes). After some google search, the main way of doing this is to use realloc(). A way I implemented this is using linked lists with 32 bytes chunks for each node. I was wondering if there are better ways of tackling this apart from using realloc...

Strange behavior using getchar() and -O3

I have these two functions void set_dram_channel_width(int channel_width){ printf("one\n"); getchar(); } void set_dram_transaction_granularity(int cacheline_size){ printf("two\n"); getchar(); } //output: one f //my keyboard input two one f //keyboard input tw...

Array index out of bound in C

Why does C differentiates in case of array index out of bound #include <stdio.h> int main() { int a[10]; a[3]=4; a[11]=3;//does not give segmentation fault a[25]=4;//does not give segmentation fault a[20000]=3; //gives segmentation fault return 0; } I understand that it's trying to access memory allocated to pr...

How Does sizeof(Array) work

How does c find at run time the size of array? where is the information about array size or bounds of array stored ? ...

What is the fastest/most efficient way to find the last set bit (msb) in an integer in C?

If I have some integer n, and I want to know the position of the last set bit (that is, if the least significant bit is on the right, I want to know the position of the furthest left bit that is a 1), what is the quickest/most efficient method of finding out? I know that POSIX supports a ffs() method in strings.h to find the first set b...

Illogical benchmarking?

Hello, I witnessed the following weird behavior. I have two functions, which do almost the same - they measure the number of cycles it takes to do a certain operation. In one function, inside the loop I increment a variable; in the other nothing happens. The variables are volatile so they won't be optimized away. These are the functions...

How do I send an array of integers over TCP in C?

I'm lead to believe that write() can only send data buffers of byte (i.e. signed char), so how do I send an array of long integers using the C write() function in the sys/socket.h library? Obviously I can't just cast or convert long to char, as any numbers over 127 would be malformed. I took a look at the question, how to decompose int...

Fastcall GCC example

Could some one provide an example use of fastcall for use with gcc? If possible could you provide the equivalent call without using fastcall and explain how they would be different? ...

Best way to do binary arithmetic in C?

I am learning C and am trying to write a program that will take 2 value from a user and will check if these strings are binary values (composed of the characters 1 or 0 is how I approached it) before attempting to, according to user selection: Add the two values, Subtract input 2 from input 1, or Multiply the two values. I can implem...

Networking with C/C++ in a Windows enviroment

What is the best way to use sockets on the Windows platform? Basic sockets I guess, TCP/IP. Maybe for a chat client, or just for learning. Could someone give me an example of WININET usage? maybe ftpgetfile() ...

How to prevent multiple definitions in C?

I'm a C newbie and I was just trying to write a console application with Code::Blocks. Here's the (simplified) code: main.c: #include <stdio.h> #include <stdlib.h> #include "test.c" // include not necessary for error in Code::Blocks int main() { //t = test(); // calling of method also not necessary return 0; } test.c: void t...

Why do I get weird results when reading an array of integers from a TCP socket?

As was suggested in an answer to my last question (How do I send an array of integers over TCP in C?), I tried to send an array of long int, however I may be doing something to break the solution... #define ARRAY_LEN 4 /* I'm using long because the numbers are very large, * but in this example they're small to save space. */ long orig...

Optimize Binary Search Algorithm

In a binary search, we have two comparisons one for greater than and other for less than, otherwise its the mid value. How would you optimize so that we need to check only once? bool binSearch(int array[], int key, int left, int right) { mid = left + (right-left)/2; if (key < array[mid]) return binSearch(array, key, lef...

developed a strtok alternative.

Hello, I have developed my own version of strtok. Just to practice the use of pointers. Can anyone see any limitations with this or anyway I can improve. void stvstrtok(const char *source, char *dest, const char token) { /* Search for the token. */ int i = 0; while(*source) { *dest++ = *source++; if(*source ...

What is the workaround for unaligned memory access exception on ARM9 using C?

Architecture ARM9. Programming Language C. We have a third-party stack and one of the calls takes a pointer(pBuffer) to a memory location. Within the stack, they are free to move around the pointer passed and access it as they wish. Unfortunately, they offset the passed in pointer and passed it into a another function that tried to do t...

problems Wrapping Patricia Tries using Swig, python

Hello all! I'm trying to wrap the Patricia Tries (Perl's NET::Patricia) to be exposed in python. I am having difficulty with one of the classes. So instances the patricia node (below) as viewed from python have a "data" property. Reading it goes fine, but writing to it breaks. typedef struct _patricia_node_t { u_int bit; /* ...