c

Malloc of 2 bytes gives issues

I am trying to use a malloc of short, something like typedef union _SOME_STRUCT_ { struct { USHORT u:4; USHORT v:4; USHORT w:4; } x; USHORT word; } SOME_STRUCT, *PSOME_STRUCT; PSOME_STRUCT p = malloc (sizeof (SOME_STRUCT)); if (p) { p->x.u = 0; } free (p); // **** RANDOMLY CRASHING HERE **** I am d...

Malloc thread-safe?

Is malloc re-entrant? ...

C++ Equivalent to Designated Initializers?

Recently I've been working on some embedded devices, where we have some structs and unions that need to be initialized at compile time so that we can keep certain things in flash or ROM that don't need to be modified, and save a little flash or SRAM at a bit of a performance cost. Currently the code compiles as valid C99, but without th...

Effects of the `extern` keyword on C functions

In C I did not notice any effect of the extern keyword used before function declaration. At first I thought that when defining extern int f(); in a single file forces you to implement it outside of the file's scope. However I found out that both: extern int f(); int f() {return 0;} and extern int f() {return 0;} compile just fine,...

Selecting a Unique Identifier in C for an Embedded Application

I am currently trying to implement an algorithm to select a unique (16-bit) identifier. The challenge is to do this in an fast way that doesn't use too much memory. The list of currently used identifiers is determined through scanning an external Flash device via a sequence of SPI transactions and is therefore a relatively slow process...

Threadsafe vs re-entrant

Recently, I asked a question, with title as "Is malloc thread safe?", and inside that I asked, "Is malloc re-entrant?" I was under the impression that all re-entrant are thread-safe. Is this assumption wrong? ...

i want redirect tcp packets to user space using netlink raw sockets with netlink_firewall option

can have some sample programs using netlink sockets with firewall protocol. i found but giving some errors like segfault core dump. and error code -22. so need better one iam very new for socket programing. please help me ...

How to convert an arbitrary large integer from base 10 to base 16?

The program requires an input of an arbitrary large unsigned integer which is expressed as one string in base 10. The outputs is another string that expresses the integer in base 16. For example, the input is "1234567890987654321234567890987654321234567890987654321", and the output shall be "CE3B5A137DD015278E09864703E4FF9952FF6B62C1CB1...

How to implement Unix ls -l command in C?

Hi, I am implementing unix ls command in c with all option.But i am not getting any idea about ls -l command. please can some body give me direction of this or give one sample program for it. Thanks ...

Union – useless anachronism or useful old school trick?

I recently came across a great data structures book,"Data Structures Using C" (c) 1991, at a local Library book sale for only $2. As the book's title implies, the book covers data structures using the C programming language. I got the book knowing it would be out-dated but would probably contain lots of advanced C topics that I wouldn't...

can PHP do something like p($i) and it prints "$i is 5" -- C and Ruby can... (that is, to print out "$i" automatically)

I was wondering if PHP can do this as there seems to be no good solution to it yet: p($i) and it will print $i is 5 and p(1 + 2) will print 1 + 2 is 3 and p($i * 2) => $i * 2 is 10 p(3 * factorial(3)) => 3 * factorial(3) is 18 C and Ruby both can do it... in C, it can be done by stringification, and in Ruby, there i...

How large structs can be passed by value efficiently?

The rule of thumb is that it is okay to pass small structs by value and larger ones should be done pointers. My question is where exactly is this cutoff point? How large can the structures be before you are better off passing them by pointer. I know this will vary between platforms, but I assume some rough estimates could be given. A ...

C enum different compilers

I'm building an application that needs to compile on both Windows and Linux. The application is written in C, almost everything works except the MinGW compiler refuses this typedef struct somestruct{ ...snip... enum {NODE, REAL} type; }; somestruct* something; switch (something->type){ case NODE: ...stuff...; break; case ...

Which issues have you encountered due to sequence points in C and C++?

Below are two common issues resulting in undefined behavior due to the sequence point rules: a[i] = i++; //has a read and write between sequence points i = i++; //2 writes between sequence points What are other things you have encountered with respect to sequence points? It is really difficult to find out these issues when the comp...

C pointer to array/array of pointers disambiguation

What is the difference between the following declarations: int* arr1[8]; int (*arr2)[8]; int *(arr3[8]); What is the general rule for understanding more complex declarations? ...

Do I need a lock when only a single thread writes to a shared variable?

I have 2 threads and a shared float global. One thread only writes to the variable while the other only reads from it, do I need to lock access to this variable? In other words: volatile float x; void reader_thread() { while (1) { // Grab mutex here? float local_x = x; // Release mutex? do_stuff_wi...

++ on a dereferenced pointer in C?

Trying to understand the behaviour of pointers in C, i was a little surprised by the following (example code below): #include <stdio.h> void add_one_v1(int *our_var_ptr) { *our_var_ptr = *our_var_ptr +1; } void add_one_v2(int *our_var_ptr) { *our_var_ptr++; } int main() { int testvar; testvar = 63; add_one_v1(&(t...

C: how can I fix warnings like: "comparison between signed and unsigned"

I have been advised to use the following options with GCC, as it helps to avoid a lot of common errors. It turns on a bunch of warnings and -Werror turns them into errors. gcc -pedantic -W -Wall -Wextra -Wshadow -Wstrict-overflow=5 -Wwrite-strings -std=c99 -Werror Given the following test code: #include <stdio.h> int main(void) { ...

Macro-producing macros in C?

I'd like to get the C preprocessor to generate macros for me (i.e., I'm using C99 only). I'd write a macro #define make_macro(in) <...magic here...> and when I put make_macro(name1) make_macro(name2) later in the code, it would expand to #define name1(...) name1_fn(name1_info, __VA_ARGS__) #define name2(...) name2_fn(name2_info, _...

What happens? gets()

Let's consider this two lines: char input[1]; gets(input); Let's say the input is "test". printf("%s\n",input) => "test" but if i use the debugger i see input[0]='t' no input[1]... What exactly happens? ...